1. 程式人生 > >第二十二章 Spring cloud Zuul使用正則表示式指定路由規則

第二十二章 Spring cloud Zuul使用正則表示式指定路由規則

Zuul使用正則表示式指定路由規則


EurekaApplication

package com.example.demo;

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 EurekaApplication {
  public static void main(String[] args) {
    SpringApplication.run(EurekaApplication.class, args);
  }
  
  /**
   * Zuul使用正則表示式指定路由規則
   * 您可以使用regexmapper提供serviceId和路由之間的約定。它使用名為組的正則表示式從serviceId中提取變數,並將其注入到路由模式中。
   * @return
   */
  @Bean
  public PatternServiceRouteMapper serviceRouteMapper() {
      return new PatternServiceRouteMapper(
          "(?<name>^.+)-(?<version>v.+$)",
          "${version}/${name}");
  }
}

application.yml配置

server:
  port: 8040
spring:
  application:
    name: spring-cloud-zuul-api-gateway-reg
eureka:
  client:
    serviceUrl:
      defaultZone: http://user:[email protected]:8761/eureka
  instance:
    prefer-ip-address: true
    
#此處主要是通過正則表示式定義路由規則:
#比如:此處路由到使用者微服務,改下使用者微服務appliaction.name為:spring-cloud-user-v1
#那麼訪問的路徑就為:http://10.138.5.48:8040/v1/spring-cloud-user/user/1

pom.xml配置

<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.example.demo</groupId>
	<artifactId>spring-cloud-zuul-api-gateway-reg</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>spring-cloud-zuul-api-gateway-reg</name>
	<description>spring-cloud-zuul-api-gateway-reg</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.1.RELEASE</version>
	</parent>

	<properties>
		<!-- 檔案拷貝時的編碼 -->
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<!-- 編譯時的編碼 -->
		<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
		<!-- jdk版本 -->
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zuul</artifactId>
		</dependency>
			<!-- 註冊中心 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>		
		<!-- 用於熱部署 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<!-- 版本依賴管理,故之後新增依賴無需指定version -->
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Camden.SR2</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<!-- 用以為integration-test提供支援。 -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>