当前位置:网站首页>其他服务注册与发现
其他服务注册与发现
2022-06-27 00:05:00 【喵先森爱吃鱼】
一、Zookeeper 服务注册与发现
1.1 Zookeeper 安装
直接去官网下载:
Zookeeper-3.7.0
然后解压:
//解压
tar -zxvf zookeeper-3.7.0-bin.tar.gz
//移动到应用目录
sudo mv apache-zookeeper-3.7.0-bin /usr/local/
// 更改默认配置文件名称
cd /usr/local/apache-zookeeper-3.7.0-bin/conf
mv zoo_sample.cfg zoo.cfg
启动 Zookeeper Server:
cd /usr/local/apache-zookeeper-3.7.0-bin/bin
./zkServer.sh start
连接客户端:
./zkCli.sh
停止 Server:
./zkServer stop
1.2 注册中心 Zookeeper
Zookeeper 是一个分布式协调工具,可以实现注册中心功能。在安装好 Zookeeper 后,执行 ./zkServer start,将注册中心启动。
1.3 搭建服务提供者 cloud-provider-payment8004
1.3.1 新建 cloud-provider-payment8004 子模块
1.3.2 修改 pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud2020</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>cloud-provider-payment8004</artifactId>
<dependencies>
<!-- SpringBoot 整合 Web 组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<!-- 引入自己定义的 api 通用包,可以使用 Payment 支付 Entity -->
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency> <!-- SpringBoot 整合 zookeeper 客户端 -->
<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>
1.3.3 配置 application.yml
# 8004 表示注册到 Zookeeper 服务器的支付服务提供者端口号
server:
port: 8004
# 服务别名——Zookeeper注册到注册中心的名称
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: localhost:2181
1.3.4 配置主启动类
@SpringBootApplication
@EnableDiscoveryClient
public class
PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class, args);
}
}
@EnableDiscoveryClient:使用 Consul 或者 Zookeeper作为注册中心时,可使用该注解向注册中心注册服务
1.3.5 controller
@RestController
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@RequestMapping("/payment/zk")
public String payment(){
return "springcloud with zookeeper: " + serverPort + "\t" + UUID.randomUUID().toString();
}
}
1.3.6 测试
如果安装的 Zookeeper 版本过低,在启动时可能会如下错误:
这是由于 Zookeeper 的 jar 包版本冲突导致的,如下:
解决方案:
修改 pom.xml 文件,将自带的 zookeeper3.5.3 排除,重新引入符合自己安装的 Zookeeper 的版本 jar
<!-- SpringBoot 整合 zookeeper 客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!-- 先排除自带的 zookeeper3.5.3-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加 zookeeper3.4.9 版本 -->
<dependency>
<groupId> org.apache.zookeeper </groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
我自己安装的 Zookeeper 版本为 3.7.0,没有遇到这个问题,建议还是使用高版本。
启动后,在浏览器中访问 http://localhost:8004/payment/zk
登录 Zookeeper 客户端,执行以下命令:
最终可以获得一串 Json 字符串:
{
"name": "cloud-provider-payment",
"id": "bba4b3ac-4175-4b49-b47c-c1c8e989be3c",
"address": "IP地址",
"port": 8004,
"sslPort": null,
"payload": {
"@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
"id": "application-1",
"name": "cloud-provider-payment",
"metadata": {
}
},
"registrationTimeUTC": 1656235221350,
"serviceType": "DYNAMIC",
"uriSpec": {
"parts": [
{
"value": "scheme",
"variable": true
},
{
"value": "://",
"variable": false
},
{
"value": "address",
"variable": true
},
{
"value": ":",
"variable": false
},
{
"value": "port",
"variable": true
}
]
}
}
思考: Zookeeper 中的节点是临时节点还是持久节点?
临时节点。将 8004 服务关闭后,可以看到 Zookeeper 会将创建的节点删除。
1.4 服务消费者 cloud-consumerzk-order80
1.4.1 创建 cloud-consumerzk-order80 子模块
1.4.2 修改 pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud2020</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>cloud-consumerzk-order80</artifactId>
<dependencies>
<!-- SpringBoot 整合 Web 组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<!-- 引入自己定义的 api 通用包,可以使用 Payment 支付 Entity -->
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency> <!-- SpringBoot 整合 zookeeper 客户端 -->
<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>
1.4.3 配置 application.yml
server:
port: 80
spring:
application:
name: cloud-consumer-order
cloud:
# 注册到 Zookeeper 的地址
zookeeper:
connect-string: localhost:2181
1.4.4 配置主启动类
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZKMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderZKMain80.class, args);
}
}
1.4.5 创建 ApplicationContextConfig,配置 RestTemplate
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
1.4.6 controller
@RestController
@Slf4j
public class OrderZKController {
public static final String INVOKE_URL = "http://cloud-provider-payment";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/zk")
public String paymentInfo(){
return restTemplate.getForObject(INVOKE_URL + "/payment/zk", String.class);
}
}
1.4.7 测试
先启动 8004 服务提供者,再启动 80 服务消费者,然后进入 Zookeeper 客户端,查看节点创建情况:
浏览器访问 http://localhost/consumer/payment/zk,结果如下:
边栏推荐
猜你喜欢

超硬核!华为智慧屏上的家庭相册竟可以自动精准分类?

技术干货|什么是大模型?超大模型?Foundation Model?

Sword finger offer 10- ii Frog jumping on steps
![[vscode] setting sync, a plug-in for synchronizing extensions and settings](/img/e0/4889b59105e9815d11ae31988f58f2.jpg)
[vscode] setting sync, a plug-in for synchronizing extensions and settings

Target tracking shooting? Target occlusion shooting? With 1.9 billion installed petal apps, what unique features attract users?

Why does EDR need defense in depth to combat ransomware?

Competition Registration | one of the key ai+ scientific computing competitions - China open source scientific software creativity competition, competing for 100000 bonus!

Oracle database basics concepts

07 | 工作流设计:如何设计合理的多人开发模式?

redis详细教程
随机推荐
冲刺强基计划数学物理专题二
新型冠状病毒变异Delta毒株的模拟(MindSPONGE应用)
Is it safe to open a compass account?
Technical dry goods | top speed, top intelligence and minimalist mindspore Lite: help Huawei watch become more intelligent
【Mysql】时间字段默认设置为当前时间
我的c语言进阶学习笔记 ----- 关键字
Sword finger offer 10- ii Frog jumping on steps
论文解读(LG2AR)《Learning Graph Augmentations to Learn Graph Representations》
Simple and fast digital network (network dolls in the network)
指南针开户安全的吗?
简单快速的数网络(网络中的网络套娃)
这3个并发编程的核心,竟然还有人不知道?
Is it safe to buy pension insurance online? Is there a policy?
基于SSMP的宠物医院管理系统
Great health industry annual must attend event, 2022 Shandong International Great Health Industry Expo
[微服务]Nacos
In the Internet industry, there are many certificates with high gold content. How many do you have?
1+1<2 ?! Interpretation of hesic papers
PHP code audit series (I) basis: methods, ideas and processes
當Transformer遇見偏微分方程求解