springboot2.X手册:Eureka不更,Consul被禁,启用Nacos
程序员闪充宝
共 4795字,需浏览 10分钟
·
2020-08-12 11:15
引入包体
"1.0"?>
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4.0.0
com.boots
boots
1.1.0.RELEASE
boots-register
boots-register
http://maven.apache.org
UTF-8
com.boots
module-boots-api
2.0.0.RELEASE
com.alibaba.boot
nacos-discovery-spring-boot-starter
0.2.7
com.alibaba.boot
nacos-config-spring-boot-starter
0.2.7
配置类
/**
* 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 getProjectName() {
return MsgUtils.buildSuccessMsg(projectName);
}
/**
* 查询所有服务
* @author 溪云阁
* @param serviceName
* @return ResponseMsg>
*/
@ApiOperation(value = "查询所有服务")
@GetMapping(value = "/getAllServices")
@SneakyThrows({ CommonRuntimeException.class, NacosException.class })
public ResponseMsg> getAllServices(@RequestParam("serviceName") String serviceName) {
return MsgUtils.buildSuccessMsg(namingService.getAllInstances(serviceName));
}
}
测试
此时如果你在Nacos上更改配置上面的属性,发布后重新获取数据,也会跟着变更。
拓展
1、生产中,要把做高可用
好文章,我在看
好文章,我在看
评论