引入包體
<?xml version="1.0"?>
<project xsi:schemaLocation="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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.boots</groupId>
<artifactId>boots</artifactId>
<version>1.1.0.RELEASE</version>
</parent>
<artifactId>boots-register</artifactId>
<name>boots-register</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 公共組件:swagger服務+入參出參+統一異常攔截 -->
<dependency>
<groupId>com.boots</groupId>
<artifactId>module-boots-api</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!-- nacos服務註冊包 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>0.2.7</version>
</dependency>
<!-- nacos配置包 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.7</version>
</dependency>
</dependencies>
</project>
配置類
/**
* 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<String>
*/
@ApiOperation(value = "獲取項目名稱")
@GetMapping(value = "/getProjectName")
@SneakyThrows(CommonRuntimeException.class)
public ResponseMsg<String> getProjectName() {
return MsgUtils.buildSuccessMsg(projectName);
}
/**
* 查詢全部服務
* @author 溪雲閣
* @param serviceName
* @return ResponseMsg<List<Instance>>
*/
@ApiOperation(value = "查詢全部服務")
@GetMapping(value = "/getAllServices")
@SneakyThrows({ CommonRuntimeException.class, NacosException.class })
public ResponseMsg<List<Instance>> getAllServices(@RequestParam("serviceName") String serviceName) {
return MsgUtils.buildSuccessMsg(namingService.getAllInstances(serviceName));
}
}
測試
此時若是你在Nacos上更改配置上面的屬性,發佈後從新獲取數據,也會跟着變動。java
拓展
一、生產中,要把作高可用程序員
好文章,我在看springboot
好文章,我在看springboot
本文分享自微信公衆號 - 程序員閃充寶(cxyscb1024)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。微信