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

边栏推荐
猜你喜欢

Visual effects, picture to cartoon function

SWT/ANR问题--StorageManagerService卡住

Short message sending solution in medical his industry
![Pytorch —— 基礎指北_貳 高中生都能看懂的[反向傳播和梯度下降]](/img/6e/279dbb7a8d7a5ecd240de464c5b8b2.png)
Pytorch —— 基礎指北_貳 高中生都能看懂的[反向傳播和梯度下降]

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

@ConfigurationProperties和@Value的区别

PMP是什麼?

如何在智汀中实现智能锁与灯、智能窗帘电机场景联动?

A preliminary understanding of operator overloading

nacos配置中心使用教程
随机推荐
Pytorch —— 基礎指北_貳 高中生都能看懂的[反向傳播和梯度下降]
Is there any discount for opening an account now? In addition, is it safe to open a mobile account?
Machine learning 9-universal approximator radial basis function neural network, examining PDA and SVM from a new perspective
map数组函数
How does the property send a text message to the owner?
SWT / anr problem - SWT caused by long execution time of native method
SWT/ANR问题--Native方法执行时间过长导致SWT
PMP是什么?
Zero foundation self-study SQL course | window function
Some uses of Halcon array
With one-stop insight into industry hot spots, the new function "traffic market" of feigua data station B is launched!
详解数据治理知识体系
VirtualBox installation enhancements
Pytorch - - Basic Reference North Deux élèves du secondaire peuvent comprendre [Rétropropagation et Gradient descendant]
5款主流智能音箱入门款测评:苹果小米华为天猫小度,谁的表现更胜一筹?
Leetcode interview question 17.10 Main elements
股票开账户如何优惠开户?还有,在线开户安全么?
C # generates PPK files in putty format (supports passphrase)
Ernie-gram, 显式、完备的 n-gram 掩码语言模型,实现了显式的 n-gram 语义单元知识建模。
Cluster method synchronous execution framework suona