這篇文章主要講解了“微服務(wù)中eureka+zuul的基本部署教程”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“微服務(wù)中eureka+zuul的基本部署教程”吧!
專業(yè)從事網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì),高端網(wǎng)站制作設(shè)計(jì),成都小程序開發(fā),網(wǎng)站推廣的成都做網(wǎng)站的公司。優(yōu)秀技術(shù)團(tuán)隊(duì)竭力真誠服務(wù),采用H5開發(fā)+CSS3前端渲染技術(shù),成都響應(yīng)式網(wǎng)站建設(shè)公司,讓網(wǎng)站在手機(jī)、平板、PC、微信下都能呈現(xiàn)。建站過程建立專項(xiàng)小組,與您實(shí)時(shí)在線互動(dòng),隨時(shí)提供解決方案,暢聊想法和感受。
由于前段時(shí)間遇到工作危機(jī)差點(diǎn)被開讓我意識(shí)到技術(shù)的重要性,所以在公司架構(gòu)師的建議下開始了解和學(xué)習(xí)微服務(wù),首先學(xué)習(xí)的就是zuul網(wǎng)關(guān)和eureka注冊(cè)中心,因?yàn)榘滋旃ぷ鏖_發(fā)量比較多,所以只能晚上抽空學(xué)習(xí),如今終于可以簡單的部署一套環(huán)境并成功運(yùn)行起來了,雖然這些對(duì)于老手來說可以算是皮毛了,但是對(duì)我來說還是值得高興的。
關(guān)于zuul和eureka的相關(guān)介紹我就不多說了一搜一大把,直接上配置。
1.首先創(chuàng)建Spring boot。
下一步修改項(xiàng)目名
然后一直下一步就可以了,中間選擇組件的時(shí)候我直接跳過,創(chuàng)建后在pom里加也可以。
2.引入相關(guān)依賴
eureka:
<!-- 服務(wù)端 -> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
zuul:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <!-- 客戶端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
dome1、dome2:
<!-- 客戶端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
3.啟動(dòng)類注解
eureka:
@EnableEurekaServer
zuul:
@EnableZuulProxy
dome1、dome2:
@EnableEurekaClient
4:配置文件
eureka:application.properties
server.port=8080 eureka.instance.hostname=127.0.0.1 # 是否向服務(wù)中心注冊(cè)自己 eureka.client.register-with-eureka=false # 是否檢索服務(wù) eureka.client.fetch-registry=false eureka.instance.prefer-ip-address=true # 服務(wù)注冊(cè)中心的配置內(nèi)容,指定服務(wù)注冊(cè)中心的位置 eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
zuul:application.yml
server: port: 8084 #服務(wù)端口 spring: application: name: api-zuul #服務(wù)名 eureka: client: registry-fetch-interval-seconds: 5 # 獲取服務(wù)列表的周期:5s service-url: defaultZone: http://127.0.0.1:8080/eureka #eureka地址 zuul: prefix: /api # 添加路由前綴所有訪問路由加這個(gè)前綴 retryable: true #是否開啟重試功能 routes: api: # 隨便起 path: /service/** #訪問地址以service請(qǐng)求的都會(huì)轉(zhuǎn)發(fā)到service-provider-A serviceId: service-provider-A # 如果兩個(gè)服務(wù)名都是以service-provider-A注冊(cè)到eureka,zuul會(huì)隨機(jī)轉(zhuǎn)發(fā)
dome1、dome2:application.properties(端口不同,服務(wù)名相同)
# 注冊(cè)中心的注冊(cè)地址 eureka.client.service-url.defaultZone=http://127.0.0.1:8080/eureka/ # 服務(wù)名稱--調(diào)用的時(shí)候根據(jù)名稱來調(diào)用該服務(wù)的方法 spring.application.name=service-provider-A server.port=8081
以上配置為負(fù)載均衡的配置,反向代理可將zuul中routes下的配置新增一個(gè)名,path和serviceid為另一個(gè)服務(wù)名就可以。
dome1、dome2需要各自創(chuàng)建一個(gè)測(cè)試類
package com.example.eurekaclientdemo.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; @RequestMapping("/users") @RestController public class UserController { @GetMapping("/{name}") public Map<String,Object> getUser(@PathVariable("name") String userName) { Map<String,Object> data = new HashMap<>(); data.put("id",userName); data.put("from","provider-A-1"); return data; } }
package com.example.eurekaclient2demo.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; @RequestMapping("/users") @RestController public class UserController { @GetMapping("/{name}") public Map<String,Object> getUser(@PathVariable("name") String userName) { Map<String,Object> data = new HashMap<>(); data.put("id",userName); data.put("from","provider-A-2"); return data; } }
以上配置完成后,依次啟動(dòng)eureka、zuul、dome1、dome2。在瀏覽器輸入http://127.0.0.1:8080后查看注冊(cè)的服務(wù)
然后在瀏覽器輸入http://127.0.0.1:8084/api/service/users/sdffsd,需要刷新來看效果。
感謝各位的閱讀,以上就是“微服務(wù)中eureka+zuul的基本部署教程”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)微服務(wù)中eureka+zuul的基本部署教程這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
本文題目:微服務(wù)中eureka+zuul的基本部署教程
當(dāng)前地址:http://newbst.com/article6/jhcpog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、標(biāo)簽優(yōu)化、商城網(wǎng)站、建站公司、App設(shè)計(jì)、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)