当前位置:网站首页>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创建角色 添加权限



第三步克隆服务列表

第四步点击测试 启动成功

边栏推荐
- AS400 大廠面試
- There is no future to be expected. It is just the last fantasy of a migrant worker before he dies
- House change for agricultural products? "Disguised" house purchase subsidy!
- [punch in questions] integrated daily 5 questions sharing (phase I)
- (translation) use eyebrow shaped text to improve Title click through rate
- How do the top ten securities firms open accounts? Also, is it safe to open an account online?
- 我的PMP学习考试心得
- Delete duplicate email
- SWT / anr problem - SWT caused by too long dump time
- AS400 entretien d'usine
猜你喜欢

SWT / anr problem - storagemanagerservice stuck

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

Video tutorial | Chang'an chain launched a series of video tutorial collections (Introduction)

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

【2022年】江西省研究生数学建模方案、代码
2022年最新csdn涨薪技术栈-app自动化测试概述

(翻译)使用眉状文本提高标题点击率

Analysis on user behavior loss of data exploration e-commerce platform

Rocketqa: cross batch negatives, de noised hard negative sampling and data augmentation

最新微信ipad协议 CODE获取 公众号授权等
随机推荐
零基础自学SQL课程 | 窗口函数
AS400 大廠面試
【2022年】江西省研究生数学建模方案、代码
FL Studio20.9水果软件高级中文版电音编曲
The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer
What is PMP?
Objects and object variables
LabVIEW计算相机图像传感器分辨率以及镜头焦距
[proteus simulation] Arduino UNO +74c922 keyboard decoding drive 4x4 matrix keyboard
工厂+策略模式
SQL语句关联表 如何添加关联表的条件 [需要null值或不需要null值]
【agora】用户管理
Pytorch - - Basic Reference North Deux élèves du secondaire peuvent comprendre [Rétropropagation et Gradient descendant]
机器学习10-信念贝叶斯分类器
SWT / anr problem - storagemanagerservice stuck
运算符重载的初识
Clickhouse 消除由group by产生的间隙
Template: globally balanced binary tree
centos 安装多个版本的php并切换
软件开发中的上游和下游