一、前言
網關也被稱爲邊緣服務,因爲網關是微服務集羣的統一入口處在微服務的外圍所以被稱爲邊緣服務。微服務集羣搭建好之後便需要一個統一的入口來處理鑑權等邏輯,所以網關在微服務集羣中也很重要。本篇博客便講解Spring Cloud的zuul網關組件。
二、開發工具及相關版本號
開發工具 | 版本號 |
---|---|
idea | 2018.2.4 |
spring boot | 2.1.3 |
spring cloud | Greenwich.RELEASE |
java | 1.8 |
三、搭建zuul網關
新建一個spring boot項目,代碼如下。
- maven依賴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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gateway</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<!-- web服務-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- zuul網關-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!-- eureka客戶端-->
<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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</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>
</repository>
</repositories>
</project>
- application.yml配置文件如下
spring:
application:
name: zuul-gateway
server:
port: 80
zuul:
routes:
api-a:
path: /micro-consumer/** #匹配後轉發至某服務
service-id: micro-consumer
sensitive-header:
api-b:
path: /baidu/** #轉發至具體的url
url: http://www.baidu.com
host:
connect-timeout-millis: 30000 #網關超時時間
eureka:
client:
service-url:
defaultZone: http://admin:admin@localhost:8080/eureka/
- 啓動類添加@EnableEurekaClient註解和@EnableZuulProxy註解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableEurekaClient
@EnableZuulProxy
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
- 啓動其他微服務,打開註冊中心查看,zuul-gateway已經成功註冊到註冊中心
- 查看效果
現在通過網關訪問http://localhost/micro-consumer/consumer/hello,如下圖
- 通過網關訪問 http://localhost/micro-provider/provider/hello ,如下圖
可以看到在網關的配置中我們並沒有配置生產者的路由轉發,但是生產者也可以通過網關訪問到。其實zuul網關默認就會匹配url如果匹配到了和微服務服務名一樣的路由,默認就會將請求轉發至該服務。
五、注意
- zuul網關默認會將"Cookie", “Set-Cookie”, "Authorization"這三個敏感的請求頭屏蔽,如果不需要此動作在配置文件中將sensitive-header配置爲空就好
2.zuul網關默認的超時時間很短需要手動配置,但是配置時要注意保證超時時間要比Hystrix的大
六、 小結
網關在微服務集羣中出來做一些路由轉發的工作外還會處理一些過濾鑑權操作,下一節中將講解zuul網關的過濾器的使用。
博客中所有的代碼都以上傳到我的github上了,地址:俞兆鵬的github