Zuul路由網(wǎng)關(guān)
概述
Zuul包含了對(duì)請(qǐng)求的路由和過(guò)濾兩個(gè)主要的功能辰如,其中路由的功能是負(fù)責(zé)將外部請(qǐng)求轉(zhuǎn)發(fā)到具體的微服務(wù)實(shí)例上,是實(shí)現(xiàn)外部訪問統(tǒng)一入口的基礎(chǔ)而過(guò)濾功能是負(fù)責(zé)對(duì)請(qǐng)求的處理過(guò)程進(jìn)行干預(yù)褐奥,是實(shí)現(xiàn)請(qǐng)求校驗(yàn)杆故,服務(wù)聚合等功能的基礎(chǔ)洛二,Zuul和Eureka進(jìn)行整合舱沧,將Zuul自身注冊(cè)近Eureka服務(wù)治理的應(yīng)用妹沙,同時(shí)從Eureka中獲取其他微服務(wù)的消息,也及時(shí)以后的訪問服務(wù)都是通過(guò)Zuul跳轉(zhuǎn)后獲得熟吏, 注意的是Zuul服務(wù)最終還是會(huì)注冊(cè)近Eureka中
路由基本配置
新建項(xiàng)目microservicecloud-zuul-gateway-9527距糖,添加依賴如下
pom.xml文件
<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.luo.springcloud</groupId>
<artifactId>microservicecloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservicecloud-zuul-gateway-9527</artifactId>
<dependencies>
<!-- zuul路由網(wǎng)關(guān) -->
<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監(jiān)控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- hystrix容錯(cuò) -->
<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>
<!-- 日常標(biāo)配 -->
<dependency>
<groupId>com.luo.springcloud</groupId>
<artifactId>microservicecloud-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>
</project>
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
zuul:
#ignored-services: microservicecloud-dept
prefix: /luo
ignored-services: "*"
info:
app.name: luo-microcloud
company.name: www.luo.com
build.artifactId: $project.artifactId$
build.version: $project.version$
修改host文件
127.0.0.1 myzuul.com
主啟動(dòng)類Zuul_9527_StartSpringCloudApp
@SpringBootApplication
@EnableZuulProxy
public class Zuul_9527_StartSpringCloudApp {
public static void main(String[] args) {
SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args);
}
}
啟動(dòng)
三個(gè)集群,一個(gè)服務(wù)提供類microservicecloud-provider-dept-8001牵寺,一個(gè)路由
測(cè)試
不使用路由:http://localhosat:8001/dept/get/2
使用路由:http://myzuul.com:9527/microservicecloud-dept/dept/get/2
Zuul路由訪問映射
在前面的測(cè)試中我們可以使用http://myzuul.com:9527/microservicecloud-dept/dept/get/2訪問我們的接口悍引,這樣就暴露我們的微服務(wù)名稱,需要做安全加固帽氓,就用到了路由訪問映射趣斤,修改路由項(xiàng)目的yml文件,添加 mydept.path: /mydept/**
zuul:
#ignored-services: microservicecloud-dept #忽略真實(shí)地址,只讓虛擬地址訪問
prefix: /luo #訪問地址前綴
ignored-services: "*"#忽略真實(shí)地址杏节,只讓虛擬地址訪問
routes:
mydept.serviceId: microservicecloud-dept ##真實(shí)地址
mydept.path: /mydept/** # 虛擬地址