资讯专栏INFORMATION COLUMN

SpringCloud(第 022 篇)Zuul 网关微服务的regexmapper属性测试, 类似

cyqian / 459人阅读

摘要:地址可以查看该微服务网关代理了多少微服务的。微服务已启动使用提供和之间的绑定它使用正则表达式组来从提取变量然后注入到路由表达式中。

SpringCloud(第 022 篇)Zuul 网关微服务的 regexmapper 属性测试, 类似测试 zuul 的自定义路径规则一样

-

一、大致介绍
1、本章节将 Zuul 的 regexmapper 属性多带带拿出来,主要是这种配置规则,可以在一定程度上切分服务版本,根据版本信息请求服务;
2、在一些这样的场景中,后台每升级一个版本,需要不同环境测试,可以将 springms-provider-user-version 这样的名字命名为 springms-provider-user-test、springms-provider-user-prd 等名字,可以作为多套环境切换的一种方式而已,仅仅只是建议,不过这种多套环境的切换,后面会讲到用 config 来更简便配置。
二、实现步骤 2.1 添加 maven 引用包


    4.0.0

    springms-gateway-zuul-reg-exp
    1.0-SNAPSHOT
    jar

    
        com.springms.cloud
        springms-spring-cloud
        1.0-SNAPSHOT
    

    
        
        
            org.springframework.cloud
            spring-cloud-starter-zuul
        

        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
    

2.2 添加应用配置文件(springms-gateway-zuul-reg-expsrcmainresourcesapplication.yml)
spring:
  application:
    name: springms-gateway-zuul-reg-exp
server:
  port: 8185
eureka:
  datacenter: SpringCloud   # 修改 http://localhost:8761 地址 Eureka 首页上面 System Status 的 Data center 显示信息
  environment: Test         # 修改 http://localhost:8761 地址 Eureka 首页上面 System Status 的 Environment 显示信息
  client:
    service-url:
      defaultZone: http://admin:admin@localhost:8761/eureka
    healthcheck:  # 健康检查
      enabled: true
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
2.3 添加zuul服务网关微服务启动类(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;

/**
 * Zuul 网关微服务的 regexmapper 属性测试, 类似测试 zuul 的自定义路径规则一样。
 *
 * 注意 EnableZuulProxy 注解能注册到 eureka 服务上,是因为该注解包含了 eureka 客户端的注解,该 EnableZuulProxy 是一个复合注解。
 *
 * @EnableZuulProxy --> { @EnableCircuitBreaker、@EnableDiscoveryClient } 包含了 eureka 客户端注解,同时也包含了 Hystrix 断路器模块注解。
 *
 * http://localhost:8185/routes 地址可以查看该zuul微服务网关代理了多少微服务的serviceId。
 *
 * @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微服务 】】】】】】已启动.");
    }

    /**
     * 使用regexmapper提供serviceId和routes之间的绑定. 它使用正则表达式组来从serviceId提取变量, 然后注入到路由表达式中。
     *
     * 这个意思是说"springms-provider-user-version"将会匹配路由"/version/springms-provider-user/**". 任何正则表达式都可以, 但是所有组必须存在于servicePattern和routePattern之中.
     *
     * @return
     */
    @Bean
    public PatternServiceRouteMapper serviceRouteMapper() {
        return new PatternServiceRouteMapper("(?^.+)-(?v.+$)", "${version}/${name}");
    }
}
三、测试
/****************************************************************************************
 一、Zuul 网关微服务的 regexmapper 属性测试, 类似测试 zuul 的自定义路径规则一样:

 1、编写 application.yml 文件,添加应用程序的注解 EnableZuulProxy 配置;
 2、在 MsGatewayZuulRegExpApplication 类中添加 serviceRouteMapper 方法;
 3、启动 springms-discovery-eureka 模块服务,启动1个端口;
 4、启动 springms-provider-user-version 模块服务,启动1个端口;
 5、启动 springms-gateway-zuul-reg-exp 模块服务;

 6、新起网页页签,输入 http://localhost:8185/version/springms-provider-user/simple/4 正常情况下是能看到 ID != 0 一堆用户信息被打印出来;

 总结:springms-provider-user-version 通过名字和版本被切割后,利用路径拼接规则,通过 http://localhost:8185/version/springms-provider-user/simple/4 也可以访问用户微服务;
 ****************************************************************************************/
四、下载地址

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

SpringCloudTutorial交流QQ群: 235322432

SpringCloudTutorial交流微信群: 微信沟通群二维码图片链接

欢迎关注,您的肯定是对我最大的支持!!!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70594.html

相关文章

  • SpringCloud 018 Zuul 服务 API 网关服务之代理与反向代理

    摘要:注意注解能注册到服务上,是因为该注解包含了客户端的注解,该是一个复合注解。地址可以查看该微服务网关代理了多少微服务的。 SpringCloud(第 018 篇)Zuul 服务 API 网关微服务之代理与反向代理 - 一、大致介绍 1、API 服务网关顾名思义就是统一入口,类似 nginx、F5 等功能一样,统一代理控制请求入口,弱化各个微服务被客户端记忆功能; 2、本章节主要讲解了使用...

    YancyYe 评论0 收藏0
  • SpringCloud 020 Zuul 网关模块添加 listOfServers 属性,达

    摘要:注意注解能注册到服务上,是因为该注解包含了客户端的注解,该是一个复合注解。地址可以查看该微服务网关代理了多少微服务的。 SpringCloud(第 020 篇)Zuul 网关模块添加 listOfServers 属性,达到客户端负载均衡的能力 - 一、大致介绍 1、本章节添加另外一个属性 listOfServers 来给 zuul 赋上异样的功能色彩,提供负载均衡的能力; 2、而其实说...

    Dogee 评论0 收藏0
  • SpringCloud 019 Zuul 网关服务一些属性应用测试

    SpringCloud(第 019 篇)Zuul 网关微服务的一些属性应用测试 - 一、大致介绍 1、本章节根据官网资料,尝试了一些其它属性的设置,比如 path、serviceId、prefix、strip-prefix 等应用; 2、这些组合试用的场景大多数在一些地址方面需要重新映射或者针对特殊地址做特殊处理等,至于其它一些深层次的应用大家做过知道的话也可以告尽情回帖让大家都来学习学习。 二、...

    imtianx 评论0 收藏0
  • SpringCloud 021 Zuul 过滤器 ZuulFilter 使用

    摘要:第篇的过滤器的使用一大致介绍我们在学的时候,就有过滤器和拦截器的使用,而同样也有过滤器的使用,本章节我们指在如何简单使用。是否执行该过滤器。说明需要过滤说明不要过滤过滤器的具体逻辑。请求的添加服务网关微服务启动类的过滤器的使用。 SpringCloud(第 021 篇)Zuul 的过滤器 ZuulFilter 的使用 - 一、大致介绍 1、我们在学 Spring 的时候,就有过滤器和拦...

    kumfo 评论0 收藏0
  • SpringCloud 025 Zuul 路由后面服务挂了后,Zuul 提供了一种回退机

    摘要:英文意思就是说提供一个回退机制当路由后面的服务发生故障时。注意注解能注册到服务上,是因为该注解包含了客户端的注解,该是一个复合注解。地址可以查看该微服务网关代理了多少微服务的。 SpringCloud(第 025 篇)Zuul 路由后面的微服务挂了后,Zuul 提供了一种回退机制来应对熔断处理 - 一、大致介绍 1、在一些不稳定因素导致路由后面的微服务宕机或者无响应时,zuul 就会累...

    Jochen 评论0 收藏0

发表评论

0条评论

cyqian

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<