日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

史上最简单的SpringCloud教程 | 第五篇: 路由网关(zuul)

發(fā)布時間:2024/9/27 javascript 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 史上最简单的SpringCloud教程 | 第五篇: 路由网关(zuul) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

轉(zhuǎn):https://blog.csdn.net/forezp/article/details/69939114
最新版本:
史上最簡單的SpringCloud教程 | 第五篇: 路由網(wǎng)關(zuul)(Finchley版本):https://blog.csdn.net/forezp/article/details/81041012

在微服務架構(gòu)中,需要幾個基礎的服務治理組件,包括服務注冊與發(fā)現(xiàn)、服務消費、負載均衡、斷路器、智能路由、配置管理等,由這幾個基礎組件相互協(xié)作,共同組建了一個簡單的微服務系統(tǒng)。一個簡答的微服務系統(tǒng)如下圖:


注意:A服務和B服務是可以相互調(diào)用的,作圖的時候忘記了。并且配置服務也是注冊到服務注冊中心的。

在Spring Cloud微服務系統(tǒng)中,一種常見的負載均衡方式是,客戶端的請求首先經(jīng)過負載均衡(zuul、Ngnix),再到達服務網(wǎng)關(zuul集群),然后再到具體的服。,服務統(tǒng)一注冊到高可用的服務注冊中心集群,服務的所有的配置文件由配置服務管理(下一篇文章講述),配置服務的配置文件放在git倉庫,方便開發(fā)人員隨時改配置。

一、Zuul簡介

Zuul的主要功能是路由轉(zhuǎn)發(fā)和過濾器。路由功能是微服務的一部分,比如/api/user轉(zhuǎn)發(fā)到到user服務,/api/shop轉(zhuǎn)發(fā)到到shop服務。zuul默認和Ribbon結(jié)合實現(xiàn)了負載均衡的功能。zuul有以下功能:Authentication Insights Stress Testing Canary Testing Dynamic Routing Service Migration Load Shedding Security Static Response handling Active/Active traffic management

二、準備工作

繼續(xù)使用上一節(jié)的工程。在原有的工程上,創(chuàng)建一個新的工程。

三、創(chuàng)建service-zuul工程

其pom.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.forezp</groupId><artifactId>service-zuul</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>service-zuul</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zuul</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.RC1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories> </project>

在其入口applicaton類加上注解@EnableZuulProxy,開啟zuul的功能:

@EnableZuulProxy @EnableEurekaClient @SpringBootApplication public class ServiceZuulApplication {public static void main(String[] args) {SpringApplication.run(ServiceZuulApplication.class, args);} }

加上配置文件application.yml加上以下的配置代碼:

eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/ server:port: 8769 spring:application:name: service-zuul zuul:routes:api-a:path: /api-a/**serviceId: service-ribbonapi-b:path: /api-b/**serviceId: service-feign

首先指定服務注冊中心的地址為http://localhost:8761/eureka/,服務的端口為8769,服務名為service-zuul;以/api-a/ 開頭的請求都轉(zhuǎn)發(fā)給service-ribbon服務;以/api-b/開頭的請求都轉(zhuǎn)發(fā)給service-feign服務;
依次運行這五個工程;打開瀏覽器訪問:http://localhost:8769/api-a/hi?name=forezp ;瀏覽器顯示:

hi forezp,i am from port:8762

打開瀏覽器訪問:http://localhost:8769/api-b/hi?name=forezp ;瀏覽器顯示:

hi forezp,i am from port:8762

這說明zuul起到了路由的作用

四、服務過濾

zuul不僅只是路由,并且還能過濾,做一些安全驗證。繼續(xù)改造工程;

@Component public class MyFilter extends ZuulFilter{private static Logger log = LoggerFactory.getLogger(MyFilter.class);@Overridepublic String filterType() {return "pre";}@Overridepublic int filterOrder() {return 0;}@Overridepublic boolean shouldFilter() {return true;}@Overridepublic Object run() {RequestContext ctx = RequestContext.getCurrentContext();HttpServletRequest request = ctx.getRequest();log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));Object accessToken = request.getParameter("token");if(accessToken == null) {log.warn("token is empty");ctx.setSendZuulResponse(false);ctx.setResponseStatusCode(401);try {ctx.getResponse().getWriter().write("token is empty");}catch (Exception e){}return null;}log.info("ok");return null;} }

filterType:返回一個字符串代表過濾器的類型,在zuul中定義了四種不同生命周期的過濾器類型,具體如下:

pre:路由之前 routing:路由之時 post: 路由之后 error:發(fā)送錯誤調(diào)用 filterOrder:過濾的順序 shouldFilter:這里可以寫邏輯判斷,是否要過濾,本文true,永遠過濾。 run:過濾器的具體邏輯??捎煤軓碗s,包括查sql,nosql去判斷該請求到底有沒有權(quán)限訪問。

這時訪問:http://localhost:8769/api-a/hi?name=forezp ;網(wǎng)頁顯示:

token is empty

訪問 http://localhost:8769/api-a/hi?name=forezp&token=22 ;
網(wǎng)頁顯示:

hi forezp,i am from port:8762

本文源碼下載:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter5

五、參考資料

http://projects.spring.io/spring-cloud/spring-cloud.html#_router_and_filter_zuul

總結(jié)

以上是生活随笔為你收集整理的史上最简单的SpringCloud教程 | 第五篇: 路由网关(zuul)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。