当前位置:网站首页>Other service registration and discovery
Other service registration and discovery
2022-06-27 00:40:00 【Miaoshiansen likes fish】
One 、Zookeeper Service registration and discovery
1.1 Zookeeper install
Go directly to the official website to download :
Zookeeper-3.7.0
Then decompress :
// decompression
tar -zxvf zookeeper-3.7.0-bin.tar.gz
// Move to app directory
sudo mv apache-zookeeper-3.7.0-bin /usr/local/
// Change the default profile name
cd /usr/local/apache-zookeeper-3.7.0-bin/conf
mv zoo_sample.cfg zoo.cfg
start-up Zookeeper Server:
cd /usr/local/apache-zookeeper-3.7.0-bin/bin
./zkServer.sh start
Connect to the client :
./zkCli.sh
stop it Server:
./zkServer stop
1.2 Registry Center Zookeeper
Zookeeper It's a distributed coordination tool , It can realize the function of registration center . After installation Zookeeper after , perform ./zkServer start, Start registry .
1.3 Build service provider cloud-provider-payment8004
1.3.1 newly build cloud-provider-payment8004 Sub module
1.3.2 modify 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 Integrate Web Components -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<!-- Introduce your own definition of api General package , have access to Payment payment Entity -->
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency> <!-- SpringBoot Integrate zookeeper client -->
<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 To configure application.yml
# 8004 Sign up to Zookeeper The port number of the server's payment service provider
server:
port: 8004
# Service alias ——Zookeeper The name of the registry
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: localhost:2181
1.3.4 Start the main class configuration
@SpringBootApplication
@EnableDiscoveryClient
public class
PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class, args);
}
}
@EnableDiscoveryClient: Use Consul perhaps Zookeeper As a registry , You can use this annotation to register services with the registry
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 test
If installed Zookeeper Version is too low , The following errors may occur during startup :
This is because Zookeeper Of jar Package version conflict , as follows :
Solution :
modify pom.xml file , Will bring your own zookeeper3.5.3 exclude , Reintroduce your own installation Zookeeper Version of jar
<!-- SpringBoot Integrate zookeeper client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!-- Exclude the self-contained zookeeper3.5.3-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- add to zookeeper3.4.9 edition -->
<dependency>
<groupId> org.apache.zookeeper </groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
I installed it myself Zookeeper Version is 3.7.0, I didn't encounter this problem , It is recommended to use a higher version .
After starting , Access in a browser http://localhost:8004/payment/zk
Sign in Zookeeper client , Execute the following command :
You can finally get a string of Json character string :
{
"name": "cloud-provider-payment",
"id": "bba4b3ac-4175-4b49-b47c-c1c8e989be3c",
"address": "IP Address ",
"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
}
]
}
}
reflection : Zookeeper Is the node in a temporary node or a persistent node ?
Temporary node . take 8004 After the service is shut down , You can see Zookeeper The created node will be deleted .
1.4 Serving consumers cloud-consumerzk-order80
1.4.1 establish cloud-consumerzk-order80 Sub module
1.4.2 modify 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 Integrate Web Components -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<!-- Introduce your own definition of api General package , have access to Payment payment Entity -->
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency> <!-- SpringBoot Integrate zookeeper client -->
<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 To configure application.yml
server:
port: 80
spring:
application:
name: cloud-consumer-order
cloud:
# Sign up to Zookeeper The address of
zookeeper:
connect-string: localhost:2181
1.4.4 Start the main class configuration
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZKMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderZKMain80.class, args);
}
}
1.4.5 establish ApplicationContextConfig, To configure 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 test
Start... First 8004 Service providers , Restart 80 Serving consumers , Then enter Zookeeper client , View node creation :
Browser access http://localhost/consumer/payment/zk, give the result as follows :
边栏推荐
- Deep learning method for solving mean field game theory problems
- 【Mysql】时间字段默认设置为当前时间
- 手机能开户炒股吗 网上开户炒股安全吗
- 05 | 規範設計(下):commit 信息風格迥异、難以閱讀,如何規範?
- 复杂数据没头绪?
- How to use Pinia (I) introduce Pinia into the project
- The fourth bullet of redis interview eight part essay (end)
- JS library for number formatting
- 剑指 Offer 10- II. 青蛙跳台阶问题
- 全網最全的混合精度訓練原理
猜你喜欢
![[微服务]认识微服务](/img/62/e826e692e7fd6e6e8dab2baa4dd170.png)
[微服务]认识微服务

Why does EDR need defense in depth to combat ransomware?

Introduction to software engineering -- Chapter 4 -- formal description technology

简单快速的数网络(网络中的网络套娃)
![[vscade] preview MD file](/img/b8/0413eaade0a7da9ddb5494b093665c.png)
[vscade] preview MD file

剑指 Offer 10- II. 青蛙跳台阶问题

The most complete hybrid precision training principle in the whole network

国内外最好的12款项目管理系统优劣势分析

基于SSMP的宠物医院管理系统

MindSpore新型轻量级神经网络GhostNet,在ImageNet分类、图像识别和目标检测等多个应用场景效果优异!
随机推荐
Freescale 单片机概述
Target tracking shooting? Target occlusion shooting? With 1.9 billion installed petal apps, what unique features attract users?
Is it safe for CITIC Securities Commission to open an online account and speculate in stocks
网络中的网络(套娃)
50 tips that unity beginners can definitely use
Installation of xshell and xftp
No clue about complex data?
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
Let agile return to its original source -- Some Thoughts on reading the way of agile neatness
Thesis study -- Analysis of the influence of rainfall field division method on rainfall control rate
[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
Intrusion trace cleaning
消息队列简介
Mindspire, a domestic framework, cooperates with Shanshui nature conservation center to find and protect the treasure life in the "China water tower"
[microservices] Understanding microservices
[microservices] understanding microservices
[微服务]认识微服务
Nacos安装指南
Leetcode skimming 4 Find the median of two positive arrays
CPU exception handling