午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot + Spring Cloud Consul 服務(wù)注冊和發(fā)現(xiàn)詳細解析

瀏覽:10日期:2023-08-25 09:41:25

SpringBoot + Spring Cloud Consul 服務(wù)注冊和發(fā)現(xiàn)詳細解析

什么是Consul

Consul 是 HashiCorp 公司推出的開源工具,用于實現(xiàn)分布式系統(tǒng)的服務(wù)發(fā)現(xiàn)與配置。與其它分布式服務(wù)注冊與發(fā)現(xiàn)的方案,Consul 的方案更“一站式”,內(nèi)置了服務(wù)注冊與發(fā)現(xiàn)框架、分布一致性協(xié)議實現(xiàn)、健康檢查、Key/Value 存儲、多數(shù)據(jù)中心方案,不再需要依賴其它工具(比如 ZooKeeper 等)。使用起來也較為簡單。Consul 使用 Go 語言編寫,因此具有天然可移植性(支持Linux、windows和Mac OS X);安裝包僅包含一個可執(zhí)行文件,方便部署,與 Docker 等輕量級容器可無縫配合。

Consul安裝

官網(wǎng)(consul.io)最新版本1.8.0 提供了MacOS,Windows, Linux, 如果你不知道怎么安裝,官方還提供了視頻。

SpringBoot + Spring Cloud Consul 服務(wù)注冊和發(fā)現(xiàn)詳細解析

我這里使用docker安裝,安裝過程總結(jié)起來為三句話:

docker search consuldocker pull consul docker run --name consul -d -p 8600:8500 consul

沒問題的話,本機訪問http://localhost:8600則可以打開consul自帶的管理系統(tǒng),默認情況下沒有服務(wù)注冊進來

SpringBoot + Spring Cloud Consul 服務(wù)注冊和發(fā)現(xiàn)詳細解析

準備工作新建父工程, 主要約定SpringCloud, SpringBoot版本號,我使用的是Hoxton.SR1, SpringBoot2.2 新建3個子Module,這里兩個service,一個consumer, 兩個service我用8001和8002端口來區(qū)分,主要是想在服務(wù)消費時反應(yīng)客戶端負載均衡。

項目結(jié)構(gòu)如下圖

SpringBoot + Spring Cloud Consul 服務(wù)注冊和發(fā)現(xiàn)詳細解析

父工程pom.xml

<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

服務(wù)提供者

服務(wù)注冊中心有了,那么我們來開發(fā)兩個服務(wù)提供者,這里新建了兩個Module,端口8001和8002。兩個Module代碼相同,主要為了演示負載使用。

新建Module,添加spring-cloud-starter-consul-disconvery依賴

SpringBoot + Spring Cloud Consul 服務(wù)注冊和發(fā)現(xiàn)詳細解析

pom.xml

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies>

添加配置(application.yml)

server: port: 8001spring: application: name: consul-student-service cloud: consul: port: 8600 host: 127.0.0.1 discovery: service-name: ${spring.application.name}

修改啟動類,添加服務(wù)

這里我直接寫了一個測試接口放在啟動類里。這里我只貼了端口8001的代碼,8002代碼結(jié)構(gòu)相同,只是端口不同。

@SpringBootApplication@EnableDiscoveryClient@RestControllerpublic class ConsulStudentService8001 { public static void main(String[] args) { SpringApplication.run(ConsulStudentService8001.class,args); } @GetMapping('/student/version') public String version(){ return '8001,202007222300'; }}

OK,到這一步,啟動兩個服務(wù),不出異常的情況下,可在注冊中心查看當前的服務(wù)實例。

SpringBoot + Spring Cloud Consul 服務(wù)注冊和發(fā)現(xiàn)詳細解析SpringBoot + Spring Cloud Consul 服務(wù)注冊和發(fā)現(xiàn)詳細解析SpringBoot + Spring Cloud Consul 服務(wù)注冊和發(fā)現(xiàn)詳細解析

Consul消費者

服務(wù)注冊中心有了,服務(wù)提供者也有了,我們再來開發(fā)一個服務(wù)消費者。

新建Module,同樣添加spring-cloud-starter-consul-disconvery依賴

pom.xml

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies>

添加配置(application.yml)

server: port: 8080spring: application: name: consul-student-consumer cloud: consul: host: 127.0.0.1 port: 8600 discovery: service-name: ${spring.application.name} # 不需要注冊到consul中 register: false

修改啟動類,調(diào)用服務(wù)

開發(fā)RestTemplate配置類,調(diào)用REST接口時使用。

@Configurationpublic class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); }}

修改啟動類

@SpringBootApplication@EnableDiscoveryClient@RestControllerpublic class ConsulStudentConsumer { public static void main(String[] args) { SpringApplication.run(ConsulStudentConsumer.class,args); } @Autowired RestTemplate restTemplate; @GetMapping('/consul/student/version') public String version(){ //這里使用服務(wù)實例名調(diào)用REST接口 return restTemplate.getForObject('http://consul-student-service/student/version',String.class); }}

OK, 這一步完成之后,可以啟動消費者接口,刷新幾次,從返回結(jié)果上能看出來是輪訓調(diào)用服務(wù)提供者接口實例。

到此這篇關(guān)于SpringBoot + Spring Cloud Consul 服務(wù)注冊和發(fā)現(xiàn)詳細解析的文章就介紹到這了,更多相關(guān)SpringBoot Spring Cloud Consul 服務(wù)注冊內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 亚洲第一页av | 国产亚洲91 | 亚洲一区久久久 | 亚洲人视频| 噜噜啪啪 | 精品视频三区 | 性欧美极品另类 | 亚洲午夜影视 | 中文字幕永久在线视频 | 日韩一区二区视频在线观看 | 欧产日产国产69 | 中文字幕在线网站 | 69天堂网 | 91在线观看喷潮 | 亚洲午夜伦理 | 亚洲成人精选 | 国产精品www色诱视频 | 国产午夜精品久久久 | 亚洲精品久久久久国产 | 日韩一级精品 | 精品国产乱码久久久久久蜜臀网站 | 亚洲一区二区免费视频 | 免费av免费看 | 欧美三级在线 | 国产成人精 | 日本不卡在线视频 | 亚洲国产999 | 污到下面流水 | 成年人免费小视频 | 国产精品每日更新 | 久久久久国产精品夜夜夜夜夜 | 国产欧美日韩综合精品 | 国产亚洲精品精品精品 | 日本五十路| 亚洲一区二区中文 | 3d动漫啪啪精品一区二区中文字幕 | 成人高潮片免费视频 | 亚洲女优在线 | 日韩福利视频在线观看 | 污到下面流水 | 九一在线 |