当前位置:网站首页>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
边栏推荐
- 基于超算平台气象预警并行计算架构研究
- Arrays.asList()
- Comparator (for arrays.sort)
- RPC typical framework
- Leetcode 1249. Remove invalid brackets (awesome, finally made)
- CMU puts forward a new NLP paradigm - reconstructing pre training, and achieving 134 high scores in college entrance examination English
- 金仓KFS数据集中场景(多对一)部署
- What are the ways to simulate and burn programs? (including common tools and usage)
- 基于OpenStreetMap+PostGIS的地理位置系统 论文文档+参考论文文献+项目源码及数据库文件
- Arrays. asList()
猜你喜欢

SQL注入漏洞(繞過篇)

Detailed explanation of spark specification

Coscon'22 lecturer solicitation order

時創能源沖刺科創板:擬募資11億 年營收7億淨利反降36%

Jincang KFS data cascade scenario deployment

Double buffer transparent encryption and decryption driven course paper + project source code based on minifilter framework

Redis6 note02 configuration file, publish and subscribe, new data type, jedis operation

Démarrer avec Apache shenyu

Use of Presto visualization client-yanagishima20.0

Compilation of learning from Wang Shuang (1)
随机推荐
try-catch-finally
Gaussdb others scenarios with high memory
Spark Tuning common configuration parameters
基于Minifilter框架的双缓冲透明加解密驱动 课程论文+项目源码
Démarrer avec Apache shenyu
Golden sun education listed in the U.S.: a small cap medium cap stock with a market value of USD 360million
Geographic location system based on openstreetmap+postgis paper documents + reference papers + project source code and database files
relu与sigmod的比较
Free access to the global human settlements layer (ghsl) dataset from Gee
Xishan technology rushes to the scientific innovation board: it plans to raise 660million yuan. Guoyijun and his wife have 60% of the voting rights
GC
杭州/北京内推 | 阿里达摩院招聘视觉生成方向学术实习生(人才计划)
wait()、notify()和notifyAll()、sleep()、Condition、await()、signal()
scrapy+scrapyd+gerapy 爬虫调度框架
SQL injection vulnerability (type chapter)
An interesting logic SRC mining
牛客网:分糖果问题
Kingbasees plug-in DBMS of Jincang database_ OUTPUT
Redis6笔记02 配置文件,发布和订阅,新数据类型,Jedis操作
Ladder side tuning: the "wall ladder" of the pre training model