目录
注册中心Zookeeper
五步曲:
zookeeper服务提供者
1、03-cloud-provider-payment-zookeeper-8004
2、pom.xml
3、改yum
4、主启动
5、业务类
测试1:
测试2:
服务节点是临时节点还是持久节点
粗划分:
细划分:
此时:
zookeeper服务消费者
1、03-cloud-consumer-order-eureka-80
2、pom.xml
3、改yum
4、主启动
5、业务类
config
controller
测试
访问测试地址
注册中心Zookeeper
zookeeper是一个分布式协调工具,可以实现注册中心功能
zookeeper服务器取代Eureka服务器,zk作为服务注册中心
安装Zookeeper见博客:安装Zookeeper_gh_xiaohe的博客-CSDN博客
五步曲:
1、建moudle
2、改pom
3、改yum
4、主启动
5、业务类
zookeeper服务提供者
1、03-cloud-provider-payment-zookeeper-8004
2、pom.xml
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.gh</groupId>
<artifactId>00-cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!-- SpringBoot整合zookeeper客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</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>
若出现依赖问题见博客: Maven依赖冲突_gh_xiaohe的博客-CSDN博客
3、改yum
#8004表示注册到zookeeper服务器的支付服务提供者端口号
server:
port: 8004
#服务别名----注册zookeeper到注册中心名称
spring:
application:
name: 03-cloud-provider-payment-zookeeper-8004
cloud:
zookeeper:
connect-string: xxx.xxx.xxx.xxx:2181
4、主启动
@SpringBootApplication
@EnableDiscoveryClient//该注解用于向使用consul或者zookeeper作为注册中心时注册服务
public class PaymentZookeeperMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentZookeeperMain8004.class, args);
}
}
5、业务类
/**
* @author gh Email:@2495140780qq.com
* @Description
* @date 2022-03-28-上午 11:09
*/
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@RequestMapping(value = "/payment/zk")
public String paymentzk() {
return "springcloud with zookeeper: " + serverPort + "\t" + UUID.randomUUID().toString();
}
}
测试1:
访问:http://localhost:8004/payment/zk
测试2:
[zk: localhost:2181(CONNECTED) 6] ls /
[services, zookeeper]
[zk: localhost:2181(CONNECTED) 7] ls /services/03-cloud-provider-payment-zookeeper-8004
[4d4a95dd-81db-40bc-a953-287088b0057f]
[zk: localhost:2181(CONNECTED) 8] ls /services/03-cloud-provider-payment-zookeeper-8004/4d4a95dd-81db-40bc-a953-287088b0057f
[]
[zk: localhost:2181(CONNECTED) 9] get /services/03-cloud-provider-payment-zookeeper-8004/4d4a95dd-81db-40bc-a953-287088b0057f
{"name":"03-cloud-provider-payment-zookeeper-8004","id":"4d4a95dd-81db-40bc-a953-287088b0057f","address":"localhost","port":8004,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"application-1","name":"03-cloud-provider-payment-zookeeper-8004","metadata":{}},"registrationTimeUTC":1648458730092,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}
使用JSON工具转换 :json工具 - 在线工具
服务节点是临时节点还是持久节点
粗划分:
临时节点
持久节点
细划分:
临时节点
带序号临时节点
持久节点
带序号持久节点
此时:
关闭程序后, 一段时间后无响应, 删除
重新链接后,发现,服务名一致,但是流水号已经发生了改变 【临时节点】
zookeeper服务消费者
1、03-cloud-consumer-order-zookeeper-80
2、pom.xml
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot整合zookeeper客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</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>
3、改yum
#8004表示注册到zookeeper服务器的支付服务提供者端口号
server:
port: 80
#服务别名----注册zookeeper到注册中心名称
spring:
application:
name: 03-cloud-consumer-order-eureka-80
cloud:
zookeeper:
connect-string: xxx.xxx.xxx.xxx:2181
4、主启动
/**
* @author gh Email:@2495140780qq.com
* @Description
* @date 2022-03-28-下午 7:20
*/
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZookeeperMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderZookeeperMain80.class,args);
}
}
5、业务类
config
/**
* @author gh Email:@2495140780qq.com
* @Description
* @date 2022-03-28-下午 7:22
*/
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
controller
/**
* @author gh Email:@2495140780qq.com
* @Description
* @date 2022-03-28-下午 7:24
*/
@RestController
public class OrderZKController {
public static final String INVOKE_URL = "http://03-cloud-provider-payment-zookeeper-8004";
@Autowired
private RestTemplate restTemplate;
@GetMapping("consumer/payment/zk")
public String paymentInfo() {
return restTemplate.getForObject(INVOKE_URL + "/payment/zk", String.class);
}
}
测试
访问测试地址
http://localhost/consumer/payment/zk