1,zuul的maven配置

<!--spring cloud 相关包-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <!-- zuul依赖-->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-zuul</artifactId>

</dependency>

2,静态路径配置

比如现在我有两个服务,一个服务叫gate 即zuul 网关,一个服务叫service-a ,是网关后面的一个服务,这里面service-a即是:

application.yml

spring:
application:
name:service-a

路由配置1:

zuul:
routes:
service-a: /node/**

这个意思是,gate 收到一个浏览器的请求,比如:http://localhost:8080/node/student/getStudents,那么就会转发给service-a,注意service-a提供的接口不能带node了,应该是:

@RestController
@RequestMapping("stduent")
public class TestController { @RequestMapping("getStudents")
public Object getStudents() { return null;
}
}

3,动态配置路由规则

有时候,我们会经常添加一些新的路由规则,每次静态添加不仅多而且麻烦,还会重新启动网关,这时就需要动态配置路由规则了,可以使用代码实现。

在zuul中,默认使用的路径类是:SimpleRouteLocator.java

在它的bean配置类:ZuulServerAutoConfiguration.java中是这样配置的

@Bean
@ConditionalOnMissingBean(SimpleRouteLocator.class)
public SimpleRouteLocator simpleRouteLocator() {
return new SimpleRouteLocator(this.server.getServlet().getServletPrefix(),
this.zuulProperties);
}

它表示当没有此类型SimpleRouteLocator.class的实现时,使用这个bean,所以我们要实现自己的路由配置,只需要重新实现相关的方法即可。

首先实现一个路由规则加载类

import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.RefreshableRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute; /**
*
* @ClassName: LogServerRouterFilter
* @Description: 日志请求相关路由分发到指定的服务器
* @author: wgs
* @date: 2018年12月18日 下午3:06:00
*/
public class LogServerRouteLocator extends SimpleRouteLocator implements RefreshableRouteLocator {
@Autowired
private ServerConfigService serverConfigService;
public LogServerRouteLocator(String servletPath, ZuulProperties properties) {
super(servletPath, properties); } @Override
public void refresh() {
doRefresh();
}
//覆盖这个方法,从重实现它
@Override
protected Map<String, ZuulProperties.ZuulRoute> locateRoutes() {
//重新定义一个路由映射map
LinkedHashMap<String, ZuulProperties.ZuulRoute> routesMap = new LinkedHashMap<>();
//把父类中的映射继承下来,它主要是从配置文件中取的映射。
routesMap.putAll(super.locateRoutes());
//这里的路由信息来自于配置文件
for (Map.Entry<String, String> entry : serverConfigService.getGmNodes().entrySet()) {
String serverId = entry.getKey();
String serviceId = entry.getValue().toLowerCase();
String path = "/node/**";
ZuulRoute zuulRoute = new ZuulRoute();
//服务提供者的id,即spring.application.name
zuulRoute.setServiceId(serviceId);
//这个id是匹配的前半部分,比如匹配模式是/node/** 那么这个id就是/node
zuulRoute.setId("/node");
//匹配的路径
zuulRoute.setPath(path);
//这里注意一下,这个key就是要匹配的path,可以查看父类的实现,它就是使用path做为key的。
routesMap.put(path, zuulRoute);
}return routesMap;
} }

上面 ZuulRoute的创建路由规则和静态配置是等价的。

然配置Bean

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class BeanConfig {
@Autowired
ZuulProperties zuulProperties;
@Autowired
ServerProperties server; @Bean
public LogServerRouteLocator getRouteLocator() {
return new LogServerRouteLocator(this.server.getServlet().getServletPrefix(), this.zuulProperties);
} }


 欢迎添加学习交流QQ群:66728073,197321069,398808948

zuul 自定义路由规则的更多相关文章

  1. MVC系列——MVC源码学习:打造自己的MVC框架(三:自定义路由规则)

    前言:上篇介绍了下自己的MVC框架前两个版本,经过两天的整理,版本三基本已经完成,今天还是发出来供大家参考和学习.虽然微软的Routing功能已经非常强大,完全没有必要再“重复造轮子”了,但博主还是觉 ...

  2. CI 框架中的自定义路由规则

    在 CI 框架中,一个 URL 和它对应的控制器中的类以及类中的方法是一一对应的,如: www.test.com/user/info/zhaoyingnan 其中 user 对应的就是控制器中的 us ...

  3. 网关服务自定义路由规则(springcloud+nacos)

    1. 场景描述 需要给各个网关服务类提供自定义配置路由规则,实时生效,不用重启网关(重启风险大),目前已实现,动态加载自定义路由文件,动态加载路由文件中的路由规则,只需在规则文件中配置下规则就可以了 ...

  4. vs2017 mvc 自定义路由规则 出现 404.0 错误代码 0x80070002

    自定义: WebApiConfig  里面最后增加 config.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttp ...

  5. zuul 自定义路由映射规则

    zuul本射自动创建eureka中的服务的路由

  6. MVC 自定义路由规则

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mv ...

  7. Spring Cloud Zuul路由规则动态更新

    背景  Spring Cloud Zuul 作为微服务的网关,请求经过zuul路由到内部的各个service,由于存在着新增/修改/删除服务的路由规则的需求,zuul的路由规则的动态变更功能 提供了 ...

  8. MVC之路由规则 (自定义,约束,debug)

    自定义路由规则的要求,小范围写在前,大范围写在后.路由规则可以注册多条,路由规则的名称不能重复路由规则有顺序,并且按照顺序进行匹配,建议小范围写在前,大范围写在后.路由规则可以设置约束 即正则表达式路 ...

  9. Spring Cloud(Dalston.SR5)--Zuul 网关-路由配置

    Spring Cloud 在 Zuul 的 routing 阶段实现了几个过滤器,这些过滤器决定如何进行路由工作. 简单路由(SimpleHostRoutingFilter) 该过滤器运行后,会将 H ...

随机推荐

  1. ubantu中执行docker免sudo方法

    1.添加用户组,如果已存在则不用设置. sudo groupadd docker 2.将用户加入该 group (docker)内 sudo gpasswd -a ${USER} docker 3.重 ...

  2. 堡垒机jumpserver测试记录--安装

    一步一步安装(CentOS) 基本都是官网的东西,只是有些坑做了记录. http://docs.jumpserver.org/zh/docs/step_by_step.html 环境 系统: Cent ...

  3. SpringCloud教程 | 第三篇: 服务消费者(Feign)

    上一篇文章,讲述了如何通过RestTemplate+Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务.一.Feign简介 Feign是一个声明式的伪Http客户端,它使得写Http ...

  4. Python requests--初识接口自动化

    requests模块初级宝典:http://docs.python-requests.org/zh_CN/latest/user/quickstart.htmlrequests模块之葵花宝典:http ...

  5. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习8

    #include <iostream>#include <string>using namespace std;int main (){ string words; int i ...

  6. Python(一)缺点

    (一)慢~~~ (二)Python不能加密

  7. Oracle 11g修改字符集

    选择静默安装的安装字符集为默认的ZHS16GBK,工作中字符集为为AL32UTF8 一.登录oracle sqlplus / as sysdba shutdown immediate; STARTUP ...

  8. Junit/idea Junit支持/Spring test之间的孽世纠葛

    最近应老板要求,研究研究Spring测试相关的东西,力求搞一个方便使用的测试工具,对于一个Spring不熟Junit不懂的人这是一个很大的坑,扫了一边spring test文档没感觉有什么收获,spr ...

  9. 邮局 100分代码(dfs+多重剪枝)

    蓝桥杯真题-邮局 #include<iostream> #include<algorithm> #include<set> #include<string&g ...

  10. Windows 主机名映射地址

    在开发中大数据集群中我们自己的电脑主机名映射不到集群的主机名下面我们就去修改自己电脑 主机名映射地址 c/Windows/System32/drivers/etc/host   文件将主机名和IP地址 ...