SpringCloud學習系列之六 ----- 路由網關Zuul基礎使用教程


前言

在上篇中介紹了SpringCloud Config的完美使用版本,本篇則介紹基於SpringCloud(基於SpringBoot2.x,.SpringCloud Finchley版)中的路由網關(SpringCloud Zuul)的使用教程。

SpringCloud Zuul

介紹

Spring Cloud Zuul 主要的功能是提供負載均衡、反向代理、權限認證、動態路由、監控、彈性、安全等的邊緣服務。其主要作用是為微服務架構提供了前門保護的作用,同時將權限控制這些較重的非業務邏輯內容遷移到服務路由層面,使得服務集群主體能夠具備更高的可復用性和可測試性。

通俗一點來說,就是對服務提供一層保護,對外界的請求進行過濾轉發到后端服務中。
這里我們可以通過幾張簡單的示例圖來進行了解.。

不使用路由網關的示例圖:

使用路由網關的示例圖:

使用路由網關並且加上注冊中心的示例圖:

從上述的示例圖中,我們發現加了路由網關之后,實際上是將一對多的關系轉變成了一對一的關系,這樣的好處是我們可以在網關層進行數據合法校驗、權限認證、負載均衡等統一處理,這樣可以在很大的程度上節省的人力和物力,但是這種方式也有一定的弊端,就是以后新增了服務或者在服務中新增方法,就會使得網關層可能需要進行改動。幸好在Spring Cloud 有 Zuul 這樣一個組件,通過eureka將網關和微服務之間相互關聯起來,都會在eureka上進行注冊,這樣Zuul就能感知到哪些服務在線,並且可以通過配置路由規則將請求自動轉發到指定的后端微服務上,這樣即使后續新增了服務或者在服務中新增了某些方法,那么只需在Zuul層進行簡單配置即可。

開發准備

開發環境

  • JDK:1.8
  • SpringBoot:2.0.6.RELEASE
  • SpringCloud:Finchley.SR2

注:不一定非要用上述的版本,可以根據情況進行相應的調整。需要注意的是SpringBoot2.x以后,jdk的版本必須是1.8以上!

服務端

由於我們這里是使用的第三種模式,所以需要使用到Eureka注冊中心,因此這里我們也順便弄一個注冊中心服務。
注冊中心這塊配置和代碼和之前springcloud-config配置基本一樣即可。注冊中心新項目的的名稱為springcloud-zuul-eureka

注冊中心pom配置、application.properties配置和代碼如下:

pom:

 <dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
	</dependency>

application.properties:

spring.application.name=springcloud-zuul-eureka
server.port=8006
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

代碼:


	@EnableEurekaServer
	@SpringBootApplication
	public class ZuulApplication {
	  public static void main(String[] args) {
	      SpringApplication.run(ZuulApplication.class, args);
	      System.out.println("zuul注冊中心服務啟動...");
	  }
	}

注冊中心服務配置完成之后,我們在新增一個Zuul服務,該服務的名稱為springcloud-zuul-gateway,該服務的 pom配置、application.properties配置和代碼如下:

pom:

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter</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-netflix-zuul</artifactId>
    </dependency>
</dependencies>

application.properties:

spring.application.name=springcloud-zuul-gateway
server.port = 9009
eureka.client.serviceUrl.defaultZone=http://localhost:8006/eureka/

zuul.routes.hello.path = /hello/**
zuul.routes.hello.url = http://localhost:9010/hello
zuul.routes.hi.path = /hi/**
zuul.routes.hi.url = http://localhost:9011/hi

配置說明:

  • spring.application.name: 這個是指定服務名稱。
  • server.port:服務指定的端口。
  • eureka.client.serviceUrl.defaultZone: 這個是設置與Eureka Server交互的地址,客戶端的查詢服務和注冊服務都需要依賴這個地址。
  • zuul.routes.{route}.path:自定義路由的規則,通過path配置路徑進行過濾;
  • zuul.routes.{route}.url: 自定義路由的規則,訪問上述的路徑會轉發到該配置的地址;

注:上述的zuul.routes.{route}.pathzuul.routes.{route}.url一般來說是作為傳統的方式進行配置,是不依賴於Eureka,是屬於一對一的配置。例如,訪問:http://localhost:9009/hello/pancm 的話就會跳轉到http://localhost:9010/hello/pancm 地址上。

代碼:


@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy   
public class ZuulApplication {
	public static void main(String[] args) {
		SpringApplication.run(ZuulApplication.class, args);
		 System.out.println("zuul 服務啟動...");
	}
}
	

客戶端

這里我們也需要創建兩個客戶端服務,來進行驗證Zuul路由網關是否生效。
創建兩個客戶端服務,名稱分別為springcloud-zuul-server1springcloud-zuul-server2,兩個pom文件的配置如下:

 <dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>

兩個application.properties配置文件也基本相同,除了名稱和端口不一致。

springcloud-zuul-server1application.properties配置:

spring.application.name=springcloud-zuul-server1
server.port=9010
eureka.client.serviceUrl.defaultZone=http://localhost:8006/eureka/

springcloud-zuul-server2application.properties配置:

spring.application.name=springcloud-zuul-server2
server.port=9011
eureka.client.serviceUrl.defaultZone=http://localhost:8006/eureka/

springcloud-zuul-server1服務的代碼:

主類

@SpringBootApplication
@EnableDiscoveryClient
public class ZuulServerApplication1 {
	public static void main(String[] args) {
		SpringApplication.run(ZuulServerApplication1.class, args);
		  System.out.println("zuul 第一個服務啟動...");
	}
}

控制層:

@RestController
public class ConsumerController {

@RequestMapping("/hello/{name}")
public String index(@PathVariable String name) {
    return name+",Hello World!";
}

}

springcloud-zuul-server2服務的代碼:

主類

@SpringBootApplication
@EnableDiscoveryClient
public class ZuulServerApplication2 {
	public static void main(String[] args) {
		SpringApplication.run(ZuulServerApplication2.class, args);
		  System.out.println("zuul 第二個服務啟動...");
	}
}

控制層:

@RestController
public class ConsumerController {

	@RequestMapping("/hi")
    public String index(@RequestParam String name) {
        return name+",hi!";
    }
}

注:這里故意將兩個服務的接口參數請求和返回值弄成不一樣,以便對Zull的功能進行多方面測試。

測試

完成上述的代碼開發后,我們來進行測試springcloud-zuul是否可以地址過濾轉發功能。
首先依次啟動springcloud-zuul-eurekaspringcloud-zuul-gatewayspringcloud-zuul-server1springcloud-zuul-server2這四個項目。其中9009是服務springcloud-zuul-gateway的端口,9010是第一個客戶端springcloud-zuul-server1的端口,9011是第二個客戶端springcloud-zuul-server2的端口。
啟動成功之后,在瀏覽器輸入:

http://localhost:9010/hello/pancm

界面返回:

pancm,hello world!!

在瀏覽器輸入:

http://localhost:9011/hi?name=pancm

界面返回:

pancm,hi!

可以看出程序正常啟動,並且客戶端的接口返回正確!
那么我們再來使用同樣路徑來訪問zuul,因為是在本地進行測試,因此只需要更改下端口就可以了,將上述在瀏覽器訪問的地址的端口自都改成9009。

在瀏覽器輸入:

http://localhost:9009/hello/pancm

界面返回:

pancm,Hello World!

在瀏覽器輸入:

http://localhost:9009/hi?name=pancm

界面返回:

pancm,hi!

示例圖:

從上述示例中,我們可以得出zuul路由網關已經生效了,成功的幫我們的請求進行了轉發!

其他

總結

本篇文章主要介紹了關於zuul的基本使用,使用的方式也是單例的,一個路由規則對應一個地址,按照上述的三幅示例圖來說,主要是介紹了第二種。介於篇幅問題,通過Eureka注冊中心方式實現、zuul的詳細配置、 以及zuul的核心模塊過濾器還未講解,這些留在下一篇再來講解。

項目地址

基於SpringBoot2.x、SpringCloud的Finchley版本開發的地址:https://github.com/xuwujing/springcloud-study

如果感覺項目不錯,希望能給個star,謝謝!

音樂推薦

這首純音樂聽起來有種似曾相識的感覺,但仔細聽下來,又並非是自己熟悉的一首。不過真是因為這樣,才有感覺吧。

原創不易,如果感覺不錯,希望留言推薦!您的支持是我寫作的最大動力!
版權聲明:
作者:虛無境
博客園出處:http://www.cnblogs.com/xuwujing
CSDN出處:http://blog.csdn.net/qazwsxpcm    
個人博客出處:http://www.panchengming.com


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM