发布时间:2022-12-05 13:30
前言:
臧克家:“有的人活着,他其实已经死了”
Eureka:“你怕是想直接念我的身份证吧!”
Eureka2.0停更,一代新人换旧人!Eureka逐渐被Nacos,Consul,Zookeeper 替代,本文介绍Zookeeper 实现服务的注册
1.Zookeeper 简介:
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
2.服务提供者:
2.1 在父工程下,新建Module : cloud-provider-payment8004
2.2 引入相关依赖:
com.mk
cloud-api-common
${project.version}
org.springframework.cloud
spring-cloud-starter-zookeeper-discovery
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
2.3 application.yml 配置文件
#8004 表示注册到 zookeeper 服务器的端口号
server:
port: 8004
#服务别名---注册zookeeper到注册中心的名称
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
# zk地址,此处是集群的话,使用逗号隔开
connect-string: 192.168.111.128:2181
2.4 主启动类
@SpringBootApplication
//该注解用于consul或者zk作为注册中心时注册服务
@EnableDiscoveryClient
public class ZkPaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(ZkPaymentMain8004.class,args);
}
}
2.5 控制器
@RestController
@Slf4j
public class ZkPaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/payment/zk")
public String paymentzk() {
return "springcloud with zookeeper:" + serverPort + "\t" + UUID.randomUUID().toString();
}
}
完美,启动测试
额,懂得都懂,不用我教你们吧!!
jar包冲突,我的Zookeeper版本为3.4的
修改pom.xml
com.mk
cloud-api-common
${project.version}
org.springframework.cloud
spring-cloud-starter-zookeeper-discovery
org.apache.zookeeper
zookeeper
org.apache.zookeeper
zookeeper
3.4.11
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j2
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
再次启动,注册成功
3.服务消费者:
3.1 在父工程下,新建Module : cloud-consumerzk-order80
3.2 引入相关依赖:
com.mk
cloud-api-common
${project.version}
org.springframework.cloud
spring-cloud-starter-zookeeper-discovery
org.apache.zookeeper
zookeeper
org.apache.zookeeper
zookeeper
3.4.11
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j2
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
3.3 application.yml
server:
port: 80
spring:
application:
name: cloud-consumer-order
cloud:
zookeeper:
connect-string: 192.168.111.128:2181
3.4 RestTemplate 进行服务调用
@Configuration
public class ApplicationContextConfig {
@LoadBalanced
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
3.5 控制器
@RestController
public class OrderZKController {
//使用服务名的方式调用
public static final String INVOME_URL = "http://cloud-provider-payment";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/zk")
public String payment (){
String result = restTemplate.getForObject(INVOME_URL+"/payment/zk",String.class);
return result;
}
}
3.6 启动类
@SpringBootApplication
@EnableDiscoveryClient
public class ZkOrderMain80 {
public static void main(String[] args) {
SpringApplication.run(ZkOrderMain80.class,args);
}
}