微服务引擎MSE

将springboot类型的微服务集成Nacos

2025-05-27 03:14:13
配置管理

为您的 Spring Boot 应用集成Nacos 配置管理功能,在工程中引入nacos-config-spring-boot-starter和nacos-discovery-spring-boot-starter的 Maven坐标。注意:版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本。

<dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>nacos-config-spring-boot-starter</artifactId>
        <version>${latest.version}</version>
  </dependency>

在 application.properties 中配置 实例配置中心的连接地址,即前述步骤的内网地址。

nacos.config.server-addr=127.0.0.1:8848

配置管理示例:使用 @NacosPropertySource 加载 Data ID为 example 的配置源,并开启自动更新,通过 Nacos 的 @NacosValue 注解设置属性值。

@Controller
  @RequestMapping("discovery")
  public class DiscoveryController {
 
        @NacosInjected
      private   NamingService namingService;
 
        @RequestMapping(value = "/get", method = GET)
        @ResponseBody
      public   List<Instance> 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);
      }
  }

启动 NacosConfigApplication,调用 curl http://localhost:8080/config/get,返回内容是 false。通过调用 Nacos Open API 向 Nacos server 发布配置:Data ID为example,内容为useLocalCache=true

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

再次访问 http://localhost:8080/config/get,此时返回内容为true,说明程序中的useLocalCache值已经被动态更新了。

服务发现

如果需要在 Spring Boot 应用集成Nacos 服务发现和管理功能,在工程中引入nacos-discovery-spring-boot-starter的 Maven坐标。注意:版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本。

<dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>nacos-discovery-spring-boot-starter</artifactId>
        <version>${latest.version}</version>
  </dependency>

在 application.properties 中配置 实例注册中心的连接地址,即前述步骤的内网地址。

nacos.discovery.server-addr=127.0.0.1:8848

服务发现示例:使用 @NacosInjected 注入 Nacos 的 NamingService 实例

@Controller
  @RequestMapping("discovery")
  public class DiscoveryController {
 
        @NacosInjected
      private   NamingService namingService;
 
        @RequestMapping(value = "/get", method = GET)
        @ResponseBody
      public   List<Instance> 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?serviceName=example,此时返回为空 JSON 数组[]。

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

curl -X POST   'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=example&ip=127.0.0.1&port=8080'

再次访问 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": {}
    }
  ]


pC8JyheysuUx