注册中心——Zookeerper
zookeerper 是一个分布式协调工具,可以实现注册中心
首先在 Linux 系统中进行 zookeerper 服务; 执行步骤:
- 关闭防火墙
systemctl stop firewalld - 利用 docker 拉取镜像
docker pull zookeerper 3.4.9 - 启动容器
docker run --privileged=true -d --name zookeeper --publish 2181:2181 -d zookeeper:3.4.9 - 查看容器
docker ps - 进入服务器开启 2181 端口
这里我们拉去的版本是 3.4.9,如果拉取最新版本在启动 zookeerper 时将会启动失败,所以我们降低一下版本。
- 进入 zookeerper 容器进到 bin 目录运行 zookeerper
./zkServer.sh start ,出现下图则表明启动成功 - 执行客户端命令
./zkCli.sh ,出现下图 这时候表明我们的 zookeerper 已经启动成功,接下来将模块注入 zookeerper
服务提供者注入 Zookeerper
- 新建 cloud-provider-payment8004 的 maven 工程
- 编写 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.atguigu.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.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<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>
</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 文件
server:
port: 8004
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 47.93.36.142:2181
- 主启动类
@SpringBootApplication
@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;
@RequestMapping("/payment/zk")
public String paymentzk(){
return "springCloud——" + serverPort + UUID.randomUUID().toString();
}
}
到门前位置,我们就启动一下 8004 的服务提供者,启动之后在 linunx命令中查看注入情况 如果看到上图显示情况,则说明注入成功。我们进行接口访问
访问接口: 测试成功。
接着用 zookeerper 客户端操作
[zk: localhost:2181(CONNECTED) 0] ls /
[services, zookeeper]
[zk: localhost:2181(CONNECTED) 1] ls /service
Node does not exist: /service
[zk: localhost:2181(CONNECTED) 2] ls /servicer
Node does not exist: /servicer
[zk: localhost:2181(CONNECTED) 3] ls /services
[cloud-provider-payment]
[zk: localhost:2181(CONNECTED) 4] ls /services/cloud-provider-payment
[c221c2b6-b77e-4a2d-a08a-7144c6323017]
[zk: localhost:2181(CONNECTED) 5] ls /services/cloud-provider-payment/c221c2b6-b77e-4a2d-a08a-7144c6323017
[]
[zk: localhost:2181(CONNECTED) 6] get /services/cloud-provider-payment/c221c2b6-b77e-4a2d-a08a-7144c6323017
{"name":"cloud-provider-payment","id":"c221c2b6-b77e-4a2d-a08a-7144c6323017","address":"windows10.microdone.cn","port":8004,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"application-1","name":"cloud-provider-payment","metadata":{}},"registrationTimeUTC":1645085451427,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}
格式化上面的 json
{
"name": "cloud-provider-payment",
"id": "c221c2b6-b77e-4a2d-a08a-7144c6323017",
"address": "windows10.microdone.cn",
"port": 8004,
"sslPort": null,
"payload": {
"@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
"id": "application-1",
"name": "cloud-provider-payment",
"metadata": {}
},
"registrationTimeUTC": 1645085451427,
"serviceType": "DYNAMIC",
"uriSpec": {
"parts": [{
"value": "scheme",
"variable": true
}, {
"value": "://",
"variable": false
}, {
"value": "address",
"variable": true
}, {
"value": ":",
"variable": false
}, {
"value": "port",
"variable": true
}]
}
}
服务消费者注入 Zookeerper
- 新建消费者 cloud-consumerzk-order80 的 maven 工程
- 编写 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.atguigu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumerzk-order80</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<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>
</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 文件
server:
port: 80
spring:
application:
name: cloud-consumer-payment
cloud:
zookeeper:
connect-string: 47.93.36.142:2181
- 编写主程序
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZKMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderZKMain80.class, args);
}
}
- 编写配置文件
config
@Configuration
public class ApplicationContextBean {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
- 编写 controller 类
@RestController
@Slf4j
public class OrderZKController {
private static final String INVOKE_URL = "http://cloud-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/zk")
public String paymentInfo(){
return restTemplate.getForObject(INVOKE_URL + "/payment/zk", String.class);
}
}
开始运行服务消费者 80 端口,运行之后我们查看是否注入到 zookeerper,如果出现下面图片表示注入成功 网页测试地址 localhost/consumer/payment/zk 如果出现上图效果,则说明我们的生产者与消费者都注入 Zookeerper 成功。
|