Spring Cloud入門教程之Zuul實(shí)現(xiàn)API網(wǎng)關(guān)與請(qǐng)求過濾
簡(jiǎn)介
Zuul是Netflix基于JVM的路由器和服務(wù)器端負(fù)載均衡器。最常用的場(chǎng)景是替換Nginx反向代理后臺(tái)微服務(wù)供前端UI訪問。
Zuul使用Ribbon來定位一個(gè)通過發(fā)現(xiàn)轉(zhuǎn)發(fā)的實(shí)例,所有請(qǐng)求都以hystrix命令執(zhí)行,所以故障將顯示在Hystrix指標(biāo)中。
注:Zuul不包括發(fā)現(xiàn)客戶端,因此對(duì)于基于服務(wù)ID的路由,需要在類路徑中提供其中一個(gè)路由
Zuul是Spring Cloud提供的api網(wǎng)關(guān)和過濾組件,它提供如下功能:
- 認(rèn)證
- 過濾
- 壓力測(cè)試
- Canary測(cè)試
- 動(dòng)態(tài)路由
- 服務(wù)遷移
- 負(fù)載均衡
- 安全
- 靜態(tài)請(qǐng)求處理
- 動(dòng)態(tài)流量管理
在本教程中,我們將用zuul,把web端的請(qǐng)求/product轉(zhuǎn)發(fā)到對(duì)應(yīng)的產(chǎn)品服務(wù)上,并且定義一個(gè)pre過濾器來驗(yàn)證是否經(jīng)過了zuul的轉(zhuǎn)發(fā)。
基礎(chǔ)環(huán)境
- JDK 1.8
- Maven 3.3.9
- IntelliJ 2018.1
- Git
項(xiàng)目源碼
創(chuàng)建Zuul服務(wù)
在IntelliJ中創(chuàng)建一個(gè)maven項(xiàng)目:
- cn.zxuqian
- apiGateway
然后在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>cn.zxuqian</groupId> <artifactId>apiGateway</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <!-- name has changed, before: spring-cloud-starter-zuul --> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.M9</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <properties> <java.version>1.8</java.version> </properties> <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/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
需要注意的是,Spring官網(wǎng)的教程給的zuul的artifactId為spring-cloud-starter-zuul,這個(gè)是舊版zuul的名字,在我們的Finchley.M9版本中已經(jīng)更名為spring-cloud-starter-netflix-zuul。
添加src/main/resources/bootstrap.yml文件,指定spring.application.name:
spring: application: name: zuul-server
創(chuàng)建cn.zxuqian.Application類:
package cn.zxuqian; import cn.zxuqian.filters.PreFilter; 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; import org.springframework.context.annotation.Bean; @EnableZuulProxy @EnableDiscoveryClient @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public PreFilter preFilter() { return new PreFilter(); } }
這里使用了@EnableZuulProxy來指定使用zuul的反向代理,把我們的請(qǐng)求轉(zhuǎn)發(fā)到對(duì)應(yīng)的服務(wù)器上。然后啟用了eureka的服務(wù)發(fā)現(xiàn)。Zuul默認(rèn)也會(huì)使用Ribbon做負(fù)載均衡,所以可以通過eureka發(fā)現(xiàn)已注冊(cè)的服務(wù)。PreFilter是一個(gè)預(yù)過濾器,用來在request請(qǐng)求被處理之前進(jìn)行一些操作,它的代碼如下:
package cn.zxuqian.filters; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletRequest; public class PreFilter extends ZuulFilter { private static Logger log = LoggerFactory.getLogger(PreFilter.class); @Override public String filterType() { return "pre"; } @Override public int filterOrder() { return 1; } @Override public boolean shouldFilter() { return true; } @Override public Object run() throws ZuulException { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); log.info(String.format("%s 方式請(qǐng)求 %s", request.getMethod(), request.getRequestURL().toString())); return null; } }
filterType - Zuul內(nèi)置的filter類型有四種,pre, route,post,error,分別代表請(qǐng)求處理前,處理時(shí),處理后和出錯(cuò)后。
filterOrder - 指定了該過濾器執(zhí)行的順序。
shouldFilter - 是否開啟此過濾器。
run - 過濾器的業(yè)務(wù)邏輯。這里只是簡(jiǎn)單的log了一下reqeust的請(qǐng)求方式和請(qǐng)求的路徑。
接下來,在我們的配置中心的git倉(cāng)庫(kù)中創(chuàng)建zuul-server.yml文件,并添加如下配置:
server: port: 8083 zuul: routes: products: path: /product/** serviceId: product-service
這里配置了zuul的端口為8083,然后映射所有/product/的請(qǐng)求到我們的product-service服務(wù)上。如果不配置serviceId,那么products這個(gè)Key就會(huì)默認(rèn)作為ServiceId,而我們的例子中,ServiceId包括了-,所以在下邊顯示指定了ServiceId。配置完成后提交到git。
更新productService
productService的uri做了一點(diǎn)改動(dòng),使其更符合rest風(fēng)格:
@RequestMapping("/list") public String productList() { log.info("Access to /products endpoint"); return "外套,夾克,毛衣,T恤"; }
這里@RequestMapping匹配的路徑改為了/list,之前是/products。
更新web客戶端
在我們的web客戶端的ProductService中添加一個(gè)新的方法:
public String productListZuul() { return this.restTemplate.getForObject("http://zuul-server/product/list", String.class); }
這次我們直接請(qǐng)求zuul-server服務(wù),然后由它把我們的請(qǐng)求反射代理到product-service服務(wù)。最后在ProductController中添加一個(gè)請(qǐng)求處理方法:
@RequestMapping("/product/list") public String productListZuul() { return productService.productListZuul(); }
用來處理/product/list請(qǐng)求,然后調(diào)用ProductService類中的方法。
測(cè)試
使用mvn spring-boot:run啟動(dòng)configServer,registry, zuulServer, productService,web這幾個(gè)工程,然后啟動(dòng)第二個(gè)productService,使用SERVER_PORT=8082 spring-boot:run。
訪問幾次http://localhost:8080/product/list,然后除了會(huì)在瀏覽器看到返回的結(jié)果,我們還會(huì)在zuulServer的命令行窗口中看到如下字樣:
GET 方式請(qǐng)求 http://xuqians-imac:8083/product/list
然后在兩個(gè)productService的命令行窗口中,我們還會(huì)看到隨機(jī)出現(xiàn)的
Access to /products endpoint
說明zuulServer也會(huì)自動(dòng)進(jìn)行負(fù)載均衡。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
JAVA爬蟲實(shí)現(xiàn)自動(dòng)登錄淘寶
給大家分享一個(gè)關(guān)于JAVA爬蟲的相關(guān)知識(shí)點(diǎn),通過代碼實(shí)現(xiàn)自動(dòng)登錄淘寶網(wǎng),有興趣的朋友測(cè)試下。2018-04-04JavaWeb使用mvc模式實(shí)現(xiàn)登錄功能
本文主要介紹了JavaWeb使用mvc模式實(shí)現(xiàn)登錄功能,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01java隨機(jī)抽取指定范圍不重復(fù)的數(shù)字
這篇文章主要介紹了java隨機(jī)抽取指定范圍不重復(fù)的數(shù)字的相關(guān)資料,需要的朋友可以參考下2016-06-06java.io.File的renameTo方法移動(dòng)文件失敗的解決方案
這篇文章主要介紹了java.io.File的renameTo方法移動(dòng)文件失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07重寫equals的同時(shí)為何要重寫hashCode?
這篇文章主要給大家介紹了關(guān)于重寫equals的同時(shí)為何要重寫hashCode的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01確保SpringBoot定時(shí)任務(wù)只執(zhí)行一次的常見方法小結(jié)
在Spring Boot項(xiàng)目中,確保定時(shí)任務(wù)只執(zhí)行一次是一個(gè)常見的需求,這種需求可以通過多種方式來實(shí)現(xiàn),以下是一些常見的方法,它們各具特點(diǎn),可以根據(jù)項(xiàng)目的實(shí)際需求來選擇最合適的方法,需要的朋友可以參考下2024-10-10Java圖像之自定義角度旋轉(zhuǎn)(實(shí)例)
這篇文章主要介紹了Java圖像之自定義角度旋轉(zhuǎn)(實(shí)例),需要的朋友可以參考下2017-09-09