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倉庫中創(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)文章
Spring Boot 2.x 實(shí)現(xiàn)文件上傳功能
這篇文章主要介紹了Spring Boot 2.x 實(shí)現(xiàn)文件上傳功能,本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Spring Boot Actuator監(jiān)控的簡(jiǎn)單使用方法示例代碼詳解
這篇文章主要介紹了Spring Boot Actuator監(jiān)控的簡(jiǎn)單使用,本文通過實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Java實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼(前端部分)
這篇文章主要為大家介紹了如何用Java語言實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼的生成(前端部分),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下2022-10-10
SpringBoot使用MyBatis時(shí)的幾種傳參規(guī)范示例
使用Mybatis作為持久層框架時(shí),對(duì)于數(shù)據(jù)庫的增刪改查等操作都需要參數(shù)的傳遞,本文就詳細(xì)的介紹了一下SpringBoot使用MyBatis時(shí)的幾種傳參規(guī)范示例,感興趣的可以了解一下2022-02-02
java創(chuàng)建jar包并被項(xiàng)目引用步驟詳解
這篇文章主要介紹了java創(chuàng)建jar包并被項(xiàng)目引用步驟詳解,jar包實(shí)現(xiàn)了特定功能的,java字節(jié)碼文件的壓縮包,更多相關(guān)內(nèi)容需要的朋友可以參考一下2022-07-07
java圖片驗(yàn)證碼實(shí)現(xiàn)示例分享
這篇文章主要介紹了java實(shí)現(xiàn)圖片驗(yàn)證碼示例,需要的朋友可以參考下2014-02-02

