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

边栏推荐
- SAP ALV summary is inconsistent with exported excel summary data
- 7_ Openresty installation
- When facing the industrial Internet, they even use the ways and methods of consuming the Internet to land and practice the industrial Internet
- pycharm 软件deployment 灰色 无法点
- LabVIEW calculates the camera image sensor resolution and lens focal length
- Is there any discount for opening an account now? In addition, is it safe to open a mobile account?
- 机器学习9-通用逼近器径向基函数神经网络,在新观点下审视PDA和SVM
- In the fourth week of June, the list - flying melon data up main growth ranking list (BiliBili platform) was released!
- 【JS】【掘金】获取关注了里不在关注者里的人
- SWT / anr problem - storagemanagerservice stuck
猜你喜欢

Pulsar的Proxy支持和SNI路由

Ernie-gram, 显式、完备的 n-gram 掩码语言模型,实现了显式的 n-gram 语义单元知识建模。

halcon变量窗口的图像变量不显示,重启软件和电脑都没用

What other hot spots are hidden under 1500W playback? Station B 2 future trends you can't miss

Visual effects, picture to cartoon function

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

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

@ConfigurationProperties和@Value的区别

机器学习10-信念贝叶斯分类器

@The difference between configurationproperties and @value
随机推荐
Fix names in the table (first character uppercase, other lowercase)
Find the length of the common part of two line segments
Leetcode (524) -- match the longest word in the dictionary by deleting letters
Focusing on green and low carbon, data center cooling has entered a new era of "intelligent cooling"
SWT/ANR问题--AMS/WMS
Short message sending solution in medical his industry
[2022] Jiangxi postgraduate mathematical modeling scheme and code
What is the difference between port number and process number?
What are the applications of SMS in enterprises?
When facing the industrial Internet, they even use the ways and methods of consuming the Internet to land and practice the industrial Internet
Proxy support and SNI routing of pulsar
What is PMP?
在unity中使用jieba分词的方法
Delete duplicate email
How to use Jieba participle in unity
ANR问题的分析与解决思路
小程序自定义顶部导航栏,uni-app微信小程序自定义顶部导航栏
Pulsar的Proxy支持和SNI路由
SWT / anr problem - SWT caused by too long dump time
Video tutorial | Chang'an chain launched a series of video tutorial collections (Introduction)