SpringCloud 進階之Zuul(路由網關)


1. Zuul(路由網關)

  • Zuul 包含了對請求的路由和過濾兩個最主要的功能;
    • 路由功能:負責將外部請求轉發到具體的微服務實例上,是實現外部訪問統一入口的基礎;
    • 過濾功能:負責對請求的處理過程進行干預,是實現請求校驗,服務聚合等功能的基礎;
  • Zuul 服務最終還是會注冊進Eureka;

1.1 路由基本配置

新建 microservicecloud-zuul-gateway-9527

// pom.xml
<!-- 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>


// application.yml
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

  info:
    app.name: noodles-microcloud
    company.name: www.google.com
    build.artifactId: $project.artifactId$
    build.version: $project.version$


// hosts 修改: 127.0.0.1   myzuul.com

// 主啟動類
@SpringBootApplication
@EnableZuulProxy
public class Zuul_9527_StartSpringCloudApp {

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


// 啟動
// 三個Eureka集群
// microservicecloud-provider-dept-8001
// 路由

// 測試訪問:
// 不用路由: http://localhost:8001/dept/get/1
// 使用路由: http://myzuul.com:9527/microservicecloud-dept/dept/get/1

1.2 Zuul 路由訪問映射規則

// microservicecloud-zuul-gateway-9527
// 修改 application.yml
zuul:
  ignored-services: microservicecloud-dept      # 將原有路由關閉
  routes:
    prefix: /test       # 設置統一公共前綴, 訪問地址:http://myzuul.com:9527/test/mydept/dept/get/1
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**


// 修改之前,訪問地址: http://myzuul.com:9527/microservicecloud-dept/dept/get/1
// 修改之后,訪問地址: http://myzuul.com:9527/mydept/dept/get/1

參考資料:


免責聲明!

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



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