Spring cloud (Part 022) regexmapper property test of zuul gateway microservice, similar to zuul’s custom path rules

Time:2020-1-12

Spring cloud (Part 022) regexmapper property test of zuul gateway microservice, similar to the test of zuul’s custom path rules

1、 General introduction

1. In this chapter, the regexmapper attribute of zuul is taken out separately, which is mainly a configuration rule. To a certain extent, the service version can be segmented, and the service can be requested according to the version information;
2. In some of these scenarios, for each version upgraded in the background, different environment tests are required. Names such as springms provider user version can be named springms provider user test, springms provider user PRD, etc., which can be used as a way to switch multiple environments. It's just a suggestion. However, the switch of multiple environments will be discussed later To use config for easier configuration.

2、 Implementation steps

2.1 add Maven reference package

<?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>

    <artifactId>springms-gateway-zuul-reg-exp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.springms.cloud</groupId>
        <artifactId>springms-spring-cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <! -- API gateway module -- >
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>

        <! -- client discovery module. Because the document says that the Eureka client discovery module is not included in zuul's dependency, it has to be added here separately -- >
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>

</project>

2.2 add application configuration file (springms gateway zuul reg expsrcmainresources application. YML)

spring:
  application:
    name: springms-gateway-zuul-reg-exp
server:
  port: 8185
eureka:
  Datacenter: springcloud ා modify the data center display information of system status on the homepage of Eureka at http: // localhost: 8761
  Environment: Test modify the environment display information of system status on the homepage of Eureka at http: // localhost: 8761
  client:
    service-url:
      defaultZone: http://admin:[email protected]:8761/eureka
    Healthcheck: health check
      enabled: true
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

2.3 add zuul service gateway microservice startup class (springms gateway zuul reg expsrcmainjavacomspringmscloudmsgatewayzuulregexpapplication. Java)

package com.springms.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;

/**
 *The regexmapper property test of zuul gateway microservice is similar to the test of zuul's custom path rules.
 *
 *Note that the enablezuulproxy annotation can be registered on the Eureka service because it contains the annotations of the Eureka client. The enablezuulproxy is a compound annotation.
 *
 *The @ enablezuulproxy -- > {@ enablechircuitbreaker, @ enablediscoveryclient} contains both Eureka client annotations and hystrix breaker module annotations.
 *
 *Http: // localhost: 8185 / routes address can see how many microservices' serviceids the zuul microservice gateway proxies.
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/9/26
 *
 */
@SpringBootApplication
@EnableZuulProxy
public class MsGatewayZuulRegExpApplication {

    public static void main(String[] args) {
        SpringApplication.run(MsGatewayZuulRegExpApplication.class, args);
        System. Out. Println ("[[[[[[gatewayzuulregexp]]]] has been started.");
    }

    /**
     *Regexmapper is used to provide the binding between serviceid and routes. It uses regular expression groups to extract variables from serviceid and inject them into route expressions.
     *
     *This means that "springms provider user version" will match the route "/ Version / springms provider user / * *". Any regular expression can, but all groups must exist in servicepattern and routepattern
     *
     * @return
     */
    @Bean
    public PatternServiceRouteMapper serviceRouteMapper() {
        return new PatternServiceRouteMapper("(?<name>^.+)-(?<version>v.+$)", "${version}/${name}");
    }
}

Three, test

/****************************************************************************************
 1、 The regexmapper property test of zuul gateway microservice is similar to the test of zuul's custom path rules:

 1. Write application.yml file, add application annotation enablezuulproxy configuration;
 2. Add serviceroutemapper method to msgatewayzuulregexpapplication class;
 3. Start the spring MS discovery Eureka module service, and start one port;
 4. Start the spring MS provider user version module service, and start 1 port;
 5. Start the springms gateway zuul reg exp module service;

 6. On the new web page tab, enter http: // localhost: 8185 / Version / springms provider user / simple / 4. Normally, you can see a batch of user information with ID! = 0 printed out;

 Conclusion: after the spring MS provider user version is cut by name and version, the user micro service can also be accessed through http: // localhost: 8185 / Version / spring MS provider user / simple / 4 by using path splicing rules;
 ****************************************************************************************/

4、 Download address

https://gitee.com/ylimhhmily/SpringCloudTutorial.git

Spring cloudtutorial communication QQ group: 235322432

Spring cloudtutorial communication wechat group: wechat communication group QR code image link

Welcome to pay attention, your affirmation is the biggest support to me!!!

Recommended Today

[reading notes] calculation advertising (Part 3)

By logm This article was originally published at https://segmentfault.com/u/logm/articles and is not allowed to be reproduced~ If the mathematical formula in the article cannot be displayed correctly, please refer to: Tips for displaying the mathematical formula correctly This article isComputing advertising (Second Edition)Reading notes. This part introduces the key technology of online advertising, which is […]