日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Springboot 項目集成 Nacos 實現(xiàn)服務(wù)注冊發(fā)現(xiàn)與配置管理

本文轉(zhuǎn)載自微信公眾號「Java極客技術(shù)」,作者鴨血粉絲。轉(zhuǎn)載本文請聯(lián)系Java極客技術(shù)公眾號。

公司主營業(yè)務(wù):成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出新會免費做網(wǎng)站回饋大家。

Hello 大家好,我是阿粉,前面的文章給大家介紹了一下如何在本地搭建微服務(wù)環(huán)境下的服務(wù)注冊中心和配置管理中心 Nacos,今天通過

我們通過使用 SpringBoot 項目集成 Nacos 來給大家演示一下是如何使用 Nacos 來實現(xiàn)服務(wù)發(fā)現(xiàn)和配置管理的。

啟動 Nacos 服務(wù)

啟動完本地搭建的 Nacos 服務(wù)后,我們可以看到,目前的服務(wù)管理下面的服務(wù)列表里面在三個命名空間下都沒有服務(wù),這是正常的,因為目前我們還沒有服務(wù)接入Nacos。

Nacos 服務(wù)啟動成功后,我們再創(chuàng)建兩個 SpringBoot 項目,一個用于接入 Nacos 服務(wù)注冊與發(fā)現(xiàn)和配置中心作為服務(wù)提供者 Producer,另一個只接入 Nacos的服務(wù)注冊與發(fā)現(xiàn),調(diào)用 Producer 獲取配置中心的參數(shù),我們叫做Consumer。

服務(wù)提供者 Producer

1.我們首先創(chuàng)建一個 SpringBoot 的項目,bootstrap.properties 文件內(nèi)容如下:

 
 
 
 
  1. spring.application.name=producer 
  2.  
  3. #######################配置中心配置################################# 
  4. # 指定的命名空間,只會在對應(yīng)的命名空間下查找對應(yīng)的配置文件 
  5. spring.cloud.nacos.config.namespace=caeser-adsys-naming 
  6. spring.cloud.nacos.config.file-extension=properties 
  7. # 配置的分組名稱 
  8. spring.cloud.nacos.config.group=TEST1 
  9. # 配置文件,數(shù)組形式,可以多個,依次遞增 
  10. spring.cloud.nacos.config.ext-config[0].data-id=com.example.properties 
  11. spring.cloud.nacos.config.ext-config[0].group=TEST1 
  12. # 配置中心的地址 
  13. spring.cloud.nacos.config.server-addr=127.0.0.1:8848 
  14. #啟用自動刷新對應(yīng)的配置文件 
  15. spring.cloud.nacos.config.ext-config[0].refresh=true 
  16. ######################服務(wù)注冊發(fā)現(xiàn)配置################################## 
  17.  
  18. # 服務(wù)集群名稱 
  19. spring.cloud.nacos.discovery.cluster-name=TEST1_GROUP 
  20. # 服務(wù)注冊中心的地址 
  21. spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 
  22. # 服務(wù)的命名空間 
  23. spring.cloud.nacos.discovery.namespace=caeser-adsys-naming 

2.application.properties 的文件內(nèi)容如下,主要就是一個端口,其他配置根據(jù)情況自行添加或刪除就好:

 
 
 
 
  1. # 服務(wù)啟動的端口 
  2. server.port=8080 
  3. spring.main.allow-bean-definition-overriding=true 
  4. # tomcat 配置 
  5. server.tomcat.max-threads=500 
  6. spring.mvc.servlet.load-on-startup=1 
  7. spring.servlet.multipart.max-file-size=40MB 
  8. spring.servlet.multipart.max-request-size=100MB 
  9. # 日志配置 
  10. logging.level.root=info 
  11. logging.level.com.alibaba=error 
  12. logging.pattern.console=%clr{[%level]}{green} [%d{yyyy-MM-dd HH:mm:ss}] %clr{[${PID:-}]}{faint} %clr{[%thread]}{magenta} %clr{[%-40.40logger{80}:%line]}{cyan} %msg%n 

3.在啟動類上面增加如下注解

 
 
 
 
  1. package com.ziyou.nacos.demo.producer; 
  2.  
  3. import org.springframework.boot.SpringApplication; 
  4. import org.springframework.boot.autoconfigure.SpringBootApplication; 
  5. import org.springframework.cache.annotation.EnableCaching; 
  6. import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 
  7.  
  8. @SpringBootApplication(scanBasePackages = "com.ziyou.nacos") 
  9. @EnableDiscoveryClient 
  10. @EnableCaching 
  11. public class ProducerApplication { 
  12.  
  13.     public static void main(String[] args) { 
  14.         SpringApplication.run(ProducerApplication.class, args); 
  15.     } 

4.pom.xml 文件內(nèi)容如下:

 
 
 
 
  1.  
  2.  
  3.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
  4.   4.0.0 
  5.  
  6.    
  7.     org.example 
  8.     nacos-demo 
  9.     1.0-SNAPSHOT 
  10.    
  11.  
  12.   producer 
  13.   1.0-SNAPSHOT 
  14.  
  15.   producer Maven Webapp 
  16.    
  17.   http://www.example.com 
  18.  
  19.    
  20.     UTF-8 
  21.     1.7 
  22.     1.7 
  23.     2.2.2.RELEASE 
  24.    
  25.  
  26.    
  27.      
  28.      
  29.       org.springframework.boot 
  30.       spring-boot-starter 
  31.        
  32.          
  33.           org.springframework.boot 
  34.           spring-boot-starter-logging 
  35.          
  36.        
  37.      
  38.      
  39.       org.springframework.boot 
  40.       spring-boot-starter-log4j2 
  41.      
  42.      
  43.       org.springframework.boot 
  44.       spring-boot-starter-web 
  45.      
  46.  
  47.      
  48.      
  49.       com.alibaba.cloud 
  50.       spring-cloud-starter-alibaba-nacos-config 
  51.      
  52.      
  53.      
  54.       com.alibaba.cloud 
  55.       spring-cloud-starter-alibaba-nacos-discovery 
  56.      
  57.    
  58.  
  59.    
  60.      
  61.      
  62.        
  63.        
  64.         src/main/resources 
  65.         true 
  66.          
  67.           **/** 
  68.          
  69.        
  70.      
  71.     producer 
  72.      
  73.        
  74.         org.springframework.boot 
  75.         spring-boot-maven-plugin 
  76.         ${spring.maven.plugin.version} 
  77.          
  78.            
  79.              
  80.               repackage 
  81.              
  82.            
  83.          
  84.        
  85.      
  86.    
  87.  

5.在 Producer 側(cè)提供一個獲取配置里面內(nèi)容的接口,代碼如下:

 
 
 
 
  1. package com.ziyou.nacos.demo.producer.controller; 
  2.  
  3. import com.ziyou.nacos.demo.producer.config.UserConfig; 
  4. import org.springframework.beans.factory.annotation.Autowired; 
  5. import org.springframework.web.bind.annotation.GetMapping; 
  6. import org.springframework.web.bind.annotation.RequestMapping; 
  7. import org.springframework.web.bind.annotation.RestController; 
  8.  
  9. /** 
  10.  * 
     
  11.  * Function:
     
  12.  * Author:@author ziyou
     
  13.  * Date:2021-04-11 19:59
     
  14.  * Desc:
     
  15.  */ 
  16. @RestController 
  17. @RequestMapping(value = "producer") 
  18. public class ProducerController { 
  19.  
  20.     private UserConfig userConfig; 
  21.  
  22.     @GetMapping("/getUsername") 
  23.     private String getUsername() { 
  24.         String result = userConfig.getUsername() + "-" + userConfig.getPassword(); 
  25.         System.out.println(result); 
  26.         return result; 
  27.     } 
  28.  
  29.     @Autowired 
  30.     public void setUserConfig(UserConfig userConfig) { 
  31.         this.userConfig = userConfig; 
  32.     } 
 
 
 
 
  1. package com.ziyou.nacos.demo.producer.config; 
  2.  
  3. import org.springframework.beans.factory.annotation.Value; 
  4. import org.springframework.cloud.context.config.annotation.RefreshScope; 
  5. import org.springframework.stereotype.Component; 
  6.  
  7. /** 
  8.  * 
     
  9.  * Function:
     
  10.  * Author:@author ziyou
     
  11.  * Date:2021-04-11 20:39
     
  12.  * Desc:
     
  13.  */ 
  14. @RefreshScope 
  15. @Component 
  16. public class UserConfig { 
  17.     @Value("${username}") 
  18.     private String username; 
  19.     @Value("${password}") 
  20.     private String password; 
  21.  
  22.     public String getUsername() { 
  23.         return username; 
  24.     } 
  25.  
  26.     public void setUsername(String username) { 
  27.         this.username = username; 
  28.     } 
  29.  
  30.     public String getPassword() { 
  31.         return password; 
  32.     } 
  33.  
  34.     public void setPassword(String password) { 
  35.         this.password = password; 
  36.     } 

6.啟動 Producer,并且手動調(diào)用接口,啟動 Producer 過后,我們在 Nacos 的服務(wù)注冊列表可以看如下所示的內(nèi)容,在 test1 的命名空間下,已經(jīng)有了我們創(chuàng)建的 Producer 服務(wù)。

7.通過手動調(diào)用 Producer 的接口 http://127.0.0.1:8080/producer/getUsername 顯示如下內(nèi)容

并且我們看下此時 Nacos 的配置中心里面配置文件com.example.properties 里面的內(nèi)容正是這個,這個時候我們手動把配置里面password 參數(shù)的值改成JavaGeek666,再次訪問接口,我們會發(fā)現(xiàn)接口的輸出也自動改變了。

修改配置內(nèi)容如下:

再次訪問結(jié)果如下:

服務(wù)調(diào)用者 Consumer

前面我們已經(jīng)完成了Producer 的服務(wù)注冊與配置動態(tài)生效的功能,這個時候基本已經(jīng)可以使用了,不過我們還需要更進一步通過 Nacos 來實現(xiàn)服務(wù)發(fā)現(xiàn),接下來我們創(chuàng)建 Consumer 的 SpringBoot 的項目,配置文件和pom.xml 文件基本一致,只要修改端口以及對應(yīng)地方,下面貼一下不一樣的地方

1.boostrap.properties 內(nèi)容如下,因為這里我們只調(diào)用Producer 的接口,不需要接入 Nacos 的配置中心,所以這里只配置發(fā)服務(wù)注冊與發(fā)現(xiàn)

 
 
 
 
  1. spring.application.name=consumer 
  2.  
  3. ######################服務(wù)注冊發(fā)現(xiàn)配置################################## 
  4. spring.cloud.nacos.discovery.cluster-name=TEST1_GROUP 
  5. spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 
  6. spring.cloud.nacos.discovery.namespace=caeser-adsys-naming 

2.啟動類,配置上 feignClient 需要掃描的包路徑

 
 
 
 
  1. package com.ziyou.nacos.demo.consumer; 
  2.  
  3. import org.springframework.boot.SpringApplication; 
  4. import org.springframework.boot.autoconfigure.SpringBootApplication; 
  5. import org.springframework.cache.annotation.EnableCaching; 
  6. import org.springframework.cloud.openfeign.EnableFeignClients; 
  7.  
  8. /** 
  9.  * 
     
  10.  * Function:
     
  11.  * Author:@author ziyou
     
  12.  * Date:2021-04-11 17:07
     
  13.  * Desc:
     
  14.  */ 
  15. @SpringBootApplication(scanBasePackages = "com.ziyou.nacos") 
  16. @EnableFeignClients(basePackages = {"com.ziyou.nacos.demo.consumer.rpc"}) 
  17. @EnableCaching 
  18. public class ConsumerApplication { 
  19.     public static void main(String[] args) { 
  20.         SpringApplication.run(ConsumerApplication.class, args); 
  21.     } 

3.編寫調(diào)用 Producer 的接口,F(xiàn)eignClient 里面的 value 就是 Producer 的應(yīng)用名稱

 
 
 
 
  1. package com.ziyou.nacos.demo.consumer.rpc; 
  2.  
  3. import org.springframework.cloud.openfeign.FeignClient; 
  4. import org.springframework.stereotype.Component; 
  5. import org.springframework.web.bind.annotation.GetMapping; 
  6.  
  7. /** 
  8.  * 
     
  9.  * Function:
     
  10.  * Author:@author ziyou
     
  11.  * Date:2021-04-11 20:01
     
  12.  * Desc:
     
  13.  */ 
  14. @FeignClient(value = "producer") 
  15. @Component 
  16. public interface IProducerFeign { 
  17.     /** 
  18.      * 獲取生產(chǎn)者名稱接口 
  19.      * 
  20.      * @return 
  21.      */ 
  22.     @GetMapping("/producer/getUsername") 
  23.     String getUsername(); 
  24.  
 
 
 
 
  1. package com.ziyou.nacos.demo.consumer.controller; 
  2.  
  3. import com.ziyou.nacos.demo.consumer.rpc.IProducerFeign; 
  4. import org.springframework.beans.factory.annotation.Autowired; 
  5. import org.springframework.web.bind.annotation.GetMapping; 
  6. import org.springframework.web.bind.annotation.RequestMapping; 
  7. import org.springframework.web.bind.annotation.RestController; 
  8.  
  9. /** 
  10.  * 
     
  11.  * Function:
     
  12.  * Author:@author ziyou
     
  13.  * Date:2021-04-11 19:59
     
  14.  * Desc:
     
  15.  */ 
  16. @RestController 
  17. @RequestMapping(value = "consumer") 
  18. public class TestNacosController { 
  19.  
  20.     private IProducerFeign iProducerFeign; 
  21.  
  22.     @GetMapping("/testNacos") 
  23.     private String testNacos() { 
  24.         return iProducerFeign.getUsername(); 
  25.     } 
  26.  
  27.     @Autowired 
  28.     public void setiProducerFeign(IProducerFeign iProducerFeign) { 
  29.         this.iProducerFeign = iProducerFeign; 
  30.     } 

4.啟動Consumer,我們可以看到在 Nacos 如下圖所示

5.調(diào)用 Consumer 的接口consumer/testNacos,結(jié)果如下圖所示,同樣的如果此時更改了 Nacos 配置文件中的內(nèi)容,Consumer 這邊也是可以實時更新的,感興趣的小伙伴可以自己試試。

今天主要給大家介紹了一下如何通過 SpringBoot 項目來接入 Naocs 實現(xiàn)服務(wù)注冊與發(fā)現(xiàn),以及配置管理和動態(tài)刷新,相關(guān)的代碼已經(jīng)上傳到 GitHub 了。


分享名稱:Springboot 項目集成 Nacos 實現(xiàn)服務(wù)注冊發(fā)現(xiàn)與配置管理
文章位置:http://www.dlmjj.cn/article/dphojii.html