ZUUL入門代碼

ZUUL入門代碼

在這裏插入圖片描述
zuul 是netflix開源的一個API Gateway 服務器, 本質上是一個web servlet應用。
Zuul 在雲平臺上提供動態路由,監控,彈性,安全等邊緣服務的框架。Zuul 相當於是設備和 Netflix 流應用的 Web 網站後端所有請求的前門。
在這裏插入圖片描述
無論是來自於客戶端(PC或移動端)的請求,還是服務內部調用。一切對服務的請求都會經過Zuul這個網關,然後再由網關來實現 鑑權、動態路由等等操作。Zuul就是我們服務的統一入口。
1、添加依賴

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

2、啓動類

@SpringBootApplication
@EnableZuulProxy
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }

}

3、配置信息

server:
  port: 10010
spring:
  application:
    name: gateway

4、路由規則

zuul:
  routes:
    user-service:  #路由id,自定義名稱
      path: /user-service/** #映射路徑
      url: http://127.0.0.1:8081   #映射路徑實際對應的url 

5、面向服務的路由
添加依賴

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

啓動類

@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

編寫配置文件

server:
  port: 10010
spring:
  application:
    name: gateway
zuul:
  routes:
    user-service:
      path: /user-service/**
      serviceId: user-service
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka

將以上的url,更改爲serviceId,不再面向於IP地址,更改爲面向於服務,通過Eureka自動查找對應的服務名稱,底層會應用負載均衡,實現服務的調用。

簡化路由配置

server:
  port: 10010
spring:
  application:
    name: gateway
zuul:
  routes:
    user-service: /user/**
  ignored-services:
    - user-consumer
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章