阅读 83

SpringCloud Gateway使用

本文基于springboot+nacos+gateway实现,使用springboot作为基础工程,nacos作为注册中心及配置中心,gateway作为网关
项目整体使用版本号:
springboot-2.6.8
springcloud-2021.0.3
spring-cloud-alibaba-2021.0.1.0

1. Gateway网关服务:

创建普通的springboot工程,网关服务中不要添加spring-boot-starter-web依赖:
pom依赖

        <!-- gateway网关 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!-- nacos注册中心 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- nacos配置中心 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!-- nacos配置中心需要使用到的bootstrap -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.1.1</version>
        </dependency>
        <!--fegin组件  不加该依赖会出现503问题  -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- Feign Client for loadBalancing -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
            <version>3.1.3</version>
        </dependency>

yml配置
bootstrap.yml:naocs配置中心需要的依赖,因为要优先加载,所以使用该名称yml:

spring:
  application:
    name: GatewayService  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8082        group: testGroup        name: GatewayService        file-extension: yaml        namespace: Dev

application.yml普通yml文件配置,包含sentinel配置,nacos注册中心配置,gateway网关配置及相关服务路由配置

server:
  port: 8083spring:
  application:
    name: GatewayService
  # 该配置用于解决 springboot使用tomcat  Springcloud gateway使用netty 导致冲突,所以添加改配置,然后不添加spring-boot-starter-web依赖  网关服务就可以正常启动了
  main:
    web-application-type: reactive
  cloud:
    nacos:
      server-addr: 127.0.0.1:8082
      discovery:
        group: testGroup        namespace: Dev
    sentinel:
      transport:
        dashboard: 127.0.0.1:8080
    gateway:
      # 全局超时限制
      httpclient:
        connect-timeout: 1000
        response-timeout: 200
      routes:
        - id: testService01
          # 如果使用nacos作为注册中心,这里直接使用lb://服务名  非nacos uri: http://127.0.0.1:8001
          uri: lb://testService01
          predicates:#            - Path=/test01/**
            - name: Path
              args:
                pattern: /test01/**
          # 针对某个路由进行超时限制,配置该项,则全局配置不对该路由起作用
          metadata:
            response-timeout: 300
            connect-timeout: 300
        - id: testService02
          # 如果使用nacos作为注册中心,这里直接使用lb://服务名  非nacos uri: http://127.0.0.1:8001
          uri: lb://testService02
          predicates:
            - name: Path
              args:
                pattern: /test02/**

服务启动类

@SpringBootApplication@EnableDiscoveryClient // nacos注册中心使用配置public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }}

大版本控制

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.8</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2021.0.1.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2. 测试应用服务01:

pom依赖配置,使用nacos作为注册中心及配置中心

    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.1.1</version>
        </dependency>
    </dependencies>

yml配置
bootstrap.yml:naocs配置中心需要的依赖,因为要优先加载,所以使用该名称yml:

spring:
  application:
    name: Test-Service-01
  cloud:
    nacos:
      config:
        # 配置中心地址
        server-addr: 127.0.0.1:8082
        # 命名空间  可区分环境使用 dev开发环境  pro生产环境
        namespace: Dev
        # 配置列表 dataID前缀
        name: testService01
        # 配置列表 dataID后缀
        file-extension: yaml
        # 如果不配置该项,则使用DEFAULT_GROUP 不支持配置自动更新
        # 如果配置该项,支持配置自动更新
        group: testGroup
        # 动态刷新功能   true开启   false关闭
        refresh-enabled: true

application.yml普通yml文件配置,包含sentinel配置,nacos注册中心配置,gateway网关配置及相关服务路由配置

server:
  port: 8090spring:
  application:
    name: testService01  cloud:
    nacos:
      server-addr: 127.0.0.1:8082      discovery:
        group: testGroup        namespace: Dev    sentinel:
      transport:
        dashboard: 127.0.0.1:8080

启动类

@SpringBootApplication@EnableDiscoveryClient // nacos注册中心配置public class TestServiceDemo01Application {

    public static void main(String[] args) {
        SpringApplication.run(TestServiceDemo01Application.class, args);
    }}

测试方法类

@RestController@RefreshScope // nacos配置中心动态刷新获取配置public class TestController {

    @Value("${test.val:}")
    private String val;

    @GetMapping("/test01/get")
    public String get() throws Exception{
        Thread.sleep(205);
        return val;
    }}

3. nacos注册中心相关截图:

注册中心上显示的服务



作者:初心myp
链接:https://www.jianshu.com/p/953d06270f7f

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐