Zuul使用正则表达式指定路由规则

使用指定的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

让他成为一个最简单的zuul

https://cloud.spring.io/spring-cloud-netflix/reference/html/#_zuul_http_client

@Bean
public PatternServiceRouteMapper serviceRouteMapper() {
    return new PatternServiceRouteMapper(
        "(?^.+)-(?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就是以

"(?^.+)-(?v.+$)"这种表达式命名的,有一个name的变量,有一个version的变量,然后他会

映射成什么样的映射路径呢,我们之前是没有version的

我们把

spring.application.name=microservice-simple-provider-user

改成

spring.application.name=microservice-simple-provider-user-v1

localhost: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(
	  "(?^.+)-(?v.+$)",
	  "${version}/${name}");
}
#debug=true
server.port=7900

#server.context-path=/boot02

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

logging.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:/learn

spring.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=60000

mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

eureka.client.serviceUrl.defaultZone=http://admin:[email protected]:8761/eureka

spring.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


	4.0.0
	com.learn.cloud
	microservice-gateway-zuul-reg-exp
	0.0.1-SNAPSHOT
	jar

	 
	   cn.learn
	   microcloud02
	   0.0.1
	 
	
	
		UTF-8
		UTF-8
		1.8
		
	
	
        
            org.springframework.cloud
            spring-cloud-starter-eureka-server
            1.4.2.RELEASE
        
		
		    org.springframework.cloud
		    spring-cloud-starter-zuul
		
	
	
	
	
	    
	        
	            org.springframework.boot
	            spring-boot-maven-plugin
	        
	    
	

  

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:[email protected]: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);
  }
  
  @Bean
  public PatternServiceRouteMapper serviceRouteMapper() {
      return new PatternServiceRouteMapper(
          "(?^.+)-(?v.+$)",
          "${version}/${name}");
  }
}

 

你可能感兴趣的:(Zuul使用正则表达式指定路由规则)