zookeeper 服务注册中心 😶
1、SpringCloud 整合 Zookeeper 代替 Eureka
1-1、注册中心 Zookeeper
zookeeper 是一个分布式协调工具,可以实现注册中心功能
关闭 Linux 服务器防火墙后动 zookeeper 服务器
zookeeper 服务器取代 Eureka 服务器,zk 作为服务注册中心
2、服务提供者
2-1、新建模块 cloud-provider-payment8004
如果 zookeeper 的版本和导入的 jar 包版本不一致,启动就会报错,由于 zk-discovery 和 zk 之间的 jar 包冲突的问题。
下图给出的是 spring-cloud-starter-zookeeper-discovery 所引入的 zookeeper jar 包版本,可在父 pom 中找到,如果自己的 zoookeeper 版本和其一致,可不用修改。
pom.xml
解决这种冲突,需要在 pom 文件中,排除掉引起冲突的 jar 包,添加和服务器 zookeeper 版本一致的 jar 包,但是新导入的 zookeeper jar 包 又有 slf4j 冲突问题,于是再次排除引起冲突的 jar 包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| <?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>springCloud2023</artifactId> <groupId>com.jcvv.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment8004</artifactId>
<properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.9</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency>
<dependency> <groupId>com.jcvv.springcloud</groupId> <artifactId>cloud-api-common</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</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> </project>
|
application.yml
1 2 3 4 5 6 7 8 9
| server: port: 8004
spring: application: name: cloud-provider-payment cloud: zookeeper: connect-string: 119.3.211.104:2181
|
主启动类
1 2 3 4 5 6 7
| @SpringBootApplication @EnableDiscoveryClient public class PaymentMain8004 { public static void main(String[] args) { SpringApplication.run(PaymentMain8004.class,args); } }
|
controller
1 2 3 4 5 6 7 8 9 10
| @RestController 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(); } }
|
测试
然后就能在 zk 里查到注册的服务信息了。
2-2、新建消费者模块 cloud-consumerzk-order80
pom.xml,application.yml,主启动类与服务提供者类似
config 类,注入 RestTemplate
1 2 3 4 5 6 7 8
| @Configuration public class ApplicationContextBean { @Bean @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
|
controller 层也是和之前类似:
1 2 3 4 5 6 7 8 9 10 11 12
| public class OrderZKController { public static final String INVOKE_URL = "http://cloud-provider-payment";
@Resource private RestTemplate restTemplate;
@GetMapping(value = "/consumer/payment/zk") public String paymentInfo(){ String result = restTemplate.getForObject(INVOKE_URL+"/payment/zk",String.class); return result; } }
|
相关链接:Consul服务注册与发现😶