Zull
概述
什么是zuul?
Zull包含了对请求的路由(用来跳转的)和过滤两个最主要功能:
其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础,而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验,服务聚合等功能的基础。Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
注意:Zuul服务最终还是会注册进Eureka
提供:代理+路由+过滤 三大功能!
Zuul能干嘛?
路由
过滤
官方文档:https://github.com/Netflix/zuul/
入门案例
新建springcloud-zuul模块,并导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
配置yml
server:
port: 9527
spring:
application:
name: springcloud-zuul
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/, http://localhost:7002/eureka/
instance:
instance-id: zuul-9527.com
prefer-ip-address: true #显示id
info:
app.name: xuyuan-springcloud
company.name: blog.xuyuanstuday.com
zuul:
routes:
mydept.serviceId: SPRINGCLOUD-DEPT-PROVIDER
mydept.path: /mydept/**
ignored-services: "*" # 隐藏全部的微服务名称,也就是不能通过微服务名称去访问了,只能通过前缀/path去访问
prefix: /xuyuan # 如果想要的话还可以添加前缀
# 如果想要的话,还可以去到hosts文件中添加一个127.0.0.1的路径,这样更加贴近真实的网站地址,当然这里就不试了,还是使用localhost
编写主启动类,并开启zuul代理
@SpringBootApplication
@EnableZuulProxy 有的加不了
public class ZuulApplication_9527{
public static void main(String[] args) {
SpringApplication.run(ZuulApplication_9527.class, args);
}
}