Nacos Spring 快速开始

 2023-09-06 阅读 30 评论 0

摘要:Nacos Spring 快速开始 本文主要面向 Spring 的使用者,通过两个示例来介绍如何使用 Nacos 来实现分布式环境下的配置管理和服务发现。 关于 Nacos Spring 的详细文档请参看:nacos-spring-project。 通过 Nacos server 和 Nacos Spring 配置管理模块,实现

Nacos Spring 快速开始

本文主要面向 Spring 的使用者,通过两个示例来介绍如何使用 Nacos 来实现分布式环境下的配置管理和服务发现。

关于 Nacos Spring 的详细文档请参看:nacos-spring-project。

  • 通过 Nacos server 和 Nacos Spring 配置管理模块,实现配置的动态变更;
  • 通过 Nacos server 和 Nacos Spring 服务发现模块,实现服务的注册与发现。

前提条件

您需要先下载 Nacos 并启动 Nacos server。操作步骤参见 Nacos 快速入门。

启动配置管理

启动了 Nacos server 后,您就可以参考以下示例代码,为您的 Spring 应用启动 Nacos 配置管理服务了。完整示例代码请参考:nacos-spring-config-example

  1. 添加依赖。
<dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-spring-context</artifactId><version>${latest.version}</version>
</dependency>

最新版本可以在 maven 仓库,如 "mvnrepository.com" 中获取。

  1. 添加 @EnableNacosConfig 注解启用 Nacos Spring 的配置管理服务。以下示例中,我们使用 @NacosPropertySource 加载了 dataId 为 example 的配置源,并开启自动更新:
@Configuration
@EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848"))
@NacosPropertySource(dataId = "example", autoRefreshed = true)
public class NacosConfiguration {}
  1. 通过 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;}
}
  1. 启动 Tomcat,调用 curl http://localhost:8080/config/get尝试获取配置信息。由于此时还未发布过配置,所以返回内容是 false

  2. 通过调用 Nacos Open API 向 Nacos Server 发布配置:dataId 为example,内容为useLocalCache=true

curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example&group=DEFAULT_GROUP&content=useLocalCache=true"
  1. 再次访问 http://localhost:8080/config/get,此时返回内容为true,说明程序中的useLocalCache值已经被动态更新了。

启动服务发现

本节演示如何在您的 Spring 项目中启动 Nacos 的服务发现功能。完整示例代码请参考:nacos-spring-discovery-example

  1. 添加依赖。
<dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-spring-context</artifactId><version>${latest.version}</version>
</dependency>

最新版本可以在 maven 仓库,如 "mvnrepository.com" 中获取。

  1. 通过添加 @EnableNacosDiscovery 注解开启 Nacos Spring 的服务发现功能:
@Configuration
@EnableNacosDiscovery(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848"))
public class NacosConfiguration {}
  1. 使用 @NacosInjected 注入 Nacos 的 NamingService 实例:
@Controller
@RequestMapping("discovery")
public class DiscoveryController {@NacosInjectedprivate NamingService namingService;@RequestMapping(value = "/get", method = GET)@ResponseBodypublic List<Instance> get(@RequestParam String serviceName) throws NacosException {return namingService.getAllInstances(serviceName);}
}
  1. 启动 Tomcat,调用 curl http://localhost:8080/discovery/get?serviceName=example,此时返回为空 JSON 数组[]

  2. 通过调用 Nacos Open API 向 Nacos server 注册一个名称为 example 服务。

curl -X PUT 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=example&ip=127.0.0.1&port=8080'
  1. 再次访问 curl http://localhost:8080/discovery/get?serviceName=example,此时返回内容为:
[{"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": {}}
]

相关项目

  • Nacos
  • Nacos Spring
  • Nacos Spring Boot
  • Spring Cloud Alibaba

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/3773.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息