• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Nacos学习笔记 5Nacos整合SpringBoot流程

武飞扬头像
鮀城小帅
帮助3

前提,先下载Nacos并启动 Nacos Server。

1. Nacos 融合 Spring Boot 为注册配置中心

实现:

  • 通过 Nacos Server 和 nacos-config-spring-boot-starter 实现配置的动态变更;
  • 通过 Nacos Server 和 nacos-discovery-spring-boot-starter 实现服务的注册与发现。

1.1 启动配置管理

(1)创建SpringBoot项目

学新通

学新通

 学新通

 (2)添加依赖

  1.  
    <!-- https://mvnrepository.com/artifact/com.alibaba.boot/nacos-config-spring-boot-starter -->
  2.  
    <dependency>
  3.  
    <groupId>com.alibaba.boot</groupId>
  4.  
    <artifactId>nacos-config-spring-boot-starter</artifactId>
  5.  
    <version>0.2.10</version>
  6.  
    </dependency>

版本说明SpringBoot的2.x.x 版本对应的是 nacos-config的 0.2.10 版本,低于  0.2.10 版本会启动失败。

 (3)在application.properties 中配置 Nacos server 的地址:

nacos.config.server-addr=127.0.0.1:8848

(4)使用 @NacosPropertySource 加载 dataIdnacos-springboot 的配置源,并开启自动更新:

  1.  
    @SpringBootApplication
  2.  
    @NacosPropertySource(dataId = "nacos-springboot", autoRefreshed = true)
  3.  
    public class NacosSpringBootApplication {
  4.  
     
  5.  
    public static void main(String[] args) {
  6.  
    SpringApplication.run(NacosSpringBootApplication.class, args);
  7.  
    }
  8.  
    }

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

  1.  
    package com.example.controller;
  2.  
     
  3.  
    import com.alibaba.nacos.api.config.annotation.NacosValue;
  4.  
    import org.springframework.stereotype.Controller;
  5.  
    import org.springframework.web.bind.annotation.RequestMapping;
  6.  
    import org.springframework.web.bind.annotation.ResponseBody;
  7.  
     
  8.  
    import static org.springframework.web.bind.annotation.RequestMethod.GET;
  9.  
     
  10.  
    /**
  11.  
    * @author wushaopei
  12.  
    * @create 2022-12-19 15:34
  13.  
    */
  14.  
    @RestController
  15.  
    @RequestMapping("/config")
  16.  
    public class ConfigController {
  17.  
     
  18.  
    @NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
  19.  
    private boolean useLocalCache;
  20.  
     
  21.  
    @RequestMapping(value = "/get", method = GET)
  22.  
    @ResponseBody
  23.  
    public boolean get() {
  24.  
    return useLocalCache;
  25.  
    }
  26.  
    }

(6)启动 NacosConfigApplication,调用 curl http://localhost:8080/config/get,返回内容是 false

(7)通过调用 Nacos Open API 向 Nacos server 发布配置:dataId 为 nacos-springboot,内容为useLocalCache=true

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

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

学新通

学新通

 1.2 启动服务发现

(1)添加依赖

  1.  
    <dependency>
  2.  
    <groupId>com.alibaba.boot</groupId>
  3.  
    <artifactId>nacos-discovery-spring-boot-starter</artifactId>
  4.  
    <version>0.2.7</version>
  5.  
    </dependency>

注意:版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本。

(2)在 application.properties 中配置 Nacos server 的地址:

nacos.discovery.server-addr=127.0.0.1:9898

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

  1.  
    /**
  2.  
    * @author wushaopei
  3.  
    * @create 2022-12-16 11:12
  4.  
    */
  5.  
    @Controller
  6.  
    @RequestMapping("discovery")
  7.  
    public class DiscoveryController {
  8.  
     
  9.  
    @NacosInjected
  10.  
    private NamingService namingService;
  11.  
     
  12.  
    @RequestMapping(value = "/get", method = GET)
  13.  
    @ResponseBody
  14.  
    public List<Instance> get(@RequestParam String serviceName) throws NacosException {
  15.  
    return namingService.getAllInstances(serviceName);
  16.  
    }
  17.  
    }
  18.  
     
  19.  
    @SpringBootApplication
  20.  
    public class NacosDiscoveryApplication {
  21.  
     
  22.  
    public static void main(String[] args) {
  23.  
    SpringApplication.run(NacosDiscoveryApplication.class, args);
  24.  
    }
  25.  
    }

(4)启动 NacosDiscoveryApplication,调用 curl http://localhost:8080/discovery/get?serviceName=example,此时返回为空 JSON 数组[]

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

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

这里的ip、port是注册中心服务所在的服务器ip、port。

(6)再次访问 curl http://localhost:9898/discovery/get?serviceName=example,此时返回内容为:

  1.  
    [
  2.  
    {
  3.  
    "instanceId": "127.0.0.1#8080#DEFAULT#DEFAULT_GROUP@@nacos-springboot",
  4.  
    "ip": "127.0.0.1",
  5.  
    "port": 8080,
  6.  
    "weight": 1,
  7.  
    "healthy": true,
  8.  
    "enabled": true,
  9.  
    "ephemeral": true,
  10.  
    "clusterName": "DEFAULT",
  11.  
    "serviceName": "DEFAULT_GROUP@@nacos-springboot",
  12.  
    "metadata": { },
  13.  
    "instanceHeartBeatInterval": 5000,
  14.  
    "ipDeleteTimeout": 30000,
  15.  
    "instanceIdGenerator": "simple",
  16.  
    "instanceHeartBeatTimeOut": 15000
  17.  
    }
  18.  
    ]

(7)springboot整合nacos的详细配置

  1.  
    nacos:
  2.  
    config:
  3.  
    bootstrap:
  4.  
    # 开启预加载配置
  5.  
    enable: true
  6.  
    # nacos的ip地址和端口
  7.  
    server-addr: 192.168.1.2:8848
  8.  
    # nacos登录用户名
  9.  
    username: nacos
  10.  
    # nacos登录密码
  11.  
    password: nacos
  12.  
    # nacos命名空间id为 dev
  13.  
    namespace: dev
  14.  
    # 创建的配置的data-id,多个配置使用”,“隔开
  15.  
    data-ids: maple-admin, maple-admin-mysql
  16.  
    # 创建的配置的group
  17.  
    group: DEFAULT_GROUP
  18.  
    # 配置文件的后缀名
  19.  
    type: YAML
  20.  
    # 自动刷新配置
  21.  
    auto-refresh: true
  22.  
    # 长轮询的重试次数
  23.  
    max-retry: 10
  24.  
    # 重试间隔时间
  25.  
    config-retry-time: 2000
  26.  
    # 长轮询的超时时间
  27.  
    config-long-poll-timeout: 46000

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfckbfe
系列文章
更多 icon
同类精品
更多 icon
继续加载