【Spring Cloud】Zuul路由網關

前言

     Zuul是Netflix開源的一個API Gateway服務器,本質上是一個web servlet應用。Zuul在雲平臺上提供動態路由,監控、彈性、安全等邊緣服務的框架,Zuul相當於是設備和Netflix流應用的Web網站後端所有請求的前門。

Zuul工作原理   

 一、過濾器機制

     zuul的核心是一系列的filters,其作用可以類比Servlet框架的Filter或AOP,它把Request route到用戶處理邏輯的過程中,filter參與一些過濾處理,比如Authentication,Load Shedding等。

二、過濾器的生命週期

 

 

zuul項目搭建

新建服務zuul--microservicecloud-zuul-gateway-9527

1.引入依賴

<parent>
		<groupId>com.atguigu.springcloud</groupId>
		<artifactId>microservicecloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<artifactId>microservicecloud-zuul-gateway-9527</artifactId>

	<dependencies>
		<!-- zuul路由網關 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zuul</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<!-- actuator監控 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<!-- hystrix容錯 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<!-- 日常標配 -->
		<dependency>
			<groupId>com.atguigu.springcloud</groupId>
			<artifactId>microservicecloud-api</artifactId>
			<version>${project.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jetty</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>
		</dependency>
		<!-- 熱部署插件 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>

</project>

2.添加配置文件application.yml

其中關於zuul的一些屬性,prefix: /atguigu 是地址前綴的設置;mydept.path: /mydept/** 是設置訪問服務時的路由配置

server: 
  port: 9527
 
spring: 
  application:
    name: microservicecloud-zuul-gateway
 
eureka: 
  client: 
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka  
  instance:
    instance-id: gateway-9527.com
    prefer-ip-address: true 
 
 
zuul: 
  #ignored-services: microservicecloud-dept
  prefix: /atguigu
  ignored-services: "*"
  routes: 
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**
 
info:
  app.name: atguigu-microcloud
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

3.主啓動類添加註解

@EnableZuulProxy

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

4.啓動驗證路由效果

不用路由:

http://localhost:8001/dept/get/1

啓用路由: 

前綴:atguigu

mydept代替服務名稱:microservicecloud-dept

http://myzuul.com:9527/atguigu/mydept/dept/get/1

小結

    zuul的路由功能負責將外部請求轉發到具體的微服務實例上,是實現外部訪問同意入口的基礎;

    zuul的過濾器功能則負責對請求的處理過程進行干預,是實現請求校驗、服務聚合等功能的基礎。

    將zuul自身註冊爲Eureka服務治理下的應用,同時從Eureka中獲得其他微服務的消息,也即以後的訪問微服務是通過Zuul跳轉後獲得。

                                                                           感謝您的訪問!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章