当前位置:网站首页>Nacosclient client is built, and micro services are registered into Nacos
Nacosclient client is built, and micro services are registered into Nacos
2022-07-19 16:43:00 【Hu Shang】
Import the following coordinates in the project :
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
In the global profile application.yml
File for the corresponding configuration
server:
port: 8083
# Specify the application name ,Nacos This name will be used as the service name
spring:
application:
name: order-service
cloud:
nacos:
# Appoint Nacos Address of service , The default value is localhost:8848
server-addr: 127.0.0.1:8848
discovery:
# Appoint Nacos Login user name and password of management interface , The default value is nacos
username: nacos
password: nacos
# Specify the namespace
# The function of namespace is to isolate different service instances , The default name is public
namespace: public
from Spring Cloud Edgware Version start , There is no need to add @EnableDiscoveryClient
Note the , If it is a previous low version, you need to add @EnableDiscoveryClient
or @EnableEurekaClient
, You can go to maven In the central warehouse of spring-cloud-dependencies
What are the versions of .
And then start the service , Log in to Nacos You can find that the service has been registered
Next, let's test and pass Nacos The registry communicates Services
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/add")
public String add(){
// Use RestTemplate call stock-service Interfaces in services
String message = restTemplate.getForObject("http://stock-service/stock/reduce", String.class);
return " checkout success !" + message;
}
}
When you start the service, you will find an error
This is because , Service from Nacos After getting the list of services in the registry , You also need a load balancer to make service calls , As shown in the figure below
So here we are RestTemplate Here is an annotation of load balancer @LoadBalanced
,Nacos In fact, it is integrated Ribbon,
@Configuration
public class RestConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(RestTemplateBuilder builder){
RestTemplate restTemplate = builder.build();
return restTemplate;
}
}
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-Y1eulPsF-1658022301252)(E:\Java note \image\springcloud_alibaba\image-20220606123538401.png)]
Then it can be called and passed , This is because Nacos There is no way to resolve the address corresponding to the service name , We need to rely on the load balancer to resolve the address according to the service name , Then call .
边栏推荐
- 4273. 链表合并(DAY 64)
- Gesture Recognition Dataset: Jester 数据集解压
- Why do many people still work when they know that they don't earn money?
- 抽象静态方法
- The development idea plug-in failed to pass the audit due to compatibility problems
- 【Unity3D】UGUI之InputField
- codeforces每日5题(均1500)-第十八天
- 03. Dichotomy, complexity, dynamic array, hash table and ordered table
- 如何由 moment 对象 获取 时间对象
- Clouds want clothes, flowers want looks, spring breeze blows the sill, Revlon (romantic code implementation)
猜你喜欢
Yolov7: how to export the correct onnx based on your own training model
STM32中断梅开二度(一)
CSDN認證C1級別學習筆記 - Web進階篇
使用深度學習制作機器人大腦圖紙
Spichi rushes to the scientific innovation board: annual revenue of 300million and loss of 340million. Alibaba and Lenovo star are shareholders
【foobar 2000】如何在手机、电脑上播放局域网内另一台电脑上存储的音乐?FTP服务器、UPnP/DLNA协议、构建Music server、创建音乐服务器
使用深度学习制作机器人大脑图纸
Handwritten simple promise code comments
字符集7-10
Utiliser l'apprentissage profond pour faire des dessins de cerveau robotique
随机推荐
flex 布局 justify-content:space-between 最后一行左对齐的解决方案
Eslint Usage Summary
面试题 01.04. 回文排列-辅助数组法
JMeter 21 天打卡 day08
About the problem of escape characters in remote command execution Using SSH command
Thesis reading_ Medical NLP_ SMedBERT
【Unity3D】UGUI之InputField
Want to self-study software testing? Ruthless ridicule.
mysql函数汇总之日期和时间函数
epoll的ET工作模式和LT工作模式
Nacos中使用ribbon
Uniapp authorized login to obtain user information and code
The fourth day of MATLAB learning (decision statement)
面试快速复习(二):交叉熵为什么有用
Aspose. OCR 22.6 for . NET//Aspose. OCR
torch dist分布式数据汇总
Weekly resume of personal IP lab · issue 19
[foundation of deep learning] how to calculate convolution
使用go json path的demo
Complete size comparison with bit operation