SpringCloud整合Zookeeper代替Eureka
服务提供者注册到Zookeeper
启动zookeeper
依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!--3.5.3的版本,与自己启动的版本不一致,会报错-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加和启动的zookeeper版本一致的依赖-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
</dependency>
配置文件:
server:
port: 8004
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 192.168.136.140:2181
启动类添加注解:@EnableDiscoveryClient
@EnableDiscoveryClient
@SpringBootApplication
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class,args);
}
}
启动工程!
测试:zookeeper进入bin目录下,./zkCli.sh
1.输入ls /
,发现多个services
目录
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper, flink0000000001, canal-adapter, services, locks, tmp, name, create, hadoop, config]
2.进入services
,发现了我们注册上去的微服务,成功!
[zk: localhost:2181(CONNECTED) 1] ls /services
[cloud-payment-service]
3.继续进入目录,直到
注意:4777ad2f-2a31-4bd8-9e74-2cf0a92ee635
可能不一样
[zk: localhost:2181(CONNECTED) 6] ls /services/cloud-payment-service/4777ad2f-2a31-4bd8-9e74-2cf0a92ee635
[]
4.get /services/cloud-payment-service/4777ad2f-2a31-4bd8-9e74-2cf0a92ee635
[zk: localhost:2181(CONNECTED) 7] get /services/cloud-payment-service/4777ad2f-2a31-4bd8-9e74-2cf0a92ee635
{"name":"cloud-payment-service","id":"4777ad2f-2a31-4bd8-9e74-2cf0a92ee635","address":"DESKTOP-P72JK02","port":8004,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"application-1","name":"cloud-payment-service","metadata":{}},"registrationTimeUTC":1593528567106,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}
cZxid = 0x28f
ctime = Wed Jun 17 15:47:17 CST 2020
mZxid = 0x28f
mtime = Wed Jun 17 15:47:17 CST 2020
pZxid = 0x28f
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x1000001ff820031
dataLength = 534
numChildren = 0
复制上面的json串,拿到网站https://www.json.cn/ 解析
思考:微服务注册到zookeeper上的节点是临时节点还是持久化节点?
临时。
将工程关闭。等一会儿,就可以看到把节点清了。
zk: localhost:2181(CONNECTED) 11] ls /services/cloud-payment-service
[4777ad2f-2a31-4bd8-9e74-2cf0a92ee635]
[zk: localhost:2181(CONNECTED) 12] ls /services/cloud-payment-service
[]
服务消费者注册到Zookeeper
依赖同上
配置文件改端口即可
启动类添加注解:@EnableDiscoveryClient
配置类:
@Configuration
public class ApplicationContextConfig {
@LoadBalanced
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
测试controller:
@RestController
public class OrderZkController {
public static final String INVOME_URL = "http://cloud-payment-service";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/zk")
public String payment (){
String result = restTemplate.getForObject(INVOME_URL+"/payment/zk",String.class);
return result;
}
}
http://cloud-payment-service
就是服务提供者注册到zookeeper
上的微服务名
[zk: localhost:2181(CONNECTED) 2] ls /services
[cloud-payment-service, cloud-consumer-order]
启动服务消费者,即可在zookeeper上看到该微服务的名字,如上。
访问地址:http://localhost/consumer/payment/zk
,能调通提供者的服务,就成功了。