一、簡述
Zuul路由包含了對請求的路由和過濾兩個功能。
路由:路由功能負責將外部請求轉發到具體的微服務實例上,是實現外部訪問統一入口;
過濾:過濾器功能則負責對請求的處理過程進行干預,是實現請求校驗、服務聚合等功能的基礎。
Zuul和Eureka進行整合,將Zuul自身註冊爲Eureka服務治理下的應用,同時從Eureka中獲得其他微服務的消息,也即以後的訪問微服務都是通過Zuul跳轉後獲得。Zuul服務最終也會註冊進Eureka。
二、配置zuul
1.新建microservice-zuul
pom:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.syr.springcloud</groupId>
<artifactId>microservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservice-zuul</artifactId>
<dependencies>
<!-- zuul路由網關 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<!-- 熱部署插件 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
application.yml:
server:
port: 80
spring:
application:
name: microservice-zull
eureka:
client:
service-url:
defaultZone: http://eureka2001.com:2001/eureka/,http://eureka2002.com:2002/eureka/,http://eureka2003.com:2003/eureka/
instance:
instance-id: microservice-zull #自定義服務名稱信息
prefer-ip-address: true #訪問路徑可以顯示IP地址
info:
app.name: microservice-zull
company.name: www.syr.com
build.artifactId: $project.artifactId$
build.version: $project.version$
開啓zuul:
@SpringBootApplication
@EnableZuulProxy
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2.修改消費者
因爲上次創建的服務消費者沒有啓動eureka註冊,現在需要把consumer也註冊到eureka中,以便zuul路由訪問。
把microservice-consumer-feign-hystrix8080配置文件修改如下:(啓動服務註冊、增加服務名稱)
server:
port: 8080
spring:
application:
name: microservice-consumer
eureka:
instance:
instance-id: microservice-consumer-feign-hystrix
register-with-eureka: true
client:
service-url:
defaultZone: http://eureka2001.com:2001/eureka/,http://eureka2002.com:2002/eureka/,http://eureka2003.com:2003/eureka/
feign:
hystrix:
enabled: true
3.測試
啓動eureka集羣、provider服務集羣、microservice-consumer-feign-hystrix8080服務消費者、microservice-zuul路由。
(1)訪問consumer成功:
http://localhost:8080/consumer/users
(2)現需要通過路由訪問,路由ip地址爲:localhost:80
consumer服務註冊在eureka的服務名稱爲:microservice-consumer
所以,通過路由訪問consumer的路徑應爲:http://localhost/microservice-consumer/consumer/users
測試成功:
4.設置服務代理名稱
上面的url暴露了服務的真實名稱“microservice-consumer”,這裏可以設置一個別名:myconsumer
增加配置:
zuul:
routes:
mydept.serviceId: microservice-consumer
mydept.path: /myconsumer/**
5.忽略帶真實服務名的請求
別名設置成功後,真實服務名仍可以訪問,這裏應該把除別名外的url屏蔽掉,使其不能訪問:
zuul:
ignored-services: "*" #microservicecloud-provider
routes:
mydept.serviceId: microservice-consumer
mydept.path: /myconsumer/**
"*"代表所有
6.設置訪問前綴
zuul:
prefix: /MyDemo
ignored-services: "*" #microservicecloud-provider
routes:
mydept.serviceId: microservice-consumer
mydept.path: /myconsumer/**