知識點:
1.zuul:默認代理所有註冊在Eureka的服務的。
2.Zuul的代理是使用Ribbon通過服務發現來定位後端服務實例的。
一.新建一個項目:ms-zuul-consumer
1.pom文件:
<!-- 註冊eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- zuul-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!-- zuul-core -->
<dependency>
<groupId>com.netflix.zuul</groupId>
<artifactId>zuul-core</artifactId>
<version>1.3.0</version>
</dependency>
<!-- 註冊eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 打印日誌 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- feigin的依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!-- hystrix 的依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--hystrix command -->
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>1.5.12</version>
</dependency>
<!-- dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<!-- zuul-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!-- zuul-core -->
<dependency>
<groupId>com.netflix.zuul</groupId>
<artifactId>zuul-core</artifactId>
<version>1.3.0</version>
</dependency>
2.resources文件:
server:
port: 8008
spring:
application:
name: ms-zuul-consumer
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://ljf:123@localhost:8761/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
3.啓動類: @EnableZuulProxy
package com.ljf.weifuwu.springcloud.zuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableZuulProxy
public class ZuulConsumerApp
{ public static void main(String args[]){
SpringApplication.run(ZuulConsumerApp.class, args);
System.out.println( "zuul啓動起來了!" );
}
}
4.啓動服務:
ms-eureka-center(8761)、ms-eureka-provider(9701)、ms-zuul-consumer(8008)
5.訪問直接訪問提供者:http://localhost:7901/eureka-provider/1
6.通過zuul訪問:http://localhost:8008/ms-eureka-provider/eureka-provider/1
可以發現此請求觸發了Ribbon的“DynamicServerListLoadBalancer”,即客戶端的靜態服務負載均衡服務,這裏根據ServiceId獲取到其ip和端口,然後去訪問該服務,此時Zuul就實現了一個反向代理的功能,通過Zuul服務可以訪問註冊在Eureka上的任意一個服務。
總結:
1.zuul:默認代理所有註冊在Eureka的服務的。
2.Zuul的代理是使用Ribbon通過服務發現來定位後端服務實例的。
二.指定某個微服務進行代理
修改配置文件:
#指定要代理的微服務
zuul:
ignored-services: '*' #過濾掉所有服務請求
routes:
ms-eureka-provider: /user-zuul/**
1.阻止Zuul自動代理所有註冊在Eureka上的服務。
2.設置所有在ServiceId爲“ms-eureka-provider/”的微服務的代理地址變爲“/user-zuul/**”,即原本的訪問地址爲“http://localhost:8008/ms-eureka-provider/eureka-provider/1”,設置之後可以訪問簡寫地址“http://localhost:8008/user-zuul/eureka-provider/1”
也就是說設置之後,http://localhost:8008/ms-eureka-provider/eureka-provider/1 訪問不起作用了,只能通過“http://localhost:8008/user-zuul/eureka-provider/1”進行訪問。
三.通過serviceid+path進行反向代理:
#第二種方式
zuul:
routes:
user:
path: /user-zuul/**
serviceId: ms-eureka-provider
這裏的ServiceId是代理的微服務的實例ID,path是代理的ServiceId微服務對應的訪問路徑,它是一個ant風格的表達式,所以/user/*僅僅匹配一層目錄,而/user/**可以匹配任意多層級目錄。
四.總結:
(1)Spring Cloud創建了一個內置Zuul代理來簡化開發,可以令一個應用使用代理調用後端的一個或者多個服務。
(2)在Spring Boot的入口類上使用@EnableZuulProxy註解來開啓代理。
(3)代理使用Ribbon通過服務發現來定位後端服務實例。
(4)Zuul的所有請求在Hystrix Command中執行。所以當斷路器打開時,代理將不會重試連接後端服務。
(5)Zuul starter不包含服務發現客戶端,所以想要使用服務發現功能,需要提供一個服務發現客戶端(比如Eureka)。