1. 程式人生 > >六、SpringCloud五大神獸之Zuul

六、SpringCloud五大神獸之Zuul

      路由在微服務體系結構的一個組成部分。例如,/可以對映到您的Web應用程式,/api/users對映到使用者服務,/api/shop對映到商店服務。Zuul是Netflix的基於JVM的路由器和伺服器端負載均衡器。

快速使用

1、在前面建立基礎上新建一個Module,這裡命名microservicecloud-zuul-gateway-9527,然後匯入相關依賴:

<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.zhanghf</groupId>
            <artifactId>microservisecloud-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>

2、配置檔案新增路由等相關配置

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  #忽略microservicecloud-dept這個服務名,即用http://gateway-9527.com:9527/microservicecloud-dept/dept/get/2訪問無效
#  prefix: /atguigu #字首
#  ignored-services: "*" #忽略所有的服務名
  routes: 
    mydept.serviceId: microservicecloud-dept   #對映到的服務名
    mydept.path: /mydept/**  #可以用mydept替代microservicecloud-dept,即通過http://gateway-9527.com:9527/microservicecloud-dept/dept/get/2和http://gateway-9527.com:9527/mydept/dept/get/2同等
 


路由相關配置說明如上述配置註解,一個特殊的地方在於上述配置中通過域名進行匹配,因此需要在本機hosts檔案中做好域名對映127.0.0.1  gateway-9527.com

3、啟動類新增相關啟動註解@EnableZuulProxy

package com.zhanghf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

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

測試:

依次啟動7001,7002,7003,8001和9527

microservicecloud-dept服務名中,同時也可以通過http://gateway-9527.com:9527/microservicecloud-dept/dept/get/2來進行訪問,效果一樣。

如果需要隱藏原微服務,則看上述配置檔案註釋掉的程式碼 ignored-services: microservicecloud-dept

新增字首則:prefix: /atguigu #字首