日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Nacos 使用

發(fā)布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Nacos 使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

環(huán)境準(zhǔn)備

  • 64 bit OS,支持 Linux/Unix/Mac/Windows,推薦選用 Linux/Unix/Mac。
  • 64 bit JDK 1.8+;下載?&?配置。
  • Maven 3.2.x+;下載?&?配置。
  • 下載 Nacos 并啟動 Nacos server。

    啟動配置管理

    啟動了 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:8848spring.application.name=example

    說明:之所以需要配置?spring.application.name?,是因?yàn)樗菢?gòu)成 Nacos 配置管理?dataId字段的一部分。

    在 Nacos Spring Cloud 中,dataId?的完整格式如下:

    ${prefix}-${spring.profiles.active}.${file-extension}
    • prefix?默認(rèn)為?spring.application.name?的值,也可以通過配置項?spring.cloud.nacos.config.prefix來配置。
    • spring.profiles.active?即為當(dāng)前環(huán)境對應(yīng)的 profile,詳情可以參考?Spring Boot文檔。?注意:當(dāng)?spring.profiles.active?為空時,對應(yīng)的連接符?-?也將不存在,dataId 的拼接格式變成?${prefix}.${file-extension}
    • file-exetension?為配置內(nèi)容的數(shù)據(jù)格式,可以通過配置項?spring.cloud.nacos.config.file-extension?來配置。目前只支持?properties?和?yaml?類型。
  • 通過 Spring Cloud 原生注解?@RefreshScope?實(shí)現(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"
  • 運(yùn)行?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é)通過實(shí)現(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-providerspring.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);}@RestControllerclass EchoController {@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)public String echo(@PathVariable String string) {return "Hello Nacos Discovery " + string;}} }
  • 配置服務(wù)消費(fèi)者,從而服務(wù)消費(fèi)者可以通過 Nacos 的服務(wù)注冊發(fā)現(xiàn)功能從 Nacos server 上獲取到它要調(diào)用的服務(wù)。
  • i. 在?application.properties?中配置 Nacos server 的地址:

    server.port=8080 spring.application.name=service-consumerspring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

    ii. 通過 Spring Cloud 原生注解?@EnableDiscoveryClient?開啟服務(wù)注冊發(fā)現(xiàn)功能。給?RestTemplate?實(shí)例添加?@LoadBalanced?注解,開啟?@LoadBalanced?與?Ribbon?的集成:

    @SpringBootApplication @EnableDiscoveryClient public class NacosConsumerApplication {@LoadBalanced@Beanpublic RestTemplate restTemplate() {return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(NacosConsumerApplication.class, args);}@RestControllerpublic class TestController {private final RestTemplate restTemplate;@Autowiredpublic 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。
  • 總結(jié)

    以上是生活随笔為你收集整理的Nacos 使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。