如何在springboot中配置nacos
导读:本文共1941字符,通常情况下阅读需要6分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 1.安装信息导入nacos-mysql.sql启动nacos 控制台2.配置发布与获取1)配置发布2种方式:调用openapi;控制台操作2)新建命名空间3)不同namespace,不同dataId,不同groupId获取配置情况@NacosPropertySource中指定dataId,groupId//加载dataId为example的配置源,并开启自动更新... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。1.安装信息
导入nacos-mysql.sql
启动nacos 控制台
2.配置发布与获取
1)配置发布2种方式:调用openapi;控制台操作
2)新建命名空间
3)不同namespace,不同dataId,不同groupId获取配置情况
@NacosPropertySource中指定dataId,groupId
//加载dataId为example的配置源,并开启自动更新//@NacosPropertySource(dataId="example",autoRefreshed=true)//加载不同groupId的配置//@NacosPropertySource(dataId="example",groupId="test",autoRefreshed=true)//加载不同dataId的配置@NacosPropertySource(dataId="example2",autoRefreshed=true)
配置文件中指定namespace 的ID
#根据命名空间ID,加载不同namespace的配置#nacos.config.namespace=a8128f5b-7f87-42c5-9ffd-c4f17da7ee34
测试:
3.服务注册与消费服务
1)服务注册
配置文件指定:
server.port=8080#访问的根路径server.context-path=/springboot-nacos#Nacos服务名spring.application.name=UserService#nacos服务端的地址nacos.config.server-addr=127.0.0.1:8848#discovery地址nacos.discovery.server-addr=127.0.0.1:8848#根据命名空间ID,加载不同namespace的配置#nacos.config.namespace=a8128f5b-7f87-42c5-9ffd-c4f17da7ee34
服务启动时注册:
@SpringBootApplicationpublicclassSpringbootNacosApplication{@NacosInjectedprivateNamingServicenamingService;@Value("${server.port}")privateintserverPort;@Value("${spring.application.name}")privateStringapplicationName;//curl-XPUT'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=xxx&ip=127.0.0.1&port=8080'@PostConstructpublicvoidregisterInstance()throwsNacosException{if(CollectionUtils.isEmpty(namingService.getAllInstances(applicationName))){namingService.registerInstance(applicationName,"127.0.0.1",serverPort);}}publicstaticvoidmain(String[]args){SpringApplication.run(SpringbootNacosApplication.class,args);}}
2)服务消费
通过NamingService发现选择服务
Instanceinstance=namingService.selectOneHealthyInstance("UserService");
通过RestTemplate调用获取结果
springboot是什么
springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:
如何在springboot中配置nacos的详细内容,希望对您有所帮助,信息来源于网络。