springboot集成Gateway & Nacos「再来一刀」

SpringCloud官方,对SpringCloud Gateway 特征介绍如下:

(1)基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0
(2)集成 Hystrix 断路器
(3)集成 Spring Cloud DiscoveryClient
(4)Predicates 和 Filters 作用于特定路由,易于编写的 Predicates 和 Filters
(5)具备一些网关的高级功能:动态路由、限流、路径重写

平时开发中,Gateway与注册中心(Nacos)结合使用,下面简单搭建一个测试工程入门:

(1)springboot-gateway-server:gateway服务端
(2)springboot-gateway-client-a:集成Nacos客户端A
(3)springboot-gateway-client-b:集成Nacos客户端B

通过访问 springboot-gateway-server 服务端,转发至已注册到Nacos上的 springboot-gateway-client-a 服务A与springboot-gateway-client-b 服务B。

一、Gateway服务端搭建

开发环境

  • Spring Boot:2.1.12.RELEASE
  • Spring Cloud:Greenwich.SR5
  • Spring Cloud Alibaba:2.1.2.RELEASE

参考 东小西:springboot集成Gateway「初试牛刀」,搭建 Gateway 服务端。

1. 增加 Nacos 依赖

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

2. bootstrap.yml Nacos配置

spring:

  cloud:

    nacos:

      discovery:

        server-addr: localhost:8848

        namespace: 7e98b650-0c03-4663-b747-b3d4848630aa

        group: DEFAULT_GROUP

3. application.yml 配置

server:

  port: 9001
  servlet:

    context-path: /sgs
spring:

  application:

    name: springboot-gateway-server
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
        - id: springboot-gateway-client-a
          uri: lb://springboot-gateway-client-a
          predicates:
            - Path=/sgca/**
        - id: springboot-gateway-client-b
          uri: lb://springboot-gateway-client-b
          predicates:
            - Path=/sgcb/**

二、测试客户端搭建

测试客户端搭建参考:Springboot集成Nacos2「服务注册与发现」

1. POM依赖

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR5</spring-cloud.version>
        <spring-cloud-alibaba.version>2.1.2.RELEASE</spring-cloud-alibaba.version>
    </properties>
    <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>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>
    <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-discovery</artifactId>
        </dependency>
    </dependencies>

2. application.yml 配置

server:

  port: 9002
  servlet:

    context-path: /sgca
spring:

  application:

    name: springboot-gateway-client-a

3. bootstrap.yml Nacos配置(同服务端)

spring:

  cloud:

    nacos:

      discovery:

        server-addr: localhost:8848

        namespace: 7e98b650-0c03-4663-b747-b3d4848630aa

        group: DEFAULT_GROUP

4. 启动类

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayClientApplication.class, args);
        System.out.println("        ヾ(◍°∇°◍)ノ゙    Application启动成功      ヾ(◍°∇°◍)ノ゙\n" +
                "                    _\n" +
                "            "
                + "      _(_)_                           wWWWw   _\n" +
                "      @@@@       (_)@(_)   vVVVv     _     @@@@  (___) _(_)_\n" +
                "     @@()@@ wWWWw  (_)\    (___)   _(_)_  @@()@@   Y  (_)@(_)\n" +
                "      @@@@  (___)     `|/    Y    (_)@(_)  @@@@   \|/   (_)\\n" +
                "       /      Y       \|    \|/    /(_)    \|      |/      |\n" +
                "    \ |     \ |/       | / \ | /  \|/       |/    \|      \|/\n" +
                "    \\|//   \\|///  \\\|//\\\|/// \|///  \\\|//  \\|//  \\\|// \n" +
                "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
    }
}

5. 测试接口

@RestController
@RequestMapping(value = "/test")
public class TestController {

    @RequestMapping(value = "/1")
    public String test(){
        return "I am client A!!!";
    }
}

相同的方式,搭建 springboot-gateway-client-b 测试客户端。

启动Gateway服务端和客户端。

上面已简单实现了Gateway网关请求转发功能。

springboot集成Gateway DEMO: gitee.com/renxiaoshi/…

© 版权声明
THE END
喜欢就支持一下吧
点赞0

Warning: mysqli_query(): (HY000/3): Error writing file '/tmp/MYO1EPfl' (Errcode: 28 - No space left on device) in /www/wwwroot/583.cn/wp-includes/class-wpdb.php on line 2345
admin的头像-五八三
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

图形验证码
取消
昵称代码图片