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

边栏推荐
猜你喜欢

CorelDRAW 2022 Chinese Simplified 64 bit direct download

SWT/ANR问题--Binder Stuck

LabVIEW calculates the camera image sensor resolution and lens focal length

(总结一)Halcon基础之寻找目标特征+转正

RocketQA:通过跨批次负采样(cross-batch negatives)、去噪的强负例采样(denoised hard negative sampling)与数据增强(data augment
![Pytorch —— 基礎指北_貳 高中生都能看懂的[反向傳播和梯度下降]](/img/6e/279dbb7a8d7a5ecd240de464c5b8b2.png)
Pytorch —— 基礎指北_貳 高中生都能看懂的[反向傳播和梯度下降]

小程序自定义顶部导航栏,uni-app微信小程序自定义顶部导航栏

CentOS installs multiple versions of PHP and switches

RestCloud ETL WebService数据同步到本地

centos 安装多个版本的php并切换
随机推荐
House change for agricultural products? "Disguised" house purchase subsidy!
Viewing JVM parameters
SWT/ANR问题--ANR/JE引发SWT
SWT / anr problem - storagemanagerservice stuck
AI edge computing platform - beaglebone AI 64 introduction
SAP ALV summary is inconsistent with exported excel summary data
(总结一)Halcon基础之寻找目标特征+转正
How does ZABBIX configure alarm SMS? (alert SMS notification setting process)
零基础自学SQL课程 | 窗口函数
SWT/ANR问题--Dump时间过长导致的SWT
js防抖和节流
Objects and object variables
Alphabet-Rearrange-Inator 3000(字典树自定义排序)
[multi source BFS] 934 Shortest Bridge
機器學習10-信念貝葉斯分類器
What are the applications of SMS in enterprises?
RocketQA:通过跨批次负采样(cross-batch negatives)、去噪的强负例采样(denoised hard negative sampling)与数据增强(data augment
Short video platform development, relying on drawerlayout to achieve side sliding menu effect
With one-stop insight into industry hot spots, the new function "traffic market" of feigua data station B is launched!
pycharm 软件deployment 灰色 无法点