栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot整合Nacos2.0配置管理及服务发现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot整合Nacos2.0配置管理及服务发现

1、下载 Nacos 并启动 Nacos server
见 Nacos的安装使用
2、启动配置管理
(1)添加依赖


    com.alibaba.boot
    nacos-config-spring-boot-starter
    0.2.10

注意:版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本。本示例使用的是0.2.10版本,springboot要使用较低版本,否则会报错,本示例使用的是2.2.4.RELEASE版本。详见NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactorymetadata
(2)在 application.yml 中配置 Nacos server 的地址

nacos:
  config:
    server-addr: 127.0.0.1:8848

(3)使用 @NacosPropertySource 加载 dataId 为 nacostest 的配置源,并开启自动更新

@SpringBootApplication
@NacosPropertySource(dataId = "nacostest", autoRefreshed = true)
public class TengxunsmsApplication {

    public static void main(String[] args) {
        SpringApplication.run(TengxunsmsApplication.class, args);
    }
}

(4)通过 Nacos 的 @NacosValue 注解设置属性值

@Controller
@RequestMapping("config")
public class ConfigController {

    @NacosValue(value = "${userLocalCache:false}", autoRefreshed = true)
    private boolean userLocalCache;

    @RequestMapping(value = "/get", method = GET)
    @ResponseBody
    public boolean get() {
        return userLocalCache;
    }
}

(5)启动应用TengxunsmsApplication
(6)测试
1)cmd下执行

curl http://localhost:8080/config/get

返回false
2)cmd下调用 Nacos Open API 向 Nacos server 发布配置:dataId 为nacostest,内容为userLocalCache=true

curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacostest&group=DEFAULT_GROUP&content=userLocalCache=true"

再次执行

curl http://localhost:8080/config/get

返回true

3、启动服务发现
(1)添加依赖


    com.alibaba.boot
    nacos-discovery-spring-boot-starter
    0.2.10

注意:版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本。
(2)在 application.yml 中配置 Nacos server 的地址

nacos:
  discovery:
    server-addr: 127.0.0.1:8848

(3)使用 @NacosInjected 注入 Nacos 的 NamingService 实例

@Controller
@RequestMapping("discovery")
public class DiscoveryController {

    @NacosInjected
    private NamingService namingService;

    public List get(@RequestParam String serviceName) throws NacosException {
        return namingService.getAllInstances(serviceName);
    }
}

(4)启动应用TengxunsmsApplication
(5)测试
1)cmd下执行

curl http://localhost:8080/discovery/get?serviceName=nacos-test

返回为【】
2)调用 Nacos Open API 向 Nacos server 注册一个名称为 nacos-test 服务

curl -X POST "http://127.0.0.1:8848/nacos/v1/ns/instance?port=8080&healthy=true&ip=127.0.0.1&weight=1.0&serviceName=nacos-test&encoding=GBK"

再次执行

curl http://localhost:8080/discovery/get?serviceName=nacos-test

返回为

[{"instanceId":"127.0.0.1#8080#DEFAULT#DEFAULT_GROUP@@nacos-test","ip":"127.0.0.1","port":8080,"weight":1.0,"healthy":true,"enabled":true,"ephemeral":true,"clusterName":"DEFAULT","serviceName":"DEFAULT_GROUP@@nacos-test","metadata":{},"instanceHeartBeatInterval":5000,"instanceHeartBeatTimeOut":15000,"ipDeleteTimeout":30000,"instanceIdGenerator":"simple"}]
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/732129.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号