微服务网关Apache Shenyu

作者: 勤_ | 来源:发表于2021-08-11 16:53 被阅读0次

概述

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

shenyu-framework.png

依赖的框架以及版本

工具 版本
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),可以看到如下所示:

image-20210811163707670.png

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

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

    image-20210811164002658.png image-20210811164050227.png image-20210811164121231.png
  • 发起请求并验证可以转发。

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

异常报错

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

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

参考

代码实例
apache shenyu 中文文档

相关文章

  • apache-shenyu之Disruptor如何应用

    (apache-shenyu 2.4.3版本)apache shenyu前身soul网关,是一款java中spri...

  • apache-shenyu之启动项目老年人教程

    (apache-shenyu 2.4.3版本)apache shenyu前身soul网关,是一款java中spri...

  • apache-shenyu 之plugin之match/cond

    (apache-shenyu 2.4.3版本)apache shenyu前身soul网关,是一款java中spri...

  • 微服务网关Apache Shenyu

    概述 网关作为微服务架构里面的核心组件,因此在搭建微服务架构的时候选择一款合适的网关就显得非常重要了。我们之前接触...

  • Apache Shenyu quick start

    Apache Shenyu是一个正在孵化的高性能网关项目,晚上闲来无事,来尝试一下简单使用。网关是微服务组件之一,...

  • apache-shenyu之注册中心整合与元数据同步

    (apache-shenyu 2.4.3版本)本文一起学习apache-shyu的设计与实现。看看shenyu如何...

  • 2018-03-27

    微服务网关 什么是微服务网关 微服务网关也称为服务网关或者API网关。服务网关 = 路由转发 + 过滤器...

  • apache-shenyu之SPI

    从这篇文章开始会从头开始以 apache shenyu为路径,一一学习shenyu中用到的技术及设计,当前文章学习...

  • 网关Kong Gateway

    概述 上文我们了解了JAVA技术栈的网关Shenyu[https://www.jianshu.com/p/84fe...

  • jhipster快速启动微服务

    jhipster快速启动微服务 jh微服务最简启动需要注册中心、网关、业务微服务A。启动顺序注册中心、网关、微服务...

网友评论

    本文标题:微服务网关Apache Shenyu

    本文链接:https://www.haomeiwen.com/subject/kyucbltx.html