zuul包含了对请求的路由和过滤两个最主要的功能:
其中路由功能复杂将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础,而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验,服务聚合等功能的基础,Zuul和Eureka进行整个,将zuul自身注册为eureka服务治理下的应用,同时从eureka中获得其他微服务的消息,也即以后的访问微服务都是通过zuul跳转后获得;
注意:Zuul服务最早还是会注册进Eureka
4.0.0
com.yt.springcloud
microservicecloud
0.0.1-SNAPSHOT
microservicecloud-zuul-gateway-9527
org.springframework.cloud
spring-cloud-starter-zuul
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-hystrix
org.springframework.cloud
spring-cloud-starter-config
com.yt.springcloud
microservicecloud-api
${project.version}
org.springframework.boot
spring-boot-starter-jetty
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework
springloaded
org.springframework.boot
spring-boot-devtools
server:
port: 9527
spring:
application:
name: microservicecloud-zuul-gateway #注册eureka的名字
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 #改hosts文件
prefer-ip-address: true
info: #可写可不写
app.name: atguigu-microcloud
company.name: www.atguigu.com
build.artifactId: $project.artifactId$
build.version: $project.version$
hosts文件的位置在C:\Windows\System32\drivers\etc\hosts
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
127.0.0.1 myzuul.com
package com.yt.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);
}
}
网关和微服务注册进入eureka集群:http://localhost:7001/
不使用路由:http://localhost:8001/dept/get/1
使用路由:http://myzuul.com:9527/microservicecloud-dept/dept/get/1
microservicecloud-dept是微服务名称
server:
port: 9527
spring:
application:
name: microservicecloud-zuul-gateway #注册eureka的名字
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 #改hosts文件
prefer-ip-address: true
zuul:
routes:
mydept.serviceId: microservicecloud-dept #key 以前是输入微服务名称
mydept.path: /mydept/** #value 现在是映射到mydept(把真实微服务名称隐藏起来)
info: #可写可不写
app.name: atguigu-microcloud
company.name: www.atguigu.com
build.artifactId: $project.artifactId$
build.version: $project.version$
server:
port: 9527
spring:
application:
name: microservicecloud-zuul-gateway #注册eureka的名字
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 #改hosts文件
prefer-ip-address: true
zuul:
ignored-services: microservicecloud-dept
routes:
mydept.serviceId: microservicecloud-dept #key 以前是输入微服务名称
mydept.path: /mydept/** #value 现在是映射到mydept(把真实微服务名称隐藏起来)
info: #可写可不写
app.name: atguigu-microcloud
company.name: www.atguigu.com
build.artifactId: $project.artifactId$
build.version: $project.version$
server:
port: 9527
spring:
application:
name: microservicecloud-zuul-gateway #注册eureka的名字
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 #改hosts文件
prefer-ip-address: true
zuul:
#ignored-services: microservicecloud-dept 具体某一个微服务的名称
ignored-services: "*" #所有微服务,批量处理
routes:
mydept.serviceId: microservicecloud-dept #key 以前是输入微服务名称
mydept.path: /mydept/** #value 现在是映射到mydept(把真实微服务名称隐藏起来)
info: #可写可不写
app.name: atguigu-microcloud
company.name: www.atguigu.com
build.artifactId: $project.artifactId$
build.version: $project.version$
server:
port: 9527
spring:
application:
name: microservicecloud-zuul-gateway #注册eureka的名字
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 #改hosts文件
prefer-ip-address: true
zuul:
#ignored-services: microservicecloud-dept 具体某一个微服务的名称
prefix: /yt #统一前缀名
ignored-services: "*" #所有微服务,批量处理
routes:
mydept.serviceId: microservicecloud-dept #key 以前是输入微服务名称
mydept.path: /mydept/** #value 现在是映射到mydept(把真实微服务名称隐藏起来)
info: #可写可不写
app.name: atguigu-microcloud
company.name: www.atguigu.com
build.artifactId: $project.artifactId$
build.version: $project.version$
翻译于尚硅谷周阳老师:https://www.bilibili.com/video/av22613028?from=search&seid=11377756339718254280