当前位置:网站首页>Introduction to Nacos as a registry and configuration center - realize remote call, dynamically obtain configuration files and database configuration information
Introduction to Nacos as a registry and configuration center - realize remote call, dynamically obtain configuration files and database configuration information
2022-07-26 22:34:00 【Walking away and sleepwalking】
download nacos To local , You can double-click startup.cmd To start the , The default is 8848 port
As a registry
First, import dependency
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>Configure in profile Nacos Server Address
# apply name
spring.application.name=nacos1
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848Add comments to the main class , Register your own services
@EnableDiscoveryClient // Open service registration discovery
Start at this time nacos after , You can register the project after starting the project

The remote invocation
Feign It's a declarative one Http client , Provides http The request template , By writing simple interfaces and inserting annotations , You can define http Request parameters 、 Format 、 Address and other information .
Feign Integrated Ribbon( Load balancing ) and Hystrix( Service failure ), We can eliminate the need to explicitly use these two components
Remote call usage :
1. Add dependencies to the project , introduce feign
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>2. Writing interface , Write interface tell springcloud Need to call remote service
@FeignClient("nacos1") // Corresponding service name
public interface feigndemo {
// Find the method you want to call other services and copy it
@RequestMapping("/lzq/testfeign") // The path should be written completely
public R testfeign();
}At this point, when the current method calls the interface , It will automatically find the corresponding service in the registry and access the corresponding interface
You also need to add comments to the main class @EnableFeignClients, Open the remote call of the client
At the same time, you can specify the full class name of the corresponding interface package , As long as the service starts, it will automatically scan
@EnableFeignClients(basePackages = "com.lzq.feign")Configuration center

First, introduce dependency to establish connections
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>Create... In the project bootstrap.properties file , This document will take precedence over application.properties File loading
Configure the service name and address
spring.application.name=nacos1
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848Modify profile dynamically
land nacos website , Configure the information that needs to be dynamically modified in the configuration list
Data ID Name the service +.properties, Profile selection properties, After writing the configuration, click publish
Add comments to the corresponding method @RefreshScope annotation
@RefreshScope
@RestController
public class Demo {
@Value("${shop.zhekou}")
private String zk;
}At this point, get the corresponding value through the code , You can dynamically modify the corresponding configuration value
details : Namespace 、 config set 、 To configure ID、 Configure grouping
Namespace : Default public, The main function is to configure isolation ,( You can test through multiple namespaces 、 Generating environment ), It can be done by bootstrap.properties Namespace specified in
spring.cloud.nacos.config.namespace=xxxxx
Configure grouping : The same namespace can be configured differently according to different activities , Specify the corresponding group
spring.cloud.nacos.config.group=devManage data sources
spring.cloud.nacos.config.ext-config[0].data-id=datasource.yml
spring.cloud.nacos.config.ext-config[0].group=dev
spring.cloud.nacos.config.ext-config[0].refresh=true // Dynamic refresh
spring.cloud.nacos.config.ext-config[1].data-id=mybatis.yml
spring.cloud.nacos.config.ext-config[1].group=dev
spring.cloud.nacos.config.ext-config[1].refresh=true
边栏推荐
- Leetcode:857. Minimum cost of employing K workers [think in blocks + start with simplicity]
- JSON字符串转化为JSON对象,获取某个key的值,判断某个key是否存在
- 判断numpy array数组的维数
- Mate30系列发布:华为的重构影像之路还能走多远?
- Weilai cup 2022 Niuke summer multi school training camp 1
- Blog Garden beautification skills summary
- 挖财钱堂和启牛学堂哪个更好一些?是安全的吗
- : could not determine a constructor for the tag ! RootAdmin
- yolov1
- Qt中为工程添加资源文件、给按钮添加图片
猜你喜欢

模块8(消息队列存储消息数据的mysql表格)

The potential of emerging markets is unlimited, and advance.ai risk control products help Chinese offshore enterprises build a solid foundation for safe development

Summary of shell related commands

Makefile related syntax summary (openc910)

nvidia-smi报错:NVIDIA-SMI has failed because it couldn‘t communicate with the NVIDIA driver 完整记录

Xiaobai learns MySQL - derived table

2022 latest Tibet Construction Safety Officer simulation question bank and answers

Liepin questionnaire star has become a "parasite" on wechat

JSON字符串转化为JSON对象,获取某个key的值,判断某个key是否存在

Introduction to MySQL database
随机推荐
Interview Essentials
At the 100 billion yogurt track, dairy giants and new brands have started a tug of war
[idea] tutorial on using idea shortcut keys
JWT implements login authentication + token automatic renewal scheme, which is the correct posture!
MySQL数据库介绍
"Mongodb" here are all the basic mongodb operations you want
Does Guosen Securities charge for opening a mobile account? Is it safe to open an account?
Alibaba three sides: how to solve the problems of MQ message loss, duplication and backlog?
Add resource files for the project and pictures for buttons in QT
Xiaobai learns MySQL - derived table
Sequence table implementation
基于C语言的页式文件系统
Y78. Chapter IV Prometheus' monitoring system and practice -- Prometheus' service discovery mechanism (IX)
What is Base64?
博途PLC输出通道映射FC
各有一项指纹专利被宣告无效!汇顶与思立微均正式回应!
EasyUI DataGrid obtains multiple selected data for operation
Blog Garden beautification skills summary
7.27抢先看 | openEuler 志高远,开源汇智创未来-开放原子全球开源峰会欧拉分论坛最详细议程出炉
SQL injection less26 (filter spaces and comments, and use error injection without spaces)