Zookeeper服务注册与发现
学习地址:https://www.bilibili.com/video/BV18E411x7eT?p=28
源码分享:https://github.com/mobiwusihuan288/SpringCloud2020
环境准备
zookeeper版本:3.4.9
下载地址:http://archive.apache.org/dist/zookeeper/zookeeper-3.4.9/
- 下载安装zookeeper
[root@localhost ~]# cd /usr/local
# 解压
[root@localhost local]# tar -zxvf zookeeper-3.4.9.tar.gz
# 复制一份配置文件
[root@localhost zookeeper-3.4.9]# cp conf/zoo_sample.cfg conf/zoo.cfg
[root@localhost zookeeper-3.4.9]# cd bin
[root@localhost bin]# pwd
/usr/local/zookeeper-3.4.9/bin
- 关闭防火墙
[root@localhost bin]# systemctl stop firewalld
[root@localhost bin]# systemctl status firewalld
- 查看IP
[root@localhost bin]# ifconfig
# 192.168.110.128
注册中心Zookeeper
zookeeper是一个分布式协调工具,可以实现注册中心功能
关闭Linux服务器防火墙后启动zookeeper服务器
zookeeper服务器取代Eureka服务器,zk作为服务注册中心
服务提供者
cloud-provider-payment8004
-
建module
-
写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>springcloud2020</artifactId>
<groupId>com.nuc.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment8004</artifactId>
<dependencies>
<dependency>
<groupId>com.nuc.springcloud</groupId>
<artifactId>cloud-api-commons</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.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>
</project>
- 写YML
#8004表示注册到zookeeper服务器的支付服务提供的端口号
server:
port: 8004
#服务别名-注册zookeeper到注册中心名称
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 192.168.110.128
- 主启动类
@SpringBootApplication
//该注解用于向使用consul或zookeeper作为注册中心时注册服务
@EnableDiscoveryClient
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class,args);
}
}
- Controller
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/payment/zk")
public String paymentzk(){
return "springcloud with zookeeper:"+serverPort+"\t"+ UUID.randomUUID().toString();
}
}
-
启动8004
jar包冲突
maven
- 修改POM
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!--排除zk3.5.3-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加zk 3.4,9版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
- 启动zookeeper
[root@localhost bin]# ./zkServer.sh start
[root@localhost bin]# ./zkCli.sh
- 测试
http://localhost:8004/payment/zk
思考:服务节点是临时节点还是持久节点
是临时的
关闭--重启,查看
服务消费者
cloud-consumerzk-order80
参考cloud-provider-payment8004
-
建module
-
写POM(参考cloud-provider-payment8004)
-
写YML
server:
port: 80
spring:
application:
name: cloud-consumer-order
cloud:
# 注册到zookeeper地址
zookeeper:
connect-string: 192.168.110.128
- 主启动
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZKMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderZKMain80.class,args);
}
}
- 业务类
- bean
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
- controller
@RestController
@Slf4j
public class OrderZKController {
public static final String INVOME_URL = "http://cloud-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/zk")
public String payment() {
String result = restTemplate.getForObject(INVOME_URL + "/payment/zk", String.class);
return result;
}
}
- 测试
http://localhost:8004/payment/zk
http://localhost/consumer/payment/zk
zookeeper