当前位置:网站首页>其他服务注册与发现
其他服务注册与发现
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,结果如下:
边栏推荐
- 手机能开户炒股吗 网上开户炒股安全吗
- test
- 小白看MySQL--windows环境安装MySQL
- 为什么EDR需要深度防御来打击勒索软件?
- The most complete hybrid precision training principle in the whole network
- Is it reliable to speculate in stocks by mobile phone? Is it safe to open an account and speculate in stocks online
- [microservice]eureka
- 简单快速的数网络(网络中的网络套娃)
- Understanding of "the eigenvectors corresponding to different eigenvalues cannot be orthogonalized"
- 基于SSMP的宠物医院管理系统
猜你喜欢

Moher College -x-forwarded-for injection vulnerability practice

CPU exception handling

Pinpoint attackers with burp

No clue about complex data?
![The [MySQL] time field is set to the current time by default](/img/40/5f1d3448259ab703c4b5dc29713a99.png)
The [MySQL] time field is set to the current time by default

Network in network (dolls)
![[microservices] understanding microservices](/img/62/e826e692e7fd6e6e8dab2baa4dd170.png)
[microservices] understanding microservices

Safe and cost-effective payment in Thailand
![[test] the content of the hottest test development learning route has been updated again to help pass the customs and open the test of large factories](/img/ee/b7cb528b79036896da781b73620758.jpg)
[test] the content of the hottest test development learning route has been updated again to help pass the customs and open the test of large factories

Installation of xshell and xftp
随机推荐
手机能开户炒股吗 网上开户炒股安全吗
手机炒股靠谱吗 网上开户炒股安全吗
redis详细教程
In the Internet industry, there are many certificates with high gold content. How many do you have?
PHP code audit series (I) basis: methods, ideas and processes
matlab数据类型 —— 字符型
论文学习——降雨场次划分方法对降雨控制率的影响分析
Leetcode 718. Longest repeating subarray (violence enumeration, to be solved)
Can I open an account for stock trading on my mobile phone? Is it safe to open an account for stock trading on the Internet
idea 热启动失效解决方案
Understanding of "the eigenvectors corresponding to different eigenvalues cannot be orthogonalized"
为什么EDR需要深度防御来打击勒索软件?
泰国安全又划算的支付方式
[vscade] preview MD file
Thesis study -- Analysis of the influence of rainfall field division method on rainfall control rate
“message“:“Bad capabilities. Specify either app or appTopLevelWindow to create a session“
kubernetes可视化界面dashboard
超硬核!华为智慧屏上的家庭相册竟可以自动精准分类?
不会写免杀也能轻松过defender上线CS
简单快速的数网络(网络中的网络套娃)