
引入包体
`
    
    
        
        
        
    
    
    
    
    
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    
    
        
        
            
            
            
        
        
        
            
            
            
        
        
        
            
            
            
        
    
`
配置类
`/**
 * All rights Reserved, Designed By 林溪
 * Copyright:    Copyright(C) 2016-2020
 * Company       溪云阁 .
 */  
package com.boots.register.common.config;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;  
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;  
/**
 * Nacos配置
 * @author:溪云阁
 * @date:2020年6月12日
 */
@Configuration
public class NacosConfig {  
    @Value("${server.port}")
    private int serverPort;  
    @Value("${spring.application.name}")
    private String applicationName;  
    @Value("${nacos.host}")
    private String nacosHost;  
    @NacosInjected
    private NamingService namingService;  
    @PostConstruct
    public void registerInstance() throws NacosException {
        namingService.registerInstance(applicationName, nacosHost, serverPort);
    }  
}
`
属性文件配置
######配置基本信息######   ##配置应用端口号   server.port: 8080   ##配置应用名称   spring.application.name: boots-register   ##配置时间格式,为了避免精度丢失,全部换成字符串   spring.jackson.timeZone: GMT+8   spring.jackson.dateFormat: yyyy-MM-dd HH:mm:ss   spring.jackson.generator.writeNumbersAsStrings: true   ##注册及配置中心地址   nacos.host: 127.0.0.1   nacos.port: 8848   nacos.config.serverAddr: ${nacos.host}:${nacos.port}   nacos.discovery.serverAddr: ${nacos.host}:${nacos.port}   
启动类配置
`package com.boots.register;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;  
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
/**
 * 服务启动类
 * @author:溪云阁
 * @date:2020年5月2日
 */
@SpringBootApplication
@ComponentScan(basePackages = { "com.module", "com.boots" })
@NacosPropertySource(dataId = "DATA_BOOTS_ID", groupId="GROUP_BOOTS_ID", autoRefreshed = true)
public class BootsRegisterApplication {  
    public static void main(String[] args) {
        SpringApplication.run(BootsRegisterApplication.class, args);
    }  
}
`
接口编写
`/**
 * All rights Reserved, Designed By 林溪
 * Copyright:    Copyright(C) 2016-2020
 * Company       溪云阁 .
 */  
package com.boots.register.view.register.view;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;  
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.module.boots.api.message.ResponseMsg;
import com.module.boots.api.utils.MsgUtils;
import com.module.boots.exception.CommonRuntimeException;  
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;  
/**
 * Nacos控制器
 * @author:溪云阁
 * @date:2020年6月12日
 */
@Api(tags = { "web服务:查询注册中心上的信息" })
@RestController
@RequestMapping("view/minio")
public class NacosView {  
    @NacosInjected
    private NamingService namingService;  
    @NacosValue(value = "${project.name:null}", autoRefreshed = true)
    private String projectName;  
    /**
     * 获取项目名称
     * @author 溪云阁
     * @return ResponseMsg
     */
    @ApiOperation(value = "获取项目名称")
    @GetMapping(value = "/getProjectName")
    @SneakyThrows(CommonRuntimeException.class)
    public ResponseMsg
        return MsgUtils.buildSuccessMsg(projectName);
    }  
    /**
     * 查询所有服务
     * @author 溪云阁
     * @param serviceName
     * @return ResponseMsg<List
     */
    @ApiOperation(value = "查询所有服务")
    @GetMapping(value = "/getAllServices")
    @SneakyThrows({ CommonRuntimeException.class, NacosException.class })
    public ResponseMsg<List
        return MsgUtils.buildSuccessMsg(namingService.getAllInstances(serviceName));
    }  
}
`
测试

 此时如果你在Nacos上更改配置上面的属性,发布后重新获取数据,也会跟着变更。
此时如果你在Nacos上更改配置上面的属性,发布后重新获取数据,也会跟着变更。
拓展
1、生产中,要把做高可用

- SpringBoot:掌握这两个属性,你的测试类可以启动的更快些 - 好文章,我在看 
===============
本文分享自微信公众号 - 程序员闪充宝(cxyscb1024)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
 
  
  
  
 
 
  
 
 
 