什么是Zuul?
Zuul 包含了对请求的路由和过滤两个最主要的功能:
路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础过滤功能负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础Zuul 和Eureka进行整合,将Zuul自身注册为 Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
总之:Zuul服务最终仍会注册到Eureka;提供代理、路由、过滤三大功能。
1、导入依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> <version>1.4.7.RELEASE</version> </dependency>2、编写配置信息:
server: port: 9527 eureka: client: service-url: defaultZone: http://localhost:7001/eureka/ instance: instance-id: zuul-9527.com prefer-ip-address: true # 可以显示服务的ip地址 info: app.name: xingyu-springcloud company.name: xingyu-XJTU zuul: routes: mydept.serviceId: springcloud-provider-dept mydept.path: /mydept/** ignored-services: "*" #不能使用该路径进行访问,忽略 prefix: /xingyu #设置公共前缀3、主启动类中开启Zuul,使用注解@EnableZuulProxy:
@SpringBootApplication @EnableZuulProxy public class ZuulApplcation9527 { public static void main(String[] args) { SpringApplication.run(ZuulApplcation9527.class, args); } }4、启动Zuul,之后客户端的所有访问都可以通过Zuul(端口9527)进行
