日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/4/13 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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讓他成為一個最簡單的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使用正则表达式指定路由规则的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。