SpringCloud07-Zuul 路由网关

Zuul 路由网关

什么是Zuul

Zuul包含了对请求的路径和过滤两个最主要的功能:

其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础,而过滤器功能则负责对请求的处理过程进行干预,实现请求校验,服务聚合等功能的基础。Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获取其他的服务的消息,也即以后的访问微服务都是通过Zuul跳转获得的。

注意:Zuul服务最终还是会注册到Eureka

提供:代理+路由+过滤三大功能

Zuul能干嘛

  • 路由(不能通过真实的地址访问,必须得通过自己设定的路由来访问)
  • 过滤

举例

​ 1)导入依赖

<!--Zuul-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zuul</artifactId>
    <version>1.4.6.RELEASE</version>
</dependency>

​ 2)编写配置文件

server:
  port: 9527
spring:
  application:
    name: springcloud-zuul

eureka:
  client:
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: zuul9527.xom

    prefer-ip-address: true   #隐藏ip地址
  
 

​ 3)开启主启动类@Enable~

@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication_9527 {
    public static void main(String[] args) {
        SpringApplication.run(ZuulApplication_9527.class,args);
    }
}

​ 4)运行

可以在本机更改一下虚拟域名。这样的话我们的服务器的名称就显示出来了,我们可以更改一下服务器的别名。


 # 更改路由的ip地址的服务名
Zuul:
  routes:
    mydept.serviceId: springcloud-privider-dept
    mydept.path: /mydept/**
  #ignored-services: springcloud-privider-dept   # 忽略原有的路径,即不能使用它请求了
  ignored-services: "*"       # 忽略全部原有的路径,即不能使用它请求了
 


还可以通过前缀

 prefix: /mjh    #设置公共的前缀

posted @ 2020-07-28 18:49  林森001  阅读(87)  评论(0编辑  收藏  举报