新聞中心
本文轉(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)容如下:
- spring.application.name=producer
- #######################配置中心配置#################################
- # 指定的命名空間,只會在對應(yīng)的命名空間下查找對應(yīng)的配置文件
- spring.cloud.nacos.config.namespace=caeser-adsys-naming
- spring.cloud.nacos.config.file-extension=properties
- # 配置的分組名稱
- spring.cloud.nacos.config.group=TEST1
- # 配置文件,數(shù)組形式,可以多個,依次遞增
- spring.cloud.nacos.config.ext-config[0].data-id=com.example.properties
- spring.cloud.nacos.config.ext-config[0].group=TEST1
- # 配置中心的地址
- spring.cloud.nacos.config.server-addr=127.0.0.1:8848
- #啟用自動刷新對應(yīng)的配置文件
- spring.cloud.nacos.config.ext-config[0].refresh=true
- ######################服務(wù)注冊發(fā)現(xiàn)配置##################################
- # 服務(wù)集群名稱
- spring.cloud.nacos.discovery.cluster-name=TEST1_GROUP
- # 服務(wù)注冊中心的地址
- spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
- # 服務(wù)的命名空間
- spring.cloud.nacos.discovery.namespace=caeser-adsys-naming
2.application.properties 的文件內(nèi)容如下,主要就是一個端口,其他配置根據(jù)情況自行添加或刪除就好:
- # 服務(wù)啟動的端口
- server.port=8080
- spring.main.allow-bean-definition-overriding=true
- # tomcat 配置
- server.tomcat.max-threads=500
- spring.mvc.servlet.load-on-startup=1
- spring.servlet.multipart.max-file-size=40MB
- spring.servlet.multipart.max-request-size=100MB
- # 日志配置
- logging.level.root=info
- logging.level.com.alibaba=error
- 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.在啟動類上面增加如下注解
- package com.ziyou.nacos.demo.producer;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cache.annotation.EnableCaching;
- import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
- @SpringBootApplication(scanBasePackages = "com.ziyou.nacos")
- @EnableDiscoveryClient
- @EnableCaching
- public class ProducerApplication {
- public static void main(String[] args) {
- SpringApplication.run(ProducerApplication.class, args);
- }
- }
4.pom.xml 文件內(nèi)容如下:
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0 org.example nacos-demo 1.0-SNAPSHOT producer 1.0-SNAPSHOT producer Maven Webapp http://www.example.com UTF-8 1.7 1.7 2.2.2.RELEASE org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-logging org.springframework.boot spring-boot-starter-log4j2 org.springframework.boot spring-boot-starter-web com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery src/main/resources true **/** producer org.springframework.boot spring-boot-maven-plugin ${spring.maven.plugin.version} repackage
5.在 Producer 側(cè)提供一個獲取配置里面內(nèi)容的接口,代碼如下:
- package com.ziyou.nacos.demo.producer.controller;
- import com.ziyou.nacos.demo.producer.config.UserConfig;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- /**
- *
- * Function:
- * Author:@author ziyou
- * Date:2021-04-11 19:59
- * Desc:無
- */
- @RestController
- @RequestMapping(value = "producer")
- public class ProducerController {
- private UserConfig userConfig;
- @GetMapping("/getUsername")
- private String getUsername() {
- String result = userConfig.getUsername() + "-" + userConfig.getPassword();
- System.out.println(result);
- return result;
- }
- @Autowired
- public void setUserConfig(UserConfig userConfig) {
- this.userConfig = userConfig;
- }
- }
- package com.ziyou.nacos.demo.producer.config;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.cloud.context.config.annotation.RefreshScope;
- import org.springframework.stereotype.Component;
- /**
- *
- * Function:
- * Author:@author ziyou
- * Date:2021-04-11 20:39
- * Desc:無
- */
- @RefreshScope
- @Component
- public class UserConfig {
- @Value("${username}")
- private String username;
- @Value("${password}")
- private String password;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
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)
- spring.application.name=consumer
- ######################服務(wù)注冊發(fā)現(xiàn)配置##################################
- spring.cloud.nacos.discovery.cluster-name=TEST1_GROUP
- spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
- spring.cloud.nacos.discovery.namespace=caeser-adsys-naming
2.啟動類,配置上 feignClient 需要掃描的包路徑
- package com.ziyou.nacos.demo.consumer;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cache.annotation.EnableCaching;
- import org.springframework.cloud.openfeign.EnableFeignClients;
- /**
- *
- * Function:
- * Author:@author ziyou
- * Date:2021-04-11 17:07
- * Desc:無
- */
- @SpringBootApplication(scanBasePackages = "com.ziyou.nacos")
- @EnableFeignClients(basePackages = {"com.ziyou.nacos.demo.consumer.rpc"})
- @EnableCaching
- public class ConsumerApplication {
- public static void main(String[] args) {
- SpringApplication.run(ConsumerApplication.class, args);
- }
- }
3.編寫調(diào)用 Producer 的接口,F(xiàn)eignClient 里面的 value 就是 Producer 的應(yīng)用名稱
- package com.ziyou.nacos.demo.consumer.rpc;
- import org.springframework.cloud.openfeign.FeignClient;
- import org.springframework.stereotype.Component;
- import org.springframework.web.bind.annotation.GetMapping;
- /**
- *
- * Function:
- * Author:@author ziyou
- * Date:2021-04-11 20:01
- * Desc:無
- */
- @FeignClient(value = "producer")
- @Component
- public interface IProducerFeign {
- /**
- * 獲取生產(chǎn)者名稱接口
- *
- * @return
- */
- @GetMapping("/producer/getUsername")
- String getUsername();
- }
- package com.ziyou.nacos.demo.consumer.controller;
- import com.ziyou.nacos.demo.consumer.rpc.IProducerFeign;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- /**
- *
- * Function:
- * Author:@author ziyou
- * Date:2021-04-11 19:59
- * Desc:無
- */
- @RestController
- @RequestMapping(value = "consumer")
- public class TestNacosController {
- private IProducerFeign iProducerFeign;
- @GetMapping("/testNacos")
- private String testNacos() {
- return iProducerFeign.getUsername();
- }
- @Autowired
- public void setiProducerFeign(IProducerFeign iProducerFeign) {
- this.iProducerFeign = iProducerFeign;
- }
- }
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


咨詢
建站咨詢
