SpringCloud超詳細(xì)講解微服務(wù)網(wǎng)關(guān)Zuul基礎(chǔ)
一、Zuul的簡(jiǎn)介
1、Zuul是怎么工作的
Zull包含了對(duì)請(qǐng)求的路由(用來跳轉(zhuǎn)的)和過濾兩個(gè)最主要功能:
其中路由功能負(fù)責(zé)將外部請(qǐng)求轉(zhuǎn)發(fā)到具體的微服務(wù)實(shí)例上,是實(shí)現(xiàn)外部訪問統(tǒng)一入口的基礎(chǔ),而過濾器功能則負(fù)責(zé)對(duì)請(qǐng)求的處理過程進(jìn)行干預(yù),是實(shí)現(xiàn)請(qǐng)求校驗(yàn),服務(wù)聚合等功能的基礎(chǔ)。Zuul和Eureka進(jìn)行整合,將Zuul自身注冊(cè)為Eureka服務(wù)治理下的應(yīng)用,同時(shí)從Eureka中獲得其他服務(wù)的消息,也即以后的訪問微服務(wù)都是通過Zuul跳轉(zhuǎn)后獲得。
注意:Zuul 服務(wù)最終還是會(huì)注冊(cè)進(jìn) Eureka
提供:代理 + 路由 + 過濾 三大功能!
2、Zuul能干嘛
- 路由
- 過濾
- (摘抄自遇見狂神說)
二、Zuul的使用
1、配置Pom.xml
新建一個(gè)module:springcloud-zuul-9050,需要引入Zuul的依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.you</groupId> <artifactId>springcloud-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--引入Eureka的依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--引入ribbon--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--引入DashBorder--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--引入hystrix依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--引入zuul依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> <version>1.4.6.RELEASE</version> </dependency> </dependencies>
2、配置Application.yml
需要配置端口,自己的名字,Eureka以及Zuul
server:
port: 9050spring:
application:
name: springcloud-zuul
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
instance-id: zuul-9050
prefer-ip-address: true #隱藏ip地址info: #autuator監(jiān)控信息
app.name: rainhey-springcloud
company.name: www.rainhey.com#Zuul配置
Zuul:
routes:
mydept.serviceId: springcloud-provider-dept # 用到服務(wù)的名字
mydept.path: /mydept/** # 改成的名字
ignored-services: springcloud-provider-dept #禁止用這個(gè)訪問
prefix: /you # 前綴
3、撰寫啟動(dòng)類
寫一個(gè)啟動(dòng)類ZuulApplicaiton_9050,同時(shí)加上一句@EnableZuulProxy
package com.you; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableZuulProxy public class ZuulApplicaiton_9050 { public static void main(String[] args) { SpringApplication.run(ZuulApplicaiton_9050.class,args); } }
4、效果圖
啟動(dòng)DeptApplication_Hystrix_8001、springcloud-eureka-7001、springcloud-zuul-9050。訪問端口7001,可以看到兩個(gè)服務(wù)都注冊(cè)進(jìn)來了。
訪問8011的get請(qǐng)求,也可以拿到數(shù)據(jù)
訪問9050的Zuul也可以成功訪問
使用Zuul修改服務(wù)名后,我們可以用自己設(shè)置的地址去訪問服務(wù)
Zuul:
routes:
mydept.serviceId: springcloud-provider-dept
mydept.path: /mydept/**
但是此刻用原服務(wù)的名字也是可以訪問的
Zuul支持禁止某個(gè)名字訪問??梢酝ㄟ^ignored-services: 服務(wù)名來設(shè)置
Zuul:
routes:
mydept.serviceId: springcloud-provider-dept
mydept.path: /mydept/**
# 下面這個(gè)是我們此時(shí)需要的
ignored-services: springcloud-provider-dept #禁止用這個(gè)訪問
#ignored-services: "*" #隱藏全部的服務(wù),也就是原服務(wù)名不可訪問
修改后的地址是可以訪問的
用原服務(wù)名進(jìn)行訪問,則顯示找不到
三、學(xué)會(huì)SpringCloud的感觸
其實(shí)學(xué)到一半就想要放棄了,很多基礎(chǔ)的知識(shí)聽不懂。很多人說狂神講的不好,但我自己跟著學(xué)、跟著打、跟著寫博客,還是收獲不菲!現(xiàn)在我要重新學(xué)習(xí)基礎(chǔ)的JavaWeb了,框架的學(xué)習(xí)很快,一個(gè)星期入門足夠了,但是基礎(chǔ)的學(xué)習(xí),卻是不易。抓住本質(zhì),好好利用時(shí)間,好好珍惜自己的青春!
到此這篇關(guān)于SpringCloud超詳細(xì)講解微服務(wù)網(wǎng)關(guān)Zuul基礎(chǔ)的文章就介紹到這了,更多相關(guān)SpringCloud Zuul內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java項(xiàng)目怎么集成stable diffusion圖文生成算法
在開發(fā)Java項(xiàng)目過程中,我們經(jīng)常需要使用消息傳遞來實(shí)現(xiàn)不同組件之間的通信,Stable Diffusion是一種基于消息傳遞的實(shí)時(shí)通信解決方案,使用Java調(diào)用外部服務(wù)(如Python腳本或API服務(wù)),這些服務(wù)運(yùn)行Stable Diffusion模型,本文將介紹如何將Stable Diffusion集成到Java項(xiàng)目2024-07-07httpclient模擬post請(qǐng)求json封裝表單數(shù)據(jù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猦ttpclient模擬post請(qǐng)求json封裝表單數(shù)據(jù)的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12Java?Valhalla?Project項(xiàng)目介紹
這篇文章主要介紹了Java?Valhalla?Project項(xiàng)目介紹,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09SpringBoot2.0整合Shiro框架實(shí)現(xiàn)用戶權(quán)限管理的示例
這篇文章主要介紹了SpringBoot2.0整合Shiro框架實(shí)現(xiàn)用戶權(quán)限管理的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08擲6面骰子6000次每個(gè)點(diǎn)數(shù)出現(xiàn)的概率
今天小編就為大家分享一篇關(guān)于擲6面骰子6000次每個(gè)點(diǎn)數(shù)出現(xiàn)的概率,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02Java Hutool 包工具類推薦 ExcelUtil詳解
這篇文章主要介紹了Java Hutool 包工具類推薦 ExcelUtil詳解,需要引入hutool包,版本號(hào)可根據(jù)實(shí)際情況更換,除hutool包之外,還需要引入操作Excel必要包,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09