Nacos Config 使用
- 啟動Nacos訪問地址為:http://101.200.201.195:8848/nacos/
準備作業完成后進行服務消費,
第一步:創建一個Maven工程buscien-service-04
第二步:編輯pom.xml,加入必要的依賴配置
<!-- 匯入bom依賴管理工程 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.opensource</groupId>
<artifactId>bom</artifactId>
<version>1.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 業務服務所需依賴 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
在bom工程中已經全部定義好了對應的版本所以我們在業務工程中就不需要定義依賴版本了,統一在bom中進行維護,
第三步:創建一個SpringBoot啟動類,并且增加一個Http服務
/**
* SpringCloud Alibaba Nacos配置中心使用Demo
*
* @author WuWenTao
* @version 1.0
* @date 2020/9/2 14:18
*/
@SpringBootApplication
@EnableDiscoveryClient
public class BuscienService04Appl {
public static void main(String[] args) {
SpringApplication.run(BuscienService04Appl.class, args);
}
}
@RestController
@RequestMapping("/config/")
class NacosConfigPropertyController{
@Value("${configinfo:defualt}")
private String configinfo;
@GetMapping("configinfo")
public String configinfo(){
return this.configinfo;
}
}
以上內容非常清晰,NacosConfigPropertyController中請求服務回傳一個configinfo欄位,該欄位源來于組態檔,如果沒有則默認為default,
第四步:撰寫bootstrap.properties
server.port=4000
spring.application.name=buscien-service-04-app
spring.cloud.nacos.discovery.server-addr=101.200.201.195:8848
spring.cloud.nacos.config.enabled=true
spring.cloud.nacos.config.server-addr=101.200.201.195:8848
spring.cloud.nacos.config.prefix=buscien-service-04-app
spring.cloud.nacos.config.file-extension=properties
spring.profiles.active=dev
第五步:登陸Nacos管理界面,在配置串列中新增dev,test,uat三個環境配置作為三個環境不同配置,

里面內容為configinfo=對應配置的DataId
第六步:啟動BuscienService04Appl,啟動完成,在Nacos界面中能夠看到注冊上去的服務,訪問BuscienService03Appl中的服務介面:
http://localhost:4000/config/configinfo
目前bootstrap.properties中的配置為:spring.profiles.active=dev
訪問服務介面頁面列印:buscien-service-04-app-dev.properties
將bootstrap.properties中的配置為:spring.profiles.active=uat
訪問服務介面頁面列印:buscien-service-04-app-uat.properties
這里是通過bootstrap.properties中的spring.profiles.active指定不同環境獲取不同的配置,
并且獲取組態檔的規則為:${spring.cloud.nacos.config.prefix}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
第六步:實作Nacos實時重繪配置,在NacosConfigPropertyController類上增加注解@RefreshScope代表這個類中的@Value屬性為實時重繪的,在類上加上注解后重啟服務,
首次訪問服務:http://localhost:4000/config/configinfo
訪問服務介面頁面列印:buscien-service-04-app-uat.properties
然后將Nacos頁面中的DataId為buscien-service-04-app-uat.properties中的configinfo修改為:
configinfo=配置實時重繪1
再次訪問服務:http://localhost:4000/config/configinfo
訪問服務介面頁面列印:配置實時重繪1
注:配置實時重繪并不是在Nacos中已修改就能夠生效的,有秒級延遲
原始碼地址:https://github.com/450255266/spring-cloud-alibaba
Nacos地址對外開放:http://101.200.201.195:8848/nacos/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/37663.html
標籤:Java
上一篇:64位應用程式怎么將32位dll注入到32位行程里面
下一篇:找師傅
