使用指定的URL,使用负载均衡,你可以使用regexmapper,在serviceId和路由之间,提供一个约定,他使用正则表达式,命名组,去将serviceId的变量,注入到router pattern里面去,这里其实提到了几个概念,regexmapper,一个叫variables from serviceId,还有一个叫route pattern,直接讲概念可能比较抽象You can provide a convention between serviceId and routes by using regexmapper. It uses regular-expression named groups to extract variables from serviceId and inject them into a route pattern, as shown in the following example:microservice-gateway-zuul-reg-exp让他成为一个最简单的zuulhttps://cloud.spring.io/spring-cloud-netflix/reference/html/#_zuul_http_client@Bean
public PatternServiceRouteMapper serviceRouteMapper() {return new PatternServiceRouteMapper("(?<name>^.+)-(?<version>v.+$)","${version}/${name}");
}我们看一下这个代码public PatternServiceRouteMapper(String servicePattern, String routePattern) {this.servicePattern = Pattern.compile(servicePattern);this.routePattern = routePattern;
}这里是servicePattern和routePattern,routePattern是什么,这边routePattern就解释了就是路由的表达式,路由的正则表达式,其实就是把servicePattern这个里面的变量,注入到routePattern这个里面去,这个就是所谓的PatternServiceRouteMapper,然后我们来分析一下,serviceId就是以"(?<name>^.+)-(?<version>v.+$)"这种表达式命名的,有一个name的变量,有一个version的变量,然后他会映射成什么样的映射路径呢,我们之前是没有version的我们把spring.application.name=microservice-simple-provider-user改成spring.application.name=microservice-simple-provider-user-v1localhost:8040/v1/microservice-simple-provider-user/simple/1就可以访问到用户微服务的这个东西,就相当于访问localhost:7900/simple/1这就说明我们已经实现了,serviceId of myusers-v1 is mapped to route /v1/myusers/**,spring.application.name=microservice-simple-provider-user-v1就是这个东西,代码servicePattern and routePattern这两个参数都得有,如果servicePattern和serviceId不匹配,假设我现在是这样的,microservice-simple-provider-user,根本不符合正则表达式,就是你这边写了和没写是一样的@Bean
public PatternServiceRouteMapper serviceRouteMapper() {return new PatternServiceRouteMapper("(?<name>^.+)-(?<version>v.+$)","${version}/${name}");
}
#debug=true
server.port=7900#server.context-path=/boot02spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=truelogging.level.com.learn=trace
#logging.file=D:/springboot.log
logging.file=springboot.log
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd} ==== [%thread] %-5level ==== %logger{50} ==== %msg%n
#spring.resources.static-locations=classpath:/hello,classpath:/learnspring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://59.110.158.145:3306/SpringCloud?useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xmleureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eurekaspring.application.name=microservice-simple-provider-user-v1
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
#eureka.client.healthcheck.enabled=true
spring.redis.host=10.40.8.152
spring.redis.password=1234
spring.redis.port=6379#eureka.instance.appname=microservice-simple-provider-user
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn.cloud</groupId><artifactId>microservice-gateway-zuul-reg-exp</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties>    <dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId><version>1.4.2.RELEASE</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zuul</artifactId></dependency></dependencies><!-- 这个插件,可以将应用打包成一个可执行的jar包 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
server.port=8040
spring.application.name=microservice-gateway-zuul-reg-exp
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eureka
eureka.instance.appname=microservice-gateway-zuul-reg-exp
package com.learn.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.filters.discovery.PatternServiceRouteMapper;
import org.springframework.context.annotation.Bean;@EnableZuulProxy
@SpringBootApplication
public class ZuulApplication {public static void main(String[] args) {SpringApplication.run(ZuulApplication.class, args);}@Beanpublic PatternServiceRouteMapper serviceRouteMapper() {return new PatternServiceRouteMapper("(?<name>^.+)-(?<version>v.+$)","${version}/${name}");}
}

Zuul使用正则表达式指定路由规则相关推荐

  1. 【ASP.NET Core】给路由规则命名有何用处

    上一篇中老周给伙伴们介绍了自定义视图搜索路径的方法,本篇咱们扯一下有关 URL 路径规则的名称问题.在扯今天的话题之前,先补充点东东.在上一篇中设置视图搜索路径时用到三个有序参数:{2}{1}{0}, ...

  2. Dubbo 路由规则之条件路由

    Dubbo 路由规则之条件路由 前言 大家好,今天开始给大家分享 - Dubbo 专题之 Dubbo 路由规则之条件路由.在前一个章节中我们介绍了 Dubbo 令牌验证和优雅停机,以及我们也例举了常见 ...

  3. zuul动态配置路由规则,从DB读取

    前面已经讲过zuul在application.yml里配置路由规则,将用户请求分发至不同微服务的例子. zuul作为一个网关,是用户请求的入口,担当鉴权.转发的重任,理应保持高可用性和具备动态配置的能 ...

  4. thinkphp学习笔记10—看不懂的路由规则

    原文:thinkphp学习笔记10-看不懂的路由规则 路由这部分貌似在实际工作中没有怎么设计过,只是在用默认的设置,在手册里面看到部分,艰涩难懂. 1.路由定义 要使用路由功能需要支持PATH_INF ...

  5. Gateway-02-gateway路由规则和过滤器

    文章目录 1:gateway的Helloworld程序 1:设置order服务(普通服务) 2:设置user服务(普通服务) 3:设置gateway-server服务 1:导包 2:配置applica ...

  6. 示例Express中路由规则及获取请求参数

    本次给大家分享一篇基于express中路由规则及获取请求参数的方法,写的十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参考学习下.如有不足之处,欢迎批评指正. express中常见的路由规则 ...

  7. Knative 实战:如何在 Knative 中配置自定义域名及路由规则

    作者 | 元毅 阿里云智能事业群高级开发工程师 当前 Knative 中默认支持是基于域名的转发,可以通过域名模板配置后缀,但目前对于用户来说并不能指定全域名设置.另外一个问题就是基于 Path 和 ...

  8. Express新建工程以及新建路由规则、匹配路由规则、控制权转移

    场景 npm提供了大量的第三方模块,其中不乏许多Web框架,我们没有必要重复发明轮子, 因而选择使用Express作为开发框架,因为它是目前最稳定.使用最广泛,而且Node.js官 方推荐的唯一一个W ...

  9. ASP.NET Web API路由规则(二)

    默认的规则 在ASP.NET MVC4中 global.asax.cs代码中并无注册默认路由规则的代码 代码如下: public class WebApiApplication : System.We ...

最新文章

  1. 报错-Unknown class in Interface Builder file
  2. 简述C/S和B/S模式的区别
  3. 阿里8亿加持B端智能化后,本地生活服务更好做了吗?
  4. Vue001_模板语法
  5. 消息驱动 微服务器,消息驱动的微服务-Spring Cloud Stream整合RocketMQ
  6. python 程序运行计时 动态,在python中运行计时器几分钟
  7. html table datasrc,table_data_tables.html
  8. php限制接口访问次数_令牌桶限流思路分享(PHP+Redis实现机制)
  9. Entity Framework 实体框架的形成之旅--利用Unity对象依赖注入优化实体框架(2)
  10. Mysql 数据库锁机制浅析
  11. IEC 60335 全系列- 家用和类似用途电器 - 包含全部106份最新英文版标准文件
  12. dell台式计算机主板电池,怎么更换主板电池 主板电池更换方法【步骤详解】
  13. nginx学习:搭建静态资源服务器
  14. 基于Multisim的简易数字钟
  15. 智能化的Conversational UI是移动发展的一个趋势
  16. 第三讲 AHRS姿态解算
  17. 医学院交换去计算机学院,皖南医学院学生2018年赴台湾元培医事科技大学参加交换生项目交流心得...
  18. データファイルのアップロードとダウンロード
  19. WIN10无限蓝屏重启解决办法
  20. DrawBoard 是一个自定义 View 实现的画板;方便对图片进行各种编辑或涂鸦相关操作

热门文章

  1. 【转】centos安装vim7.4(转)
  2. 为什么有如此多的C++测试框架 - from Google Testing Blog
  3. 网络摄像头3 cmos ov9650,plugins/input_s3c2410/
  4. rhel5下插上耳机后喇叭还会响
  5. XML电子口岸自动报关项目 真实百万级项目下载
  6. 线上oom 自动kill 程序
  7. CSDN、博客园等6大技术博客平台的写作体验测评
  8. SAPGUI系统登录页面配置的SAProuter有什么用 1
  9. RabbitMq集群使用Nginx做负载均衡
  10. 使用移动自适应布局+easy mock实现移动界面的简单实现