本系列對應(yīng)的是尚硅谷周陽Spring Cloud的思維導(dǎo)圖整理的筆記,用來方便自己后面的知識點(diǎn)回顧荣病。分別以每個知識點(diǎn)作為一篇文章詳細(xì)講述悼做。
知識點(diǎn)傳送門:
項(xiàng)目源碼
- SpringCloud01:微服務(wù)概述與SpringCloud
- SpringCloud02:Rest微服務(wù)構(gòu)建案例工程模塊
- SpringCloud03:Eureka服務(wù)注冊與發(fā)現(xiàn)
- SpringCloud04:Ribbon負(fù)載均衡
- SpringCloud05:Feign負(fù)載均衡
- SpringCloud06:Hystrix斷路器
- SpringCloud07:zuul路由網(wǎng)關(guān)
- SpringCloud08:SpringCloud Config分布式配置中心
一间学、概述
1.是什么
Zuul包含了對請求的路由和過濾兩個最主要的功能:
其中路由功能負(fù)責(zé)將外部請求轉(zhuǎn)發(fā)到具體的微服務(wù)實(shí)例上淹办,是實(shí)現(xiàn)外部訪問統(tǒng)一入口的基礎(chǔ)而過濾器功能則負(fù)責(zé)對請求的處理過程進(jìn)行干預(yù),是實(shí)現(xiàn)請求校驗(yàn)械哟、服務(wù)聚合等功能的基礎(chǔ).
Zuul和Eureka進(jìn)行整合疏之,將Zuul自身注冊為Eureka服務(wù)治理下的應(yīng)用,同時從Eureka中獲得其他微服務(wù)的消息暇咆,也即以后的訪問微服務(wù)都是通過Zuul跳轉(zhuǎn)后獲得锋爪。
注意:Zuul服務(wù)最終還是會注冊進(jìn)Eureka
提供=代理+路由+過濾三大功能
二、路由基本配置
1. 新建Module模塊microservicecloud-zuul-gateway-9527
2.修改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.atguigu.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容錯-->
<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.atguigu.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>
3.修改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: atguigu-microcloud
company.name: www.atguigu.com
build.artifactId: $project.artifactId$
build.version: $project.version$
4.hosts修改
127.0.0.1 myzuul.com
5.主啟動類
package com.atguigu.springcloud;
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);
}
}
6.啟動
- 三個eureka集群
- 一個服務(wù)提供類microservicecloud-provider-dept-8001
- 一個路由
7. 測試
不用路由: http://localhost:8001/dept/get/2
啟用路由: http://myzuul.com:9527/microservicecloud-dept/dept/get/2
三糯崎、路由訪問映射規(guī)則
1.工程microservicecloud-zuul-gateway-9527
2.代理名稱
問題:
路由訪問OK:http://myzuul.com:9527/mydept/dept/get/1
原路徑訪問OK:http://myzuul.com:9527/microservicecloud-dept/dept/get/2
3.原真實(shí)服務(wù)名忽略
修改YML文件
單個服務(wù)
zuul:
ignored-services: microservicecloud-dept
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
多個服務(wù)
zuul:
ignored-services: "*"
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
4.設(shè)置統(tǒng)一公共前綴
zuul:
prefix: /atguigu
ignored-services: "*"
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
訪問地址 http://myzuul.com:9527/atguigu/mydept/dept/get/1
5.最后YML
server:
port: 9527
spring:
application:
name: microservicecloud-zuul-gateway
zuul:
prefix: /atguigu
ignored-services: "*"
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
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: atguigu-microcloud
company.name: www.atguigu.com
build.artifactId: $project.artifactId$
build.version: $project.version$