Nacos Spring Cloud 快速開始
本文主要面向?Spring Cloud?的使用者壤短,通過兩個示例來介紹如何使用 Nacos 來實現(xiàn)分布式環(huán)境下的配置管理和服務(wù)注冊發(fā)現(xiàn)。
關(guān)于 Nacos Spring Cloud 的詳細(xì)文檔請參看:Nacos Config?和?Nacos Discovery缔恳。
通過 Nacos Server 和 spring-cloud-starter-alibaba-nacos-config 實現(xiàn)配置的動態(tài)變更。
通過 Nacos Server 和 spring-cloud-starter-alibaba-nacos-discovery 實現(xiàn)服務(wù)的注冊與發(fā)現(xiàn)回论。
前提條件
您需要先下載 Nacos 并啟動 Nacos server蹦漠。操作步驟參見?Nacos 快速入門
啟動配置管理
啟動了 Nacos server 后,您就可以參考以下示例代碼炮叶,為您的 Spring Cloud 應(yīng)用啟動 Nacos 配置管理服務(wù)了。完整示例代碼請參考:nacos-spring-cloud-config-example
添加依賴:
<dependency>
? ? <groupId>com.alibaba.cloud</groupId>
? ? <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
? ? <version>${latest.version}</version>
</dependency>
注意:版本?2.1.x.RELEASE?對應(yīng)的是 Spring Boot 2.1.x 版本渡处。版本?2.0.x.RELEASE?對應(yīng)的是 Spring Boot 2.0.x 版本镜悉,版本?1.5.x.RELEASE?對應(yīng)的是 Spring Boot 1.5.x 版本。
更多版本對應(yīng)關(guān)系參考:版本說明 Wiki
在?bootstrap.properties?中配置 Nacos server 的地址和應(yīng)用名
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.application.name=example
說明:之所以需要配置?spring.application.name?医瘫,是因為它是構(gòu)成 Nacos 配置管理?dataId字段的一部分侣肄。
在 Nacos Spring Cloud 中,dataId?的完整格式如下:
${prefix}-${spring.profile.active}.${file-extension}
prefix默認(rèn)為spring.application.name的值醇份,也可以通過配置項spring.cloud.nacos.config.prefix來配置稼锅。
spring.profile.active即為當(dāng)前環(huán)境對應(yīng)的 profile吼具,詳情可以參考Spring Boot文檔。?注意:當(dāng)?spring.profile.active?為空時矩距,對應(yīng)的連接符-也將不存在拗盒,dataId 的拼接格式變成${prefix}.${file-extension}
file-exetension為配置內(nèi)容的數(shù)據(jù)格式,可以通過配置項spring.cloud.nacos.config.file-extension?來配置锥债。目前只支持properties?和?yaml類型陡蝇。
通過 Spring Cloud 原生注解?@RefreshScope?實現(xiàn)配置自動更新:
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
? ? @Value("${useLocalCache:false}")
? ? private boolean useLocalCache;
? ? @RequestMapping("/get")
? ? public boolean get() {
? ? ? ? return useLocalCache;
? ? }
}
首先通過調(diào)用?Nacos Open API?向 Nacos Server 發(fā)布配置:dataId 為example.properties,內(nèi)容為useLocalCache=true
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example.properties&group=DEFAULT_GROUP&content=useLocalCache=true"
運行?NacosConfigApplication哮肚,調(diào)用?curl http://localhost:8080/config/get登夫,返回內(nèi)容是?true。
再次調(diào)用?Nacos Open API?向 Nacos server 發(fā)布配置:dataId 為example.properties允趟,內(nèi)容為useLocalCache=false
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example.properties&group=DEFAULT_GROUP&content=useLocalCache=false"
再次訪問?http://localhost:8080/config/get恼策,此時返回內(nèi)容為false,說明程序中的useLocalCache值已經(jīng)被動態(tài)更新了潮剪。
啟動服務(wù)發(fā)現(xiàn)
本節(jié)通過實現(xiàn)一個簡單的?echo service?演示如何在您的 Spring Cloud 項目中啟用 Nacos 的服務(wù)發(fā)現(xiàn)功能涣楷,如下圖示:
完整示例代碼請參考:nacos-spring-cloud-discovery-example
添加依賴:
<dependency>
? ? <groupId>com.alibaba.cloud</groupId>
? ? <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
? ? <version>${latest.version}</version>
</dependency>
注意:版本?2.1.x.RELEASE?對應(yīng)的是 Spring Boot 2.1.x 版本。版本?2.0.x.RELEASE?對應(yīng)的是 Spring Boot 2.0.x 版本抗碰,版本?1.5.x.RELEASE?對應(yīng)的是 Spring Boot 1.5.x 版本狮斗。
更多版本對應(yīng)關(guān)系參考:版本說明 Wiki
配置服務(wù)提供者,從而服務(wù)提供者可以通過 Nacos 的服務(wù)注冊發(fā)現(xiàn)功能將其服務(wù)注冊到 Nacos server 上改含。
i. 在?application.properties?中配置 Nacos server 的地址:
server.port=8070
spring.application.name=service-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
ii. 通過 Spring Cloud 原生注解?@EnableDiscoveryClient?開啟服務(wù)注冊發(fā)現(xiàn)功能:
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
}
@RestController
class EchoController {
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
public String echo(@PathVariable String string) {
return "Hello Nacos Discovery " + string;
}
}
}
配置服務(wù)消費者情龄,從而服務(wù)消費者可以通過 Nacos 的服務(wù)注冊發(fā)現(xiàn)功能從 Nacos server 上獲取到它要調(diào)用的服務(wù)迄汛。
i. 在?application.properties?中配置 Nacos server 的地址:
server.port=8080
spring.application.name=service-consumer
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
ii. 通過 Spring Cloud 原生注解?@EnableDiscoveryClient?開啟服務(wù)注冊發(fā)現(xiàn)功能捍壤。給?RestTemplate?實例添加@LoadBalanced?注解,開啟?@LoadBalanced?與?Ribbon?的集成:
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {
? ? @LoadBalanced
? ? @Bean
? ? public RestTemplate restTemplate() {
? ? ? ? return new RestTemplate();
? ? }
? ? public static void main(String[] args) {
? ? ? ? SpringApplication.run(NacosConsumerApplication.class, args);
? ? }
? ? @RestController
? ? public class TestController {
? ? ? ? private final RestTemplate restTemplate;
? ? ? ? @Autowired
? ? ? ? public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}
? ? ? ? @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
? ? ? ? public String echo(@PathVariable String str) {
? ? ? ? ? ? return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
? ? ? ? }
? ? }
}
啟動?ProviderApplication?和?ConsumerApplication?鞍爱,調(diào)用?http://localhost:8080/echo/2018鹃觉,返回內(nèi)容為?Hello Nacos Discovery 2018。