当前位置:网站首页>Nacos installation and use
Nacos installation and use
2022-06-25 11:30:00 【sermonlizhi】
1.1 Nacos install
Refer to official documentation Reference:https://nacos.io/zh-cn/docs/quick-start.html
After the installation is completed, you can use http://xxx.com:8848/nacos visit Nacos The console of , The user name and password are nacos
Then create a custom namespace , The main function of namespaces is resource isolation 
Nacos Is a service registry , It is also a configuration center , The following demonstrates service registration and configuration management
1.2 Service release
With shop-user For example , stay pom Add... To the file spring-cloud-starter-alibaba-nacos-discovery rely on , Then add... To the startup class @EnableDiscoveryClient annotation
<!--nacos client -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
@SpringBootApplication
@EnableDiscoveryClient
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class,args);
}
}
The configuration file is as follows :
server:
port: 9527
spring:
application:
name: shop-user
datasource:
name: userDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://xxx.com:3306/shop?useUnicode=true&characterEncoding=UTF8&serverTimezone=Asia/Shanghai
username: root
password: lz123
druid:
max-active: 20
min-idle: 10
initial-size: 5
test-while-idle: false
cloud:
nacos:
discovery:
server-addr: 120.79.221.55:8848
namespace: 24712b7c-05ad-4b79-af97-1d202431f521
group: LZ_GROUP_MASTER
notes : Sub module dependencies must have spring-boot-starter rely on , Otherwise, the service cannot be registered to nacos
After starting the service , You can go to Nacos You can see the registered services in the service list of the console 
1.3 Service configuration
1.3.1 In the sub module pom Add... To the file config rely on
<!--config rely on -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
1.3.2 add to nacos config To configure
notes : You can't use the original application.yml As a configuration file , It's a new one bootstrap.yml As a configuration file
Profile priority ( From high to low ):
bootstrap.properties -> bootstrap.yml -> application.properties -> application.yml
Create a bootstrap.yml The configuration file
spring:
profiles:
active: dev
application:
name: shop-user
cloud:
nacos:
config:
server-addr: xxx.com:8848
namespace: 24712b7c-05ad-4b79-af97-1d202431f521
group: LZ_GROUP_MASTER
file-extension: yml
1.3.3 stay nacos Add configuration in


explain : Why configuration is needed spring.application.name , It's because it's made up of Nacos Configuration Management dataId Part of the field .
stay Nacos Spring Cloud in ,dataId The complete format is as follows :
${prefix}-${spring.profiles.active}.${file-extension}
prefixThe default isspring.application.nameValue , You can also configure items throughspring.cloud.nacos.config.prefixTo configure thespring.profiles.activeThat is, the current environment corresponds toprofile.Be careful : When
spring.profiles.activeIt's empty time , Corresponding connector - There will be no ,dataIdThe splicing format of becomes${prefix}.${file-extension}file-exetensionTo configure the data format of the content , You can use the configuration itemspring.cloud.nacos.config.file-extensionTo configure the . Currently only supportedpropertiesandyamltype .
We put the previous application.yml The connection information of the database configured in is put into the configuration item , Start again shop-user Program , The database can still be initialized successfully
notes :Nacos Support DNS agreement , That is to say, you can server-address Configure domain name at , The domain name is a simple IP Corresponding domain name , And Nacos It doesn't matter in itself
You can also add some customized configurations in the configuration file of the configuration center , Then, where the program needs to be used , Add a... To the class @RefreshScope annotation , That is, the configuration can be modified , The program dynamically updates the configuration without restarting the service
notes : The configuration file of the configuration center is the same as the local configuration file , Only the configuration center can realize dynamic update
边栏推荐
- 芯片的发展史和具体用途以及结构是什么样的
- What are the functions of arm64 assembly that need attention?
- 4 life distributions
- 龙书虎书鲸书啃不动?试试豆瓣评分9.5的猴书
- Shichuang Energy sprint Technology Innovation Board: le chiffre d'affaires annuel prévu de 1,1 milliard de RMB est de 0,7 milliard de RMB, en baisse de 36%
- MySQL and Oracle processing CLOB and blob fields
- Wait (), notify (), notifyAll (), sleep (), condition, await (), signal()
- Tidb applicable scenarios
- Kingbasees plug-in DBMS of Jincang database_ OUTPUT
- Vulnérabilité à l'injection SQL (contournement)
猜你喜欢

时创能源冲刺科创板:拟募资11亿 年营收7亿净利反降36%

Compilation of learning from Wang Shuang (1)

Niuke.com: host scheduling

Spark history server performance improvement (I) -- Application List

SQL注入漏洞(绕过篇)

Leetcode 1249. 移除无效的括号(牛逼,终于做出来了)

金太阳教育美股上市:市值3.6亿美元 成小盘中概股

Hangzhou / Beijing neitui Ali Dharma academy recruits academic interns in visual generation (talent plan)

寿命分布 4种

What are the ways to simulate and burn programs? (including common tools and usage)
随机推荐
scrapy+scrapyd+gerapy 爬虫调度框架
基于超算平台气象预警并行计算架构研究
Jincang KFS data cascade scenario deployment
贝叶斯
GaussDB others内存比较高的场景
Shichuang Energy sprint Technology Innovation Board: le chiffre d'affaires annuel prévu de 1,1 milliard de RMB est de 0,7 milliard de RMB, en baisse de 36%
金仓数据库 KingbaseES 插件identity_pwdexp
Handler、Message、Looper、MessageQueue
Bayes
Apache ShenYu 入門
2022 PMP project management examination agile knowledge points (2)
4 life distributions
MySQL synchronous data configuration and shell script implementation
Hangzhou / Beijing neitui Ali Dharma academy recruits academic interns in visual generation (talent plan)
Spannable and editable, spannablestring and spannablestring
Ladder Side-Tuning:预训练模型的“过墙梯”
An interesting logic SRC mining
try-catch-finally
Dynamic programming to solve stock problems (Part 1)
Technical practice and development trend of video conference all in one machine