Zookeeper
PPG007 ... 2021-12-28 Less than 1 minute
# Zookeeper
# 依赖引入
将上面的 eureka 依赖替换为 zookeeper 依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
1
2
3
4
2
3
4
# Provider 模块
# 编写 SpringBoot 配置文件
server:
port: 8004
spring:
application:
name: payment-service
datasource:
url: jdbc:mysql://192.168.3.14:3306?serverTimezone=UTC
username: root
password: 123456zch@ZCH
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
cloud:
zookeeper:
connect-string: 39.107.112.172:2181,115.28.211.227:2181,150.158.153.216:2181 # ZooKeeper集群
discovery:
instance-host: 192.168.3.55 # 本实例的IP地址
instance-port: 8004 # 本实例的端口号
mybatis-plus:
global-config:
db-config:
table-prefix: spring_cloud.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
其他内容与 Eureka 相同,修改主启动类注解,使用 @EnableDiscoveryClient
替换 Eureka 注解。
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentStarter8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentStarter8004.class,args);
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# Consumer 模块
# 编写 SpringBoot 配置文件
server:
port: 80
spring:
application:
name: order-service-zk-80
datasource:
url: jdbc:mysql://192.168.3.14:3306?serverTimezone=UTC
username: root
password: 123456zch@ZCH
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
cloud:
zookeeper:
connect-string: 39.107.112.172:2181,115.28.211.227:2181,150.158.153.216:2181
discovery:
instance-host: 192.168.3.14
instance-port: 80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
主启动类与 Provider 相同。
# 注入 RestTemplate
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
1
2
3
4
5
2
3
4
5
# 修改 controller 的 url
private static final String BASE_URL="http://payment-service";
1
修改为 ZooKeeper 中服务对应的节点名称,与 spring.application.name
相同。