⒈Zuul是什么?
Zuul包含了两个最主要的功能,对请求的路由和过滤。其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础。过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等动能的基础。
Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其它微服务的消息,也就是说,以后访问微服务都是通过Zuul跳转后获得。Zuul服务最终还是会注册进Eureka。
⒉示例
①新建路由项目,添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
②配置文件(yml)
spring:
application:
name: user-zuul
eureka:
instance:
prefer-ip-address: true #主机ip是否显示
client:
service-url:
defaultZone: http://localhost:8761/eureka/
③主程序启动类添加@EnableZuulProxy注解
package cn.coreqi; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication
@EnableZuulProxy
public class SpringbootcloudzuulApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootcloudzuulApplication.class, args);
} }
④访问http://localhost:9527/user-provider/users http://localhost:9527:网关 user-provider:微服务名称 users:调用
⑤★配置文件高级设置
server:
port: 9527
spring:
application:
name: user-zuul
eureka:
instance:
prefer-ip-address: true #主机ip是否显示
client:
service-url:
defaultZone: http://localhost:8761/eureka/
zuul:
ignored-services: "*" #禁用所有真实微服务地址访问
#ignored-services: user-provider #禁用单个的真实微服务地址访问
prefix: /coreqi #统一访问公共前缀
routes:
user.serviceId: user-provider
user.path: /test/** #微服务域名映射
⑥访问http://localhost:9527/coreqi/test/users