Zuul路由网关

1.简介

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

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

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

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

2.Zuul的作用

  • 路由

  • 过滤

3.Zuul整合Eureka

前提:基于负载均衡及Ribbon这一边文章中的Ribbon实战项目的基础上添加内容

  • 创建一个Modules,名称为zuul-9527,导入zuul和eureka以及actuator的依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--zuul-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
</dependencies>
  • 编写配置文件
server.port=9527
spring.application.name=zuul-9527
#eureka配置
#不在eureka中注册自己
eureka.client.register-with-eureka=true
#服务地址
eureka.client.service-url.defaultZone=http://localhost:7001/eureka/
#实例的id
eureka.instance.instance-id=springcloud-zuul-9527
#暴露实例的ip地址
eureka.instance.prefer-ip-address=true
#配置info的信息
info.app.name=zuul-9527
info.company.name=com.zixin.springcloud
  • 在src/main/java目录下创建com.zixin.springcloud包,并在该包下创建启动类Zuul_9527,同时添加@EnableZuulProxy注解,用于开启路由
package com.zixin.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
//开启路由
@EnableZuulProxy
@SpringBootApplication
public class Zuul_9527 {
public static void main(String[] args) {
SpringApplication.run(Zuul_9527.class, args);
}
}
  • 运行provider-dept-8001、eureka-server-7001以及zuul-9527项目,访问http://localhost:7001/,可以看到两个服务都注册到eureka注册中心上,如下图所示

  • 正常访问http://localhost:8001/provider/dept/findDeptById?deptno=1,可以访问到数据

  • 访问http://localhost:9527/provider-dept/provider/dept/findDeptById?deptno=1,也可以访问到数据

  • 向配置文件application.properties中添加zuul-router的配置信息

#自定义服务id,zixin-router只是给路由一个名称,可以随便命名
zuul.routes.zixin-router.service-id=provider-dept
#zixin-router对应的路径
zuul.routes.zixin-path=/diy-router/**
  • 重新运行zuul-9527项目,访问http://localhost:9527/zixin-router/provider/dept/findDeptById?deptno=1,可以获取到数据

  • 但此时通过访问http://localhost:9527/provider-dept/provider/dept/findDeptById?deptno=1,也同样可以拿到数据,这是不希望的,这里在配置文件中配置忽略代理该服务即可

#忽略代理provider-dept服务
zuul.ignored-services=provider-dept
  • 重新运行zuul-9527项目,访问http://localhost:9527/provider-dept/provider/dept/findDeptById?deptno=1,获取不到数据

访问http://localhost:9527/zixin-router/provider/dept/findDeptById?deptno=1,可以获取到数据,从而达到我们需要的效果

posted @   RBAZX  阅读(50)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示