0
点赞
收藏
分享

微信扫一扫

微服务网关Apache Shenyu

概述

网关作为微服务架构里面的核心组件,因此在搭建微服务架构的时候选择一款合适的网关就显得非常重要了。我们之前接触过的网关这块技术框架有很多,例如:spring cloud gateway、zuul 、Kong(Nginx + Lua)等等。这些网关就不做详细介绍了,而今天想向大家推荐另外一款异步的,高性能的,跨语言的,响应式的 API 网关 -- Apache ShenYu,它同时能支持各种语言(http 协议),支持 Dubbo、 Spring Cloud、 gRPC、 Motan、 Sofa、 Tars 等协议,支持灵活的流量筛选,能满足各种流量控制,具有丰富的插件,鉴权,限流,熔断,防火墙等等。

依赖的框架以及版本

工具 版本
shenyu 2.4.0
spring-cloud-starter-alibaba-nacos-discovery 2.1.0.RELEASE
spring-boot 2.2.2.RELEASE
nacos-server 2.0.3

实现步骤

因为公司主要是采用springCloud技术栈,注册中心使用Nacos,所以接下来我们将Shenyu 集成springcloud、nacos等框架。

1,从https://github.com/apache/incubator-shenyu 选择2.4.0版本下载源码。

2,调整shenyu-admin关键配置,详细参考工程代码如下:

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

server:
  port: 9095
  address: 0.0.0.0

spring:
#  profiles:
#    active: h2
  thymeleaf:
    cache: true
    encoding: utf-8
    enabled: true
    prefix: classpath:/static/
    suffix: .html
  datasource:
    url: jdbc:mysql://xxxx:3306/shenyu?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: xxxxxx
    driver-class-name: com.mysql.jdbc.Driver
......

3,调整shenyu-bootstrap关键配置及依赖,如下所示:

.........
spring:
   main:
     allow-bean-definition-overriding: true
   application:
    name: shenyu-bootstrap
   cloud:
    nacos:
       discovery:
          server-addr: localhost:8848
..............
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>2.1.0.RELEASE</version>
</dependency>
  • 注意由于后面我们要使用springcloud这个插件,为此每个接口的请求需要请求头要指定rpc_type为springCloud,这样很不方便,于是我们在这里增加了一个自定义webfiler来统一处理这一情况,代码如下所示:

    package org.apache.shenyu.bootstrap.filter;
    
    import org.springframework.core.annotation.Order;
    import org.springframework.http.server.reactive.ServerHttpRequest;
    import org.springframework.stereotype.Component;
    import org.springframework.web.server.ServerWebExchange;
    import org.springframework.web.server.WebFilter;
    import org.springframework.web.server.WebFilterChain;
    import reactor.core.publisher.Mono;
    
    @Component
    @Order(-98)
    public class RpcSpringCloudHeadFilter implements WebFilter {
    
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
            ServerHttpRequest rpcType = exchange.getRequest().mutate().header("rpc_type", "springCloud").build();
            ServerWebExchange build = exchange.mutate().request(rpcType).build();
            return chain.filter(build);
        }
    }
    

4,调整shenyu-examples\shenyu-examples-springcloud工程,关键调整如下所示:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>2.1.0.RELEASE</version>
</dependency>
......
server:
  port: 8899
  address: 0.0.0.0
  servlet:
    context-path: /${spring.application.name}

spring:
  application:
    name: examples
  main:
    allow-bean-definition-overriding: true
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
......        

5,启动nacos 2.0.3,startup.cmd -m standalone单机启动。然后分别启动shenyu-admin、shenyu-bootstrap、shenyu-examples-springcloud。

6,登录http://localhost:9095/ (admin/123456),可以看到如下所示:

7,接下来我们操作一把,通过网关shenyu-bootstrap访问shenyu-examples-springcloud实例,验证网关的流量转发。

  • 启动插件、配置选择器和规则

  • 发起请求并验证可以转发。

  • 其他的一些功能,例如限流降级、监控日志、鉴权等等我就不一一验证了。

异常报错

1,Can not find url, please check your configuration!

这是因为没有配置正确的选择器以及规则或者没有设置rpc_type为springCloud的请求头参数

参考

代码实例
apache shenyu 中文文档

举报

相关推荐

0 条评论