0-前言
我們一個個微服務構建好了,外部的應用如何來訪問內部各種各樣的微服務呢?在微服務架構中,后端服務往往不直接開放給呼叫端,而是通過一個API網關根據請求的url,路由到相應的服務,當添加API網關后,在第三方呼叫端和服務提供方之間就創建了一個代理層,這個代理層直接與呼叫方通信進行權限控制,后將請求均衡分發給后臺服務端,
Zuul:就是一個API中間代理層,可以用來執行認證、動態路由、服務前移、負載均衡、安全、動態回應處理等;比如/api/user轉發到到user服務,/api/order轉發到到shop服務,zuul默認使用Ribbon實作負載均衡;
一、ZUUL的簡單使用
1.1、創建ZUUL模塊,添加依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
1.2、創建組態檔,增加配置
server: port: 8081 spring: application: name: zuul # 配置Eureka地址 eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true # 構建路由地址 zuul: prefix: /api routes: # 這里可以自定義 user: # 匹配的路由規則 path: /user/** # 路由的目標服務名 serviceId: user order: # 匹配的路由規則 path: /order/** # 路由的目標服務名 serviceId: order
1.3、啟動類增加 @EnableZuulProxy 注解
@SpringBootApplication @EnableZuulProxy @EnableEurekaClient public class application { public static void main(String[] args) { SpringApplication.run(application.class); } }
完成
1.4、運行測驗


可以看到,ZUUL已經按照組態檔的路由規則進行了路由轉發,并對服務實作了負載均衡;
ZUUL簡單使用完畢;
GITdemo地址:https://github.com/anson-yang/springclouddemo
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/22634.html
標籤:架構設計
上一篇:spring cloud微服務快速教程之(四)熔斷器(Hystrix)及其工具(Dashboard、Turbine)
