nacos–基础–4.1–集成–SpringBoot–配置管理、服务发现、服务注册
代码位置
https://gitee.com/DanShenGuiZu/learnDemo/tree/master/nacos-learn/nacos-spring-boot
 
1、介绍
- 主要面向 SpringBoot 的使用者
 - 通过2个实例,来介绍nacos和SpringBoot的集成 
  
- 配置管理
 - 服务注册与发现
 
 
2、配置管理
实现配置的动态变更
2.1、代码
2.1.1、结构

2.1.2、代码
依赖
<!--nacos 配置管理-->
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>nacos-config-spring-boot-starter</artifactId>
    <version>0.2.12</version>
</dependency>
 
注意:
- 版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本
 - 版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本。
 
配置管理
 
@Configuration
// 使用 @NacosPropertySource 加载 dataId 为 example 的配置源,并开启自动更新:
@NacosPropertySource(dataId = "example", autoRefreshed = true)
public class ConfigManagerConfig {
}
 
配置管理controller
/**
 *
 * 配置管理
 * 
 * @author <a href="920786312@qq.com">周飞</a>
 * @since 2022/10/17 10:34
 */
@Controller
@RequestMapping("config")
public class ConfigManagerController {
    
    // 使用 @NacosInjected 注入 Nacos 的 NamingService 实例:
    @NacosInjected
    private ConfigService configService;
    
    // 通过 Nacos 的 @NacosValue 注解设置属性值。
    @NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
    private boolean useLocalCache;
    
    @RequestMapping(value = "/get", method = GET)
    @ResponseBody
    public boolean get() {
        return useLocalCache;
    }
    
    /**
     * curl -X POST
     * 'http://localhost:8080/config?dataId=example&content=useLocalCache=true'
     */
    @RequestMapping(method = POST)
    @ResponseBody
    public ResponseEntity<String> publish(@RequestParam String dataId,
            @RequestParam(defaultValue = "DEFAULT_GROUP") String group, @RequestParam String content) {
        boolean result = false;
        try {
            result = configService.publishConfig(dataId, group, content);
        } catch (NacosException e) {
            return new ResponseEntity<String>("Publish Fail:" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        if (result) {
            return new ResponseEntity<String>("Publish Success", HttpStatus.OK);
        }
        return new ResponseEntity<String>("Publish Fail, Retry", HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
 
application.properties
# Nacos 服务配置 的地址
nacos.config.server-addr=192.168.187.171:8848
 
2.2、测试
2.2.1、获取配置信息
http://localhost:8080/config/get
 

2.2.2、设置配置信息
http://localhost:8080/config?dataId=example&content=useLocalCache=true
 


 
3、服务发现
实现服务的注册与发现
3.1、代码
3.1.1、结构

3.1.2、代码
依赖
<!--nacos 服务发现-->
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>nacos-discovery-spring-boot-starter</artifactId>
    <version>0.2.12</version>
</dependency>
 
服务注册和发现controller
/**
 *
 * 服务注册和发现
 * @author  <a href="920786312@qq.com">周飞</a>
 * @since 2022/10/17 10:34
 */
@Controller
@RequestMapping("discovery")
public class DiscoveryController {
    // 使用 @NacosInjected 注入 Nacos 的 NamingService 实例:
    @NacosInjected
    private NamingService namingService;
    // 通过服务名称查找服务
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public List<Instance> get(@RequestParam String serviceName) throws NacosException {
        return namingService.getAllInstances(serviceName);
    }
}
 
application.properties
# Nacos 服务发现 的地址
nacos.discovery.server-addr=192.168.187.171:8848
 
3.2、测试
3.2.1、注册服务
curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=example&ip=127.0.0.1&port=8080'
 

3.2.2、获取服务
http://localhost:8080/discovery/get?serviceName=example
 










![[附源码]Python计算机毕业设计电影售票管理系统Django(程序+LW)](https://img-blog.csdnimg.cn/525f6442f2654610828c0f3a2665c586.png)

![[附源码]Python计算机毕业设计SSM基于web的医院门诊管理系统(程序+LW)](https://img-blog.csdnimg.cn/ff3c9f20cbfa402a8cd00ae12634e033.png)







