一、概念
Zuul包含了对请求的路由和过滤两个最主要的功能:
其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.
Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
注意:Zuul服务最终还是会注册进Eureka
二、引入相关jar
<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>
三、修改全局配置文件
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: zhq-microcloud
company.name: www.zhq.com
build.artifactId: $project.artifactId$
build.version: $project.version$
四、修改主启动类
@SpringBootApplication
@EnableZuulProxy//开启Zuul代理
public class Zuul_9527_StartSpringCloudApp
{
public static void main(String[] args)
{
SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args);
}
}
启动项目测试,访问规则:http://myzuul.com(项目域名或者ip):9527/microservicecloud-dept(微服务名称)/dept/get/2
五、配置不暴露真实微服务名称
添加全局配置:
zuul:
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
你会发现:无论是http://myzuul.com:9527/mydept/dept/get/1还是http://myzuul.com:9527/microservicecloud-dept/dept/get/2都可以访问到,都达不到隐藏微服务microservicecloud-dept的目的
修改配置zuul.ignored-services忽略真实微服务名称:
zuul:
ignored-services: microservicecloud-dept
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
单个具体,多个可以用"*"
zuul:
ignored-services: "*"
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
六、配置统一的公共访问前缀
添加配置zuul.prefix
zuul:
prefix: /zhq
ignored-services: "*"
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**