当前位置:网站首页>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 foundBe 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

边栏推荐
- What is PMP?
- (summary I) Halcon Foundation's target finding features + becoming a regular
- pycharm 软件deployment 灰色 无法点
- (translation) reasons why real-time inline verification is easier for users to make mistakes
- 最新微信ipad协议 CODE获取 公众号授权等
- Calculate special bonus
- Pulsar geo replication/ disaster recovery / regional replication
- The latest wechat iPad protocol code obtains official account authorization, etc
- UE4渲染管线学习笔记
- Analysis and solution of anr problems
猜你喜欢

Image preloading in JS
![[2022] Jiangxi postgraduate mathematical modeling scheme and code](/img/f4/86b0dc2bd49c3a54c1e0538b31d458.png)
[2022] Jiangxi postgraduate mathematical modeling scheme and code

LabVIEW计算相机图像传感器分辨率以及镜头焦距

Restcloud ETL实践之数据行列转换

What is PMP?

How does ZABBIX configure alarm SMS? (alert SMS notification setting process)

Int and bit group turn to each other

5款主流智能音箱入门款测评:苹果小米华为天猫小度,谁的表现更胜一筹?

Small program cloud development -- wechat official account article collection

机器学习10-信念贝叶斯分类器
随机推荐
SWT/ANR问题--Dump时间过长导致的SWT
Visual effects, picture to cartoon function
How does the property send a text message to the owner?
SWT / anr issues - ams/wms
How to use Jieba participle in unity
SWT/ANR问题--StorageManagerService卡住
Applet custom top navigation bar, uni app wechat applet custom top navigation bar
map数组函数
VirtualBox installation enhancements
如果我在北京,到哪里开户比较好?另外,手机开户安全么?
Calculate special bonus
centos 安装多个版本的php并切换
5款主流智能音箱入门款测评:苹果小米华为天猫小度,谁的表现更胜一筹?
Thread Detach
Proxy support and SNI routing of pulsar
SWT/ANR问题--ANR/JE引发SWT
项目管理是什么?
Leetcode 面试题 17.10. 主要元素
RocketQA:通过跨批次负采样(cross-batch negatives)、去噪的强负例采样(denoised hard negative sampling)与数据增强(data augment
How to realize the scene linkage of intelligent lock, lamp and intelligent curtain motor in zhiting?