关于java:Spring-cloud-Gateway-换成-Apache-APISIX-云原生-API-网关初体验

56次阅读

共计 8243 个字符,预计需要花费 21 分钟才能阅读完成。

https://landscape.cncf.io/card-mode?category=api-gateway&grou…

一、环境我的项目筹备

1、创立 Spring Cloud Gateway API 网关我的项目

  1. 新建 demo-gateway 我的项目

pom 依赖如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>demo-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-gateway</name>
    <description>demo-gateway</description>
    <properties>
        <java.version>17</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
        <spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
        <spring-cloud.version>2021.0.5</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.example.gateway.DemoGatewayApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  1. 配置文件 bootstrap.yml

    server:
      port: 8099
    spring:
      application:
     name: demo-gateway
      profiles:
     active: dev
      cloud:
     nacos:
       discovery:
         server-addr: NacosIP 地址:8848
       config:
         server-addr: ${spring.cloud.nacos.discovery.server-addr}
         file-extension: yml

    2、创立 Srping Cloud 接口服务 我的项目

  2. 新建 demo-api 我的项目

    pom 依赖如下

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.example</groupId>
     <artifactId>demo-api</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <name>demo-api</name>
     <description>demo-api</description>
     <properties>
         <java.version>17</java.version>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <spring-boot.version>3.0.0</spring-boot.version>
         <spring-cloud-alibaba.version>2022.0.0.0-RC1</spring-cloud-alibaba.version>
         <spring-cloud.version>2022.0.0-RC2</spring-cloud.version>
     </properties>
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
         <dependency>
             <groupId>com.alibaba.cloud</groupId>
             <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
         </dependency>
         <dependency>
             <groupId>com.alibaba.cloud</groupId>
             <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-bootstrap</artifactId>
         </dependency>
     </dependencies>
     <dependencyManagement>
         <dependencies>
             <dependency>
                 <groupId>org.springframework.cloud</groupId>
                 <artifactId>spring-cloud-dependencies</artifactId>
                 <version>${spring-cloud.version}</version>
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-dependencies</artifactId>
                 <version>${spring-boot.version}</version>
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
             <dependency>
                 <groupId>com.alibaba.cloud</groupId>
                 <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                 <version>${spring-cloud-alibaba.version}</version>
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
         </dependencies>
     </dependencyManagement>
     <build>
         <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
                 <version>3.8.1</version>
                 <configuration>
                     <source>17</source>
                     <target>17</target>
                     <encoding>UTF-8</encoding>
                 </configuration>
             </plugin>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
                 <version>${spring-boot.version}</version>
                 <configuration>
                     <mainClass>com.example.demo.api.DemoApiApplication</mainClass>
                     <skip>true</skip>
                 </configuration>
                 <executions>
                     <execution>
                         <id>repackage</id>
                         <goals>
                             <goal>repackage</goal>
                         </goals>
                     </execution>
                 </executions>
             </plugin>
         </plugins>
     </build>
     <repositories>
         <repository>
             <id>spring-milestones</id>
             <name>Spring Milestones</name>
             <url>https://repo.spring.io/milestone</url>
             <snapshots>
                 <enabled>false</enabled>
             </snapshots>
         </repository>
     </repositories>
    </project>
  3. 配置文件 bootstrap.yml

    server:
      port: 9001
    spring:
      application:
     name: demo-api
      profiles:
     active: dev
      cloud:
     nacos:
       discovery:
         server-addr: NacosIP 地址:8848
       config:
         server-addr: ${spring.cloud.nacos.discovery.server-addr}
         file-extension: yml

    3、下载启动 Naocs 作为服务配置注册核心

  4. 下载安装

本地下载装置详见 https://nacos.io/zh-cn/docs/v2/quickstart/quick-start.html

当然也可 Docker 形式详见 https://nacos.io/zh-cn/docs/v2/quickstart/quick-start-docker….

  • 增加 demo-gateway、demo-api 的配置文件

demo-gateway-dev.yml

spring:
  cloud:
    gateway:
      routes:
        # 路由的 ID,没有固定规定,但要求惟一,倡议配合服务名
        - id: demo-api
          # 匹配后提供服务的路由地址
          uri: lb://demo-api
          predicates:
            - Path=/api/**

demo-api-dev.yml

demo:
  params: 来自 Nancos 配置核心的数据 

4、装置并运行 APISIX API 网关

本次应用 CentOS 选用 Docker 形式装置,须要先装置 Docker 和 Docker-Compose

  • Docker 装置
## 卸载旧版本
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
## 设置 Docker 存储库
sudo yum install -y yum-utils

sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
## 装置运行
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

sudo systemctl start docker
## 设置 docker 开机自启
systemctl ebable docker
  • Docker Compose 装置

    ## 下载安装(龟速,或者应用后边的形式)curl -SL https://github.com/docker/compose/releases/download/v2.17.2/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
    
    ## 通过地址离线下载上传 CentOS 后执行
    sudo mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose 
    sudo chmod +x /usr/local/bin/docker-compose 
    docker-compose -v
  • 正菜来了!APISIX 装置

    ## 首先下载 apisix-docker 仓库(龟速持续离线下载上传)git clone https://github.com/apache/apisix-docker.git
    ## 应用 Nacos 服务发现注册,配置 Nacos
    cd apisix-docker-master/example/apisix_conf
    ## 增加 Nacos 地址
    discovery:
    nacos:
      host:
        - "http://NacosIP 地址:8848"
    ## 回到 apisix-docker-master/example 门路下,应用 docker-compose 启用 APISIX
    docker-compose -p docker-apisix up -d
  • 下载启动实现,拜访 http://CenOS 机器 IP 地址:9000/ 即 APISIX Dashboard 界面

账号密码查看 apisix-docker-master/example/dashboard_conf/conf.yaml 默认 admin/admin

后期筹备工作实现!

二、Spring Cloud Gateway 网关运行测试

  • 启动 demo-gateway、demo-api 服务,默认 9001 再启动个 9002 服务做负载。

  • 发送申请
    http://SpringCloudGatewayIP:8099/api/demo

发送两次申请,失去 9001 和 9002 的轮询响应

Hello :9001--> 来自 Nancos 配置核心的数据
Hello :9002--> 来自 Nancos 配置核心的数据 

三、Spring Cloud Gateway 改成 APISIX 网关运行测试

  1. 登录 APISIX Dashboard,配置上游服务发现 Naocs
  1. 配置路由,除了标注和非凡配置,一路欢快的下一步即可

  1. 发送申请
    http://APISIX 的机器 IP:9080/api/demo

发送两次申请,失去 9001 和 9002 的轮询响应

Hello :9001--> 来自 Nancos 配置核心的数据
Hello :9002--> 来自 Nancos 配置核心的数据 

到此,最最最根底的代替就实现啦!

正文完
 0