当前位置:网站首页>Nacos is used as a registration center
Nacos is used as a registration center
2022-07-27 09:30:00 【Charge8】
I learned about Nacos And installed nacos-server service . Next, simply build a microservice project , Use Nacos Be a registry .
One 、 Project structures,
Project version selection :
Spring Cloud Alibaba Version selection :https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
Choose here :

1、 Create parent module
stay pom.xml Medium management version .
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.8.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- spring-cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring-cloud-alibaba-dependencies -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、 establish order service
Configure service provider order service , Service providers can use Nacos The service registration discovery function of registers its services to the Nacos server On .
2.1 Introduce dependencies
The current project pom Introduction in nacos rely on
<!-- nacos Service registration and discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
2.2 The configuration file
To configure nacos Registry Center
server:
port: 18082
servlet:
context-path: /
spring:
application:
name: app-order # Microservice name
# To configure nacos Address of Registration Center
cloud:
nacos:
discovery:
server-addr: 192.168.xxx.xxx:8848
#namespace: f16bcf35-810d-4b5b-a936-bd2abb8582a3
#cluster-name: SH

More configuration :https://github.com/alibaba/spring-cloud-alibaba/wiki/Nacos-discovery
3、 establish user service
Configure service consumers user service , Service consumers can go through Nacos The service registration discovery function of Nacos server Get the service it wants to call .
3.1 Introduce dependencies
The current project pom Introduce dependency in
<!-- nacos Service registration and discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
3.2 The configuration file
To configure nacos Registry Center
server:
port: 18081
servlet:
context-path: /app-user
spring:
application:
name: app-user # Microservice name
# To configure nacos Address of Registration Center
cloud:
nacos:
discovery:
server-addr: 192.168.xxx.xxx:8848
#namespace: f16bcf35-810d-4b5b-a936-bd2abb8582a3
#cluster-name: SH
3.3 Use RestTemplate Make service calls
1) Turn on @LoadBalanced And Ribbon Integration of .
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
2) Use RestTemplate Make service calls
Write a controller call .
@RequestMapping(value = "/findOrderByUserId/{id}")
public R findOrderByUserId(@PathVariable("id") Integer id) {
log.info(" according to userId:" + id + " Query order information ");
String url = "http://app-order/order/findOrderByUserId/" + id;
R result = restTemplate.getForObject(url, R.class);
return result;
}
4、 start-up user and order service
stay nacos Check whether the registration is successful in the management interface .

stay nacos The management end , You can also specify some configurations , such as , The weight , grouping , Cluster etc. .

5、 Test access user service
Browser access test controller, Test service invocation ok.
Here we are ,Nacos Just use it as a registration center OK 了 .Namespace and Group There are many usage scenarios for configuration items , Know for yourself .
– Stay Hungry , stay foolish .
边栏推荐
- Music experience ceiling! Emotional design details of 14 Netease cloud music
- 645. Wrong set
- 工程材料期末考试试卷
- 如何注册码云账号
- 全排列递归思路整理
- 【无标题】
- Data interaction based on restful pages
- Read the paper learning to measure changes: full revolutionary Siamese metric networks for scene change detect
- 2022 software test interview questions 200 big factory interview true questions brushed to get 10K positions
- IDL 6S lookup table
猜你喜欢

ES6 new - Operator extension

Data interaction based on restful pages
![[Wuhan University of technology] information sharing for the preliminary and second examinations of postgraduate entrance examination](/img/15/298ea6f7367741e1e085007c498e51.jpg)
[Wuhan University of technology] information sharing for the preliminary and second examinations of postgraduate entrance examination
![[C language - zero foundation lesson 6] input and output sentence format and compound sentence](/img/d5/b9935a37b634db7f5740779009e44f.png)
[C language - zero foundation lesson 6] input and output sentence format and compound sentence
![[leetcode -- the first day of introduction to programming ability] basic data type [statistics of odd numbers within the range / average wage after removing the minimum wage and maximum wage)](/img/23/497c013d105d1906ae8a37dd4e18ad.png)
[leetcode -- the first day of introduction to programming ability] basic data type [statistics of odd numbers within the range / average wage after removing the minimum wage and maximum wage)

基于 FPGA 按键控制呼吸灯原理、仿真及验证全过程

1344. 时钟指针的夹角

You haven't heard of such 6 question brushing websites, have you? Are you out?

Proposed relocation! 211 the new campus of China University of Petroleum (East China) is officially opened!

The whole process of principle, simulation and verification of breath lamp controlled by FPGA keys
随机推荐
ArkUI框架中的两个小技巧
ESP8266-Arduino编程实例-ADC
基于ArkUI eTS开发的坚果笑话(NutJoke)
Day 6 of learning C language
ES6 new - array part
vscode使用remote-ssh连接以及连接失败的解决方法
[C language - zero foundation _ study _ review _ lesson 4] data types and operations
内存墙简介
1640. Can you connect to form an array -c language implementation
Eureka 延迟注册的一个坑
MySQL transaction
Lua function nested call
NCCL (NVIDIA Collective Communications Library)
6S parameters
七月集训(第12天) —— 链表
材料工程基础-重点
Google Earth Engine APP——打印点的坐标到控制台上和map上,设置样式并更新
[C language _ study _ exam _ review lesson 3] overview of ASCII code and C language
2022软件测试面试题 200道大厂面试真题 刷完拿到10k职位
Special exercises for beginners of C language to learn code for the first time