朝阳博客

  • Article
  • Project
  • HTML+CSS
  • Bootstrap
  • JavaScript
  • jQuery
  • PHP
  • Java
  • Linux
  • MySQL
朝阳博客
一款专注于博客的响应式网站
  1. 首页
  2. Java
  3. 正文

SpringCloud Gateway使用

2023年3月28日 278点热度 0人点赞 0条评论

本文基于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依赖

        
        
            org.springframework.cloud
            spring-cloud-starter-gateway
        

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-config
        

        
            org.springframework.cloud
            spring-cloud-starter-bootstrap
            3.1.1
        

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

        
            org.springframework.cloud
            spring-cloud-loadbalancer
            3.1.3
        

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: 8083

spring:
  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);
    }
}

大版本控制

    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.8
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                2021.0.3
                pom
                import
            
            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                2021.0.1.0
                pom
                import
            
        
    

2. 测试应用服务01:

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

    
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-config
        
        
            org.springframework.cloud
            spring-cloud-starter-bootstrap
            3.1.1
        
    

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: 8090

spring:
  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注册中心相关截图:

注册中心上显示的服务
标签: 暂无
最后更新:2023年3月28日

aiyao

你要加油,你喜欢的人还没结婚。

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2021 zhaoyangweb.com. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

冀ICP备2021020018号-1