当前位置:网站首页>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 :
边栏推荐
- com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string. at [Source:x
- 目前哪个证券公司炒股开户是最好最安全的?
- 基于SSMP的宠物医院管理系统
- find_ Detailed use guide of CIRC
- Nacos installation guide
- 冲刺强基计划数学物理专题二
- 泰国安全又划算的支付方式
- 大咖讲 | 最前沿的昇思MindSpore开源社区运营的经验分享,快拿出小本本记录呀!
- 复杂数据没头绪?
- The fourth bullet of redis interview eight part essay (end)
猜你喜欢

Deep learning method for solving mean field game theory problems

冲刺强基计划数学物理专题二

Nacos installation guide

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

An article takes you to learn container escape

My advanced learning notes of C language ----- keywords

温故知新--常温常新

根据文件名批量生成文件夹
![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

全網最全的混合精度訓練原理
随机推荐
MindSpore新型轻量级神经网络GhostNet,在ImageNet分类、图像识别和目标检测等多个应用场景效果优异!
全網最全的混合精度訓練原理
find_circ详细使用指南
Target tracking shooting? Target occlusion shooting? With 1.9 billion installed petal apps, what unique features attract users?
串口调试工具 mobaxterm 下载
Cve-2022-30190 follina office rce analysis [attached with customized word template POC]
这3个并发编程的核心,竟然还有人不知道?
论文解读(LG2AR)《Learning Graph Augmentations to Learn Graph Representations》
Ten thousand words explanation - mindarmour Xiaobai tutorial!
07 | 工作流设计:如何设计合理的多人开发模式?
Introduction to software engineering -- Chapter 4 -- formal description technology
com. fasterxml. jackson. databind. exc.MismatchedInputException: Expected array or string. at [Source:x
Oracle database basics concepts
指南针开户安全的吗?
运用物理信息神经网络求解流体力学方程
股票怎样在手机上开户安全吗 网上开户炒股安全吗
05 | 規範設計(下):commit 信息風格迥异、難以閱讀,如何規範?
“message“:“Bad capabilities. Specify either app or appTopLevelWindow to create a session“
Is it safe to buy pension insurance online? Is there a policy?
目标追踪拍摄?目标遮挡拍摄?拥有19亿安装量的花瓣app,究竟有什么别出心裁的功能如此吸引用户?