网站 app 哪个先做,网站制作教程一般地建网络,绿园区建设局网站,小型网站建设价格转载自 Nacos Spring Boot 快速开始
本文主要面向 Spring Boot 的使用者#xff0c;通过两个示例来介绍如何使用 Nacos 来实现分布式环境下的配置管理和服务发现。
关于 Nacos Spring Boot 的详细文档请参看#xff1a;nacos-spring-boot-project。
通过 Nacos Server 和…转载自 Nacos Spring Boot 快速开始
本文主要面向 Spring Boot 的使用者通过两个示例来介绍如何使用 Nacos 来实现分布式环境下的配置管理和服务发现。
关于 Nacos Spring Boot 的详细文档请参看nacos-spring-boot-project。
通过 Nacos Server 和 nacos-config-spring-boot-starter 实现配置的动态变更通过 Nacos Server 和 nacos-discovery-spring-boot-starter 实现服务的注册与发现。
前提条件
您需要先下载 Nacos 并启动 Nacos server。操作步骤参见 Nacos 快速入门。
启动配置管理
启动了 Nacos server 后您就可以参考以下示例代码为您的 Spring Boot 应用启动 Nacos 配置管理服务了。完整示例代码请参考nacos-spring-boot-config-example
添加依赖。
dependencygroupIdcom.alibaba.boot/groupIdartifactIdnacos-config-spring-boot-starter/artifactIdversion${latest.version}/version
/dependency注意版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本。
在 application.properties 中配置 Nacos server 的地址
nacos.config.server-addr127.0.0.1:8848使用 NacosPropertySource 加载 dataId 为 example 的配置源并开启自动更新
SpringBootApplication
NacosPropertySource(dataId example, autoRefreshed true)
public class NacosConfigApplication {public static void main(String[] args) {SpringApplication.run(NacosConfigApplication.class, args);}
}通过 Nacos 的 NacosValue 注解设置属性值。
Controller
RequestMapping(config)
public class ConfigController {NacosValue(value ${useLocalCache:false}, autoRefreshed true)private boolean useLocalCache;RequestMapping(value /get, method GET)ResponseBodypublic boolean get() {return useLocalCache;}
}启动 NacosConfigApplication调用 curl http://localhost:8080/config/get返回内容是 false。 通过调用 Nacos Open API 向 Nacos server 发布配置dataId 为example内容为useLocalCachetrue
curl -X POST http://127.0.0.1:8848/nacos/v1/cs/configs?dataIdexamplegroupDEFAULT_GROUPcontentuseLocalCachetrue再次访问 http://localhost:8080/config/get此时返回内容为true说明程序中的useLocalCache值已经被动态更新了。
启动服务发现
本节演示如何在您的 Spring Boot 项目中启动 Nacos 的服务发现功能。完整示例代码请参考nacos-spring-boot-discovery-example
添加依赖。
dependencygroupIdcom.alibaba.boot/groupIdartifactIdnacos-discovery-spring-boot-starter/artifactIdversion${latest.version}/version
/dependency注意版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本。
在 application.properties 中配置 Nacos server 的地址
nacos.discovery.server-addr127.0.0.1:8848使用 NacosInjected 注入 Nacos 的 NamingService 实例
Controller
RequestMapping(discovery)
public class DiscoveryController {NacosInjectedprivate NamingService namingService;RequestMapping(value /get, method GET)ResponseBodypublic ListInstance get(RequestParam String serviceName) throws NacosException {return namingService.getAllInstances(serviceName);}
}SpringBootApplication
public class NacosDiscoveryApplication {public static void main(String[] args) {SpringApplication.run(NacosDiscoveryApplication.class, args);}
}启动 NacosDiscoveryApplication调用 curl http://localhost:8080/discovery/get?serviceNameexample此时返回为空 JSON 数组[]。 通过调用 Nacos Open API 向 Nacos server 注册一个名称为 example 服务
curl -X PUT http://127.0.0.1:8848/nacos/v1/ns/instance?serviceNameexampleip127.0.0.1port8080再次访问 curl http://localhost:8080/discovery/get?serviceNameexample此时返回内容为
[{instanceId: 127.0.0.1-8080-DEFAULT-example,ip: 127.0.0.1,port: 8080,weight: 1.0,healthy: true,cluster: {serviceName: null,name: ,healthChecker: {type: TCP},defaultPort: 80,defaultCheckPort: 80,useIPPort4Check: true,metadata: {}},service: null,metadata: {}}
]相关项目
NacosNacos SpringNacos Spring BootSpring Cloud Alibaba