1. Zuul(路由网关)
- Zuul 包含了对请求的路由和过滤两个最主要的功能;
- 路由功能:负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础;
- 过滤功能:负责对请求的处理过程进行干预,是实现请求校验,服务聚合等功能的基础;
- Zuul 服务最终还是会注册进Eureka;
1.1 路由基本配置
新建 microservicecloud-zuul-gateway-9527
// pom.xml
<!-- zuul 路由网关 -->
<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>
// 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
info:
app.name: noodles-microcloud
company.name: www.google.com
build.artifactId: $project.artifactId$
build.version: $project.version$
// hosts 修改: 127.0.0.1 myzuul.com
// 主启动类
@SpringBootApplication
@EnableZuulProxy
public class Zuul_9527_StartSpringCloudApp {
public static void main(String[] args) {
SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args);
}
}
// 启动
// 三个Eureka集群
// microservicecloud-provider-dept-8001
// 路由
// 测试访问:
// 不用路由: http://localhost:8001/dept/get/1
// 使用路由: http://myzuul.com:9527/microservicecloud-dept/dept/get/1
1.2 Zuul 路由访问映射规则
// microservicecloud-zuul-gateway-9527
// 修改 application.yml
zuul:
ignored-services: microservicecloud-dept # 将原有路由关闭
routes:
prefix: /test # 设置统一公共前缀, 访问地址:http://myzuul.com:9527/test/mydept/dept/get/1
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
// 修改之前,访问地址: http://myzuul.com:9527/microservicecloud-dept/dept/get/1
// 修改之后,访问地址: http://myzuul.com:9527/mydept/dept/get/1
参考资料: