当前位置:网站首页>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
边栏推荐
- 『IDEA』IDEA快捷键使用教程
- SQL injection less26 (filter spaces and comments, and use error injection without spaces)
- [basics of C language] 17 preliminary study of linked list
- JMeter -- response Chinese garbled code processing
- [paper reading] logan:membership influence attacks against generative models
- Luo Yonghao bet on the last entrepreneurial credit
- SQL injection less24 (secondary injection)
- Weilai cup 2022 Niuke summer multi school training camp 1
- EasyUI DataGrid obtains multiple selected data for operation
- Embedded SIG | 分布式软总线
猜你喜欢

yolov1

At the 100 billion yogurt track, dairy giants and new brands have started a tug of war

Chapter 15 MySQL user management

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

7.27 watch first | openeuler is ambitious, open source Huizhi creates the future - the most detailed agenda of the Euler sub forum of the open atom global open source summit is released

在灯塔工厂点亮5G,宁德时代抢先探路中国智造

Embedded sig | distributed soft bus

由若干QWidget实现日历文档

Embedded sig | distributed soft bus

v-model语法糖的实现
随机推荐
A few pictures help you clarify "China's financial institution system"
DTS is equipped with a new self-developed kernel, which breaks through the key technology of the three center architecture of the two places Tencent cloud database
Classification of banking business
Introduction to MySQL database
Iptables prevents nmap scanning and enables incremental backup of binlog
Matlab solution of the densest stacking and smallest circumscribed square of N circles (two-dimensional, three-dimensional, etc. circle packing problem)
DAO 的发展状态
挖财钱堂和启牛学堂哪个更好一些?是安全的吗
Sort out each order when you are in love (it takes two months to sort out in detail)
Leetcode 121: the best time to buy and sell stocks
[paper reading] logan:membership influence attacks against generative models
参数解析与跳石板
Xiaobai learns MySQL - derived table
【Io开发笔记】机智云智能浇花器实战(3)-自动生成代码移植
Detailed explanation of SQL secondary injection
JWT implements login authentication + token automatic renewal scheme, which is the correct posture!
A13处理器成iPhone11系列最大亮点:性能是麒麟980两倍!
Embedded SIG | 分布式软总线
APP信息侦察&夜神模拟器Burp抓包配置
Concept and classification of processes