系统架构微服务架构Gateway

微服务网关Apache Shenyu

2021-08-11  本文已影响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>

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实例,验证网关的流量转发。

异常报错

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

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

参考

代码实例
apache shenyu 中文文档

上一篇 下一篇

猜你喜欢

热点阅读