当前位置:网站首页>nacos配置中心使用教程
nacos配置中心使用教程
2022-07-01 02:11:00 【kjshuan】
nacos配置中心 项目
yml配置切换url地址
#第一步:创建配置4个yml
第一个application.yml
spring:
application:
name: myconf01
profiles:
active: test
server:
port: 9001
第二个application-dev.yml
url:
http://localhost:9999/dev
第三个application-prov.yml
url:
http://localhost:9997/prov
第四个application-test.yml
url:
http://localhost:9998/test
##其中 profiles:
active: test
activc指定下面使用那个url地址第二步配置service
@Service
public class MyService {
@Value("${url}")
private String url;
public void displayUrl(){
System.out.println(url);
}
}第三步测试类测试
@SpringBootTest
class NcaosspringbootApplicationTests {
@Resource
private MyService myService;
@Test
void contextLoads() {
myService.displayUrl();
}
}点击运行!!!

Nacos**配置中心使用**


#第一步:配置pom 导入依赖
<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>
#第二步:配置yml
#创建application.yml #注意name: nacosconf 要和nacos浏览器里配置的名一致 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 #创建bootstrap.yml spring: cloud: nacos: config: server-addr: 192.168.64.200:8848 username: nacos password: nacos namespace: public
#第三步:配置服务类
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
//同步刷新 修改nacos里的配置 这边能自动更新
@RefreshScope
public class ReadConfService {
@Value("${user.name}")
private String name;
@Value("${user.age}")
private String age;
public String getInfo(){
return name+"======="+age;
}
}
#第四步:编写测试类
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);
}
}
}
}
#注意:Test导包有2个 选api这个
#@springBootTest后面单词别拼错
#测试类使用 不要使用private!!!!!不然报错no tests were found注意点
#bootstrap.yml配置 spring: cloud: nacos: config: server-addr: 192.168.64.200:8848 username: nacos password: nacos namespace: public file-extension: yaml #自定义 file-extension: yaml #配合浏览器nacos修改的yaml group: yf01 #浏览器里的分组名 group: yf01
nacos配置开启权限
vim /opt/soft/nacos8848/conf/application.properties jps shutdown.sh kill -9 (jps出来的数字) startup.sh
自定义扩展
第一步配置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 和sharedConfigs 功能相同 #通过自定义扩展的 Data Id 配置,既可以解决多个应用间配置共享的问题,又可以支持一个应用有多个配置文件。
第二步nacos创建角色 添加权限



第三步克隆服务列表

第四步点击测试 启动成功

边栏推荐
猜你喜欢

CorelDRAW 2022中文精简64位直装版下载

go导入自建包

Mathematical knowledge: finding combinatorial number III - finding combinatorial number

求两个线段公共部分的长度

修复表中的名字(首字符大写,其他小写)

A preliminary understanding of operator overloading

What is PMP?
![Pytorch —— 基础指北_贰 高中生都能看懂的[反向传播和梯度下降]](/img/6e/279dbb7a8d7a5ecd240de464c5b8b2.png)
Pytorch —— 基础指北_贰 高中生都能看懂的[反向传播和梯度下降]

In the fourth week of June, the list - flying melon data up main growth ranking list (BiliBili platform) was released!

FL studio20.9 fruit software advanced Chinese edition electronic music arrangement
随机推荐
How to add a condition for an associated table in an SQL statement [null value required or not required]
How to learn and read code
[fundamentals of wireless communication-14]: illustrated mobile communication technology and application development-2-the first generation mobile analog communication big brother
Analysis on user behavior loss of data exploration e-commerce platform
Pytorch —— 基礎指北_貳 高中生都能看懂的[反向傳播和梯度下降]
What is PMP?
Open source basic software companies, looking for you to create the future together (api7.ai)
The whole process of AS400 API from zero to one
求两个线段公共部分的长度
开源基础软件公司,寻找一起创造未来的你(API7.ai)
小程序云开发之--微信公众号文章采集篇
SQL语句关联表 如何添加关联表的条件 [需要null值或不需要null值]
十大劵商如何开户?还有,在线开户安全么?
PMP是什么?
(summary I) Halcon Foundation's target finding features + becoming a regular
QML控件类型:ToolTip
(翻译)使用眉状文本提高标题点击率
What is project management?
Short video platform development, relying on drawerlayout to achieve side sliding menu effect
AS400 large factory interview