一. 安裝Zookeeper
zk的安裝過程什么的就不廢話了,這里有我之前安裝的分布式的一個zk,可以參照下zk集群安裝,本篇不再強調分布式,而是使用zk作為服務中心,大家可以不用配zk集群,用單機一樣的~
1.1 壓縮包
提取碼 :ds2b
1.2 安裝解壓到服務器上
這一步不做過多講解
1.3 關閉防火牆
-
sudo systemctl stop firewalld 臨時關閉
-
sudo systemctl disable firewalld ,然后reboot 永久關閉
-
sudo systemctl status firewalld 查看防火牆狀態。
二. 創建新模塊 cloud-provider-payment8004
2.1 修改POM文件引入依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloud2020</artifactId> <groupId>com.atguigu.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-provider-payment8004</artifactId> <dependencies> <!--springboot整合zookeeper客戶端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> </dependency> <!--Springboot整合web組件--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--引入自定義的api通用包,可以使用payment支付Entity--> <dependency> <groupId>com.atguigu.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
2.2 創建 application.yml 配置文件,IP你要填你自己服務器的IP哦
#8004表示注冊到zookeeper服務器的支付服務提供者端口號
server:
port: 8004
#服務別名
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 192.168.65.10:2181
2.3 主啟動類
/** * @Author: cws * @Date: 12:56 2020/8/13 * @Description: * @Version v1.0 */ @SpringBootApplication @EnableDiscoveryClient //該注解用於想使用consul或者zookeeper作為注冊中心時注冊服務 public class Payment8004 { public static void main(String[] args) { SpringApplication.run(Payment8004.class, args); } }
2.4 業務類 controller層
/** * @Author: cws * @Date: 13:10 2020/8/13 * @Description: * @Version v1.0 */ @RestController @Slf4j public class PaymentController { @Value("${server.port}") private String serverPort; @GetMapping(value = "/zk/get") public String paymentzk() { return "Spring cloud with zk port: " + serverPort + "\t" + UUID.randomUUID().toString(); } }
2.5 測試
連上我們的zkclient后,查找我們注冊進來的服務,可以找到就代表成功注冊進來
三. 創建訂單服務module cloud-consumerzk-order80
4.1 創建模塊(重復步驟不講)
4.2 改pom
<dependencies> <!--springboot整合zookeeper客戶端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> <!--排除自帶的zookeeper--> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <!--添加zookeeper3.4.14版本--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> <!--springboot整合web組件--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
4.3 建 application.yml
server: port: 80 spring: application: name: cloud-consumer-order cloud: #注冊到zookeeper地址 zookeeper: connect-string: 公網IP地址:2181
4.4 主啟動
/** * @Author: cws * @Date: 15:24 2020/8/13 * @Description: * @Version v1.0 */ @SpringBootApplication @EnableDiscoveryClient public class OrderZKMain80 { public static void main(String[] args) { SpringApplication.run(OrderZKMain80.class, args); } }
4.5 業務類
消費者需要調用 RestTemplate 來調用遠程服務
/** * @Author: cws * @Date: 15:27 2020/8/13 * @Description: * @Version v1.0 */ @Configuration public class ApplicationContextConfig { @Bean @LoadBalanced //負載均衡的能力 public RestTemplate getRestTemplate() { return new RestTemplate(); } }
/** * @Author: cws * @Date: 15:37 2020/8/13 * @Description: * @Version v1.0 */ @RestController @Slf4j public class OrderZKController { public static final String INVOKE_URL = "http://cloud-provider-payment"; private RestTemplate restTemplate; @GetMapping(value = "/consumer/zk/get") public String paymentInfo() { String result = restTemplate.getForObject(INVOKE_URL + "/zk/get", String.class); return result; } }