springcloud3 GateWay章节-通过硬编码方式实现路由规则配置2

一 Gateway通过编码方式实现

1.1 Gateway编码方式

1.编写一个注册类

package com.ljf.mscloud.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * @auther zzyy
 * @create 2020-02-21 11:42
 */
@Configuration
public class GateWayConfig
{
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder)
    {
        /**
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();

        routes.route("path_haha",
                r -> r.path("/batchSearch")
                        .uri("https://www.qcc.com/web/project")).build();//https://www.qcc.com/web/project/batchSearch
        return routes.build();
         /**********************************   百度网站 ***********************/

        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();

        /**情况1
        routes.route("path_route_rg",r->r.path("/lady").uri("https://news.baidu.com/lady")).
                route("path_route_rg2",r->r.path("/guonei").uri("https://news.baidu.com/guonei"));
         **/
        /**
         * 情况2
         routes.route("path_route_rg",r->r.path("/lady").uri("https://news.baidu.com")).
         route("path_route_rg2",r->r.path("/guonei").uri("https://news.baidu.com"));
         */
        /** 情况3
        routes.route("path_route_rg",r->r.path("/**").uri("https://news.baidu.com"));
         **/
        //return routes.build();

       // routes.route("path_haha", r -> r.path("/batchSearch").uri("https://www.qcc.com/web/project/batchSearch")); //https://www.qcc.com/web/project/batchSearch
      //  routes.route("path_haha", r -> r.path("/batchSearch").uri("https://www.qcc.com/web/project")); //https://www.qcc.com/web/project/batchSearch
        routes.route("path_haha", r -> r.path("/web/project/batchSearch").uri("https://www.qcc.com")); //https://www.qcc.com/web/project/batchSearch
      //  routes.route("path_haha", r -> r.path("/web/**").uri("https://www.qcc.com/web/project/batchSearch"));


      // routes.route("path_haha2", r -> r.path("/nav/**").uri("https://blog.csdn.net"));// https://blog.csdn.net/nav/web
      //  routes.route("path_haha2", r -> r.path("/web").uri("https://blog.csdn.net/nav/"));
       // routes.route("path_haha2", r -> r.path("/nav/web").uri("https://blog.csdn.net"));
     //   routes.route("path_haha2", r -> r.path("/nav/web").uri("https://blog.csdn.net/nav/web"));
       // return routes.build();
        return routes.build();
    }
    @Bean
    public CorsWebFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedMethod("*");
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }
}

1.2 操作方式

1.启动eureka9001,eureka9002

2.服务提供者  9003,9004,

3.网关 9007

1.3 结论

后面的测试案例中,有的配置能访问,有的不能访问,总结

1.断言:配置成域名第一级路径加通配符 /**  ,进行模糊匹配,肯定正确

2.如果域名后面有多层级路径,把域名后面的所有路径当成一个整体,配置到断言,uri配置成 域名的根级别。

     routes.route("path_route_rg",r->r.path("/lady").uri("https://news.baidu.com")).
      route("path_route_rg2",r->r.path("/guonei").uri("https://news.baidu.com"));

routes.route("path_haha",r.path("/web/project/batchSearch").uri("https://www.qcc.com")); //https://www.qcc.com/web/project/batchSearch

3.其他配置可能存在问题

二 Gateway案例测试

2.1 访问百度新闻

2.2.1 访问百度新闻1

最后一级路径做断言,uri为完整转发地址。

1.代码

        routes.route("path_route_rg",r->r.path("/lady").uri("https://news.baidu.com/lady")).
                route("path_route_rg2",r->r.path("/guonei").uri("https://news.baidu.com/guonei"));

2.代理后:http://localhost:9007/guonei

http://localhost:9007/lady

 2.2.2 访问百度新闻2

最后一级路径做断言,uri为根域名

1.代码

         routes.route("path_route_rg",r->r.path("/lady").uri("https://news.baidu.com")).
         route("path_route_rg2",r->r.path("/guonei").uri("https://news.baidu.com"));

 2.代理后:http://localhost:9007/guonei

 http://localhost:9007/lady

 2.2.3   访问百度新闻3

使用模糊匹配规则做断言

1.代码

        /** 情况3  **/
        routes.route("path_route_rg",r->r.path("/**").uri("https://news.baidu.com"));

  2.代理后:http://localhost:9007/guonei 

   http://localhost:9007/lady

 2.2 访问企查查

2.2.1 情况1

       routes.route("path_haha", r -> r.path("/batchSearch").uri("https://www.qcc.com/web/project/batchSearch")); //https://www.qcc.com/web/project/batchSearch

访问

 2.2.2 情况2

1.代码

        routes.route("path_haha", r -> r.path("/batchSearch").uri("https://www.qcc.com/web/project")); //https://www.qcc.com/web/project/batchSearch

访问

  2.2.3 情况3

1.代码

        routes.route("path_haha", r -> r.path("/web/project/batchSearch").uri("https://www.qcc.com")); //https://www.qcc.com/web/project/batchSearch

2.页面访问 

   2.2.4 情况4

1.代码

       routes.route("path_haha", r -> r.path("/web/**").uri("https://www.qcc.com/"));

2.页面访问

  2.3 访问csdn

2.3.1 情况1

1.代码

       routes.route("path_haha2", r -> r.path("/nav/**").uri("https://blog.csdn.net"));// https://blog.csdn.net/nav/web

访问:

 2.3.2 情况2

1.代码

        routes.route("path_haha2", r -> r.path("/web").uri("https://blog.csdn.net/nav/"));

2.页面访问

  2.3.3 情况3

1.代码

        routes.route("path_haha2", r -> r.path("/nav/web").uri("https://blog.csdn.net"));

2.页面访问

   2.3.4 情况4

1.代码

       routes.route("path_haha2", r -> r.path("/nav/web").uri("https://blog.csdn.net/nav/web"));

2.页面访问

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值