当前位置:网站首页>Nacos configuration center tutorial
Nacos configuration center tutorial
2022-07-01 02:28:00 【kjshuan】
nacos Configuration center project
yml Configuration switching url Address
# First step : Create a configuration 4 individual yml
first application.yml spring: application: name: myconf01 profiles: active: test server: port: 9001 the second application-dev.yml url: http://localhost:9999/dev Third application-prov.yml url: http://localhost:9997/prov The fourth one application-test.yml url: http://localhost:9998/test ## among profiles: active: test activc Specify which one to use below url Address
The second step is configuration service
@Service public class MyService { @Value("${url}") private String url; public void displayUrl(){ System.out.println(url); } }
Step 3 test class test
@SpringBootTest class NcaosspringbootApplicationTests { @Resource private MyService myService; @Test void contextLoads() { myService.displayUrl(); } }
Click on the run !!!
Nacos** Configuration center use **
# First step : To configure pom Import dependence
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
# The second step : To configure yml
# establish application.yml # Be careful name: nacosconf Want to be with nacos The names configured in the browser are consistent spring: application: name: nacosconf cloud: nacos: discovery: server-addr: 192.168.64.200:8848 username: nacos password: nacos namespace: public profiles: active: dev server: port: 12003 # establish bootstrap.yml spring: cloud: nacos: config: server-addr: 192.168.64.200:8848 username: nacos password: nacos namespace: public
# The third step : Configure service class
package com.kgc.mynacos.myconfig.services; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Service; @Service // Sync refresh modify nacos In the configuration of the It can be automatically updated here @RefreshScope public class ReadConfService { @Value("${user.name}") private String name; @Value("${user.age}") private String age; public String getInfo(){ return name+"======="+age; } }
# Step four : Writing test classes
package com.kgc.mynacos.myconfig; import com.kgc.mynacos.myconfig.services.ReadConfService; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; @SpringBootTest public class MyTest { @Resource private ReadConfService rcs; @Test public void test01(){ while(true){ System.out.println(rcs.getInfo()); try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } } } # Be careful :Test Guide package has 2 individual choose api This #@springBootTest Don't misspell the following words # Test class use Do not use private!!!!! Otherwise an error no tests were found
Be careful
#bootstrap.yml To configure spring: cloud: nacos: config: server-addr: 192.168.64.200:8848 username: nacos password: nacos namespace: public file-extension: yaml # Customize file-extension: yaml # Cooperate with browser nacos Modified yaml group: yf01 # Group name in browser group: yf01
nacos Configure open permissions
vim /opt/soft/nacos8848/conf/application.properties jps shutdown.sh kill -9 (jps The numbers come out ) startup.sh
Custom extension
The first step is to configure yml
#application.yml spring: application: name: nacosconf cloud: nacos: discovery: server-addr: 192.168.64.200:8848 username: test password: test namespace: 66f8bc4c-bb18-41c7-8b6f-e5a84dde11b9 server: port: 12003 #bootstrap.yml spring: cloud: nacos: config: server-addr: 192.168.64.200:8848 username: test password: test namespace: 66f8bc4c-bb18-41c7-8b6f-e5a84dde11b9 shared-configs: - data-id: myconf.properties group: yf01 refresh: true - data-id: myconf1.peoperties group: yf01 refresh: true - data-id: myconf3.properties group: yf01 refresh: true #extensionConfigs and sharedConfigs Function the same # Extended by custom Data Id To configure , It can solve the problem of configuration sharing among multiple applications , It can also support multiple configuration files for one application .
The second step nacos Create the role Add permissions
Step 3 clone the service list
Step 4 click test Successful launch
边栏推荐
- Applet custom top navigation bar, uni app wechat applet custom top navigation bar
- 7_ Openresty installation
- Zero foundation self-study SQL course | window function
- 如何在智汀中實現智能鎖與燈、智能窗簾電機場景聯動?
- How does ZABBIX configure alarm SMS? (alert SMS notification setting process)
- js防抖和节流
- Go import self built package
- Static domain and static method
- (总结一)Halcon基础之寻找目标特征+转正
- Video tutorial | Chang'an chain launched a series of video tutorial collections (Introduction)
猜你喜欢
FL studio20.9 fruit software advanced Chinese edition electronic music arrangement
What other hot spots are hidden under 1500W playback? Station B 2 future trends you can't miss
Restcloud ETL实践之数据行列转换
SWT / anr problem - binder stuck
Pychar open remote directory remote host
Pychart software deployment gray unable to point
Applet custom top navigation bar, uni app wechat applet custom top navigation bar
零基础自学SQL课程 | 窗口函数
@ConfigurationProperties和@Value的区别
Leetcode interview question 17.10 Main elements
随机推荐
Analysis and solution of anr problems
JS anti shake and throttling
Gartner研究:在中国,混合云的采用已成为主流趋势
Clickhouse 消除由group by产生的间隙
Int and bit group turn to each other
CorelDRAW 2022中文精简64位直装版下载
[graduation season · advanced technology Er] - summary from graduation to work
Zero foundation self-study SQL course | window function
Alphabet rearrange inator 3000 (dictionary tree custom sorting)
Pytoch -- foundation refers to the north_ II. What high school students can understand [back propagation and gradient descent]
Rocketqa: cross batch negatives, de noised hard negative sampling and data augmentation
PMP是什麼?
7_ Openresty installation
[multi source BFS] 934 Shortest Bridge
端口号和进程号的区别?
C # generates PPK files in putty format (supports passphrase)
FL Studio20.9水果软件高级中文版电音编曲
我想知道股票开户怎么开户?究竟网上开户是否安全么?
SWT / anr problem - anr/je causes SWT
Visual effects, picture to cartoon function