• 【java_wxid项目】【第六章】【Spring Cloud Gateway集成】


    主项目链接:https://gitee.com/java_wxid/java_wxid
    项目架构及博文总结:

    项目模块:
    前期规划,实现部分

    java_wxid   
    ├── demo                                                            // 演示模块
    │     └── 模块名称:apache-mybatis-demo模块                            //Apache Mybatis集成(已实现并有博文总结)
    │     └── 模块名称:apache-shardingsphere-demo模块                     //Apache ShardingSphere集成(已实现并有博文总结)
    │     └── 模块名称:design-demo模块                                    //设计模式实战落地(已实现并有博文总结)
    │     └── 模块名称:elasticsearch-demo模块                             //ElasticSearch集成(已实现并有博文总结)
    │     └── 模块名称:mongodb-demo模块                                   //MongoDB集成(已实现并有博文总结)
    │     └── 模块名称:redis-demo模块                                     //Redis集成(已实现并有博文总结)
    │     └── 模块名称:spring-boot-demo模块                               //Spring Boot快速构建应用(已实现并有博文总结)
    │     └── 模块名称:spring-cloud-alibaba-nacos-demo模块                //Spring Cloud Alibaba Nacos集成(已实现并有博文总结)
    │     └── 模块名称:spring-cloud-alibaba-seata-demo模块                //Spring Cloud Alibaba Seata集成(已实现并有博文总结)
    │     └── 模块名称:spring-cloud-alibaba-sentinel-demo模块             //Spring Cloud Alibaba Sentinel集成(已实现并有博文总结)
    │     └── 模块名称:spring-cloud-gateway-demo模块                      //Spring Cloud Gateway集成(已实现并有博文总结)
    │     └── 模块名称:spring-cloud-hystrix-demo模块                      //Spring Cloud Hystrix集成(已实现并有博文总结)
    │     └── 模块名称:spring-cloud-open-feign-demo模块                   //Spring Cloud Open Feign集成(已实现并有博文总结)
    │     └── 模块名称:spring-cloud-ribbon-demo模块                       //Spring Cloud Ribbon集成(已实现并有博文总结)
    │     └── 模块名称:spring-cloud-security-oauth2-demo模块              //Spring Cloud Security Oauth2集成(已实现并有博文总结)
    │     └── 模块名称:spring-cloud-security-oauth2-sso-client-demo模块   //Spring Cloud Security Oauth2集成(已实现并有博文总结)
    │     └── 模块名称:spring-cloud-skywalking-demo模块                   //Spring Cloud Skywalking集成(已实现并有博文总结)
    │     └── 模块名称:spring-cloud-stream-demo模块                       //Spring Cloud Stream集成(已实现并有博文总结)
    │     └── 模块名称:swagger-demo模块                                   //springfox-swagger2集成(已实现并有博文总结)
    │     └── 模块名称:xxl-job模块                                        //xxl-job集成(已实现并有博文总结)
    │     └── 模块名称:apache-spark-demo模块                              //Apache Spark集成
    │     └── 模块名称:etl-hdfs-hive-hbase-demo模块                       //ETL、HDFS、Hive、Hbase集成
    │     └── 模块名称:ddd-mode-demo模块                                  //DDD领域设计
    │     └── 模块名称:netty-demo模块                                     //Netty集成
    │     └── 模块名称:vue-demo模块                                       //前端vue集成
    ├── document                                                        // 文档
    │     └── JavaKnowledgeDocument                                     //java知识点
    │           └── java基础知识点.md                     
    │           └── mq知识点.md
    │           └── mysql知识点.md
    │           └── redis知识点.md
    │           └── springcould知识点.md
    │           └── spring知识点.md
    │     └── FounderDocument                                           //创始人
    │           └── 创始人.md
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    系列文章:快速集成各种微服务相关的技术,帮助大家可以快速集成到自己的项目中,节约开发时间。
    提示:系列文章还未全部完成,后续的文章,会慢慢补充进去的。

    本章会结合【java_wxid项目】【第五章】【Spring Cloud Hystrix集成】的spring-cloud-hystrix-demo项目进行demo演示

    创建spring-cloud-gateway-demo项目

    项目代码:https://gitee.com/java_wxid/java_wxid/tree/master/demo/spring-cloud-gateway-demo
    项目结构如下(示例):
    在这里插入图片描述

    修改pom.xml

    代码如下(示例):

    <?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>spring-cloud-gateway-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>spring-cloud-gateway-demo</name>
        <description>Demo project for Spring Boot</description>
    
        <!--    属性配置-->
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <!--引入 Spring BootSpring CloudSpring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
            在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系-->
            <spring.boot.version>2.3.12.RELEASE</spring.boot.version>
            <spring.cloud.version>Hoxton.SR12</spring.cloud.version>
            <spring.cloud.alibaba.version>2.2.7.RELEASE</spring.cloud.alibaba.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <!--        代表web模块,在这个模块中含了许多JAR包,有spring相关的jar,内置tomcat服务器,jackson等,这些web项目中常用的的功能都会自动引入-->
    <!--        <dependency>-->
    <!--            <groupId>org.springframework.boot</groupId>-->
    <!--            <artifactId>spring-boot-starter-web</artifactId>-->
    <!--        </dependency>-->
    
            <!-- Alibaba Nacos 配置 -->
            <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>
            <!--SpringBoot 2.4.x的版本之后,对于bootstrap.properties/bootstrap.yaml配置文件
            (我们合起来成为Bootstrap配置文件)的支持,其实这个jar包里什么都没有,
            就只有一个标识类Marker,用来标识要开启Bootstrap配置文件的支持,由于父类用了2.5.6版本需要导入如下的依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-bootstrap</artifactId>
                <version>3.1.0</version>
            </dependency>
    
            <!--        网关配置-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-gateway</artifactId>
            </dependency>
    <!--        spring cloud gateway是基于webflux的,如果非要web支持的话需要导入spring-boot-starter-webflux,
    而不是spring-boot-start-web。-->
    <!--        <dependency>-->
    <!--            <groupId>org.springframework.boot</groupId>-->
    <!--            <artifactId>spring-boot-starter-webflux</artifactId>-->
    <!--        </dependency>-->
    
        </dependencies>
    
    
        <!--
            引入 Spring BootSpring CloudSpring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
            在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
         -->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-parent</artifactId>
                    <version>${spring.boot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <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>
    
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <encoding>${project.build.sourceEncoding}</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122

    创建bootstrap.yml文件

    代码如下(示例):

    #bootstrap.yml优先级比application.yml优先级高
    spring:
      #prefix−{spring.profile.active}.${file-extension}
      #nacos会根据当前环境去拼接配置名称查找相应配置文件,
      #示例:{spring.application.name}-{spring.profiles.active}-{spring.cloud.nacos.config.file-extension}
      #获取到值:nacos-autoconfig-service-dev.yml
      profiles:
        #开发环境dev,测试环境test,生产环境prod
        active: dev
      application:
        #配置应用的名称,用于获取配置
        name: gateway-demo
      cloud:
        nacos:
          discovery:
            # 服务注册地址
            server-addr: 106.14.132.94:8848
          config:
            #nacos配置中心地址
            server-addr: 106.14.132.94:8848
            #配置中心的命名空间id
            namespace: 9e50b6d9-6c3d-4e7a-b701-10f085e4b98d
            #配置分组,默认没有也可以
            group: DEFAULT_GROUP
            #配置文件后缀,用于拼接配置配置文件名称,目前只支持yaml和properties
            file-extension: yaml
            #配置自动刷新
            refresh-enabled: true
            #配置文件的前缀,默认是application.name的值,如果配了prefix,就取prefix的值
            #prefix: nacos-autoconfig-service-${spring.profile.active}
            # 配置编码
            encode: UTF-8
            username: nacos
            password: nacos
        gateway:
          discovery:
            locator:
              enabled: false
              lowerCaseServiceId: true
          routes:
            - id: hystrix-route   #路由的ID,没有固定规则但要求唯一,建议配合服务名
              uri: lb://hystrix-demo #nacos的服务名称,匹配后提供服务的路由地址,也可以用uri: http://localhost:8805/
              predicates:
                - Path=/hystrix-demo/**         # 断言,路径相匹配的进行路由
              filters:
                - StripPrefix=1 #去掉服务名前缀
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    修改启动类SpringCloudGatewayDemoApplication

    代码如下(示例):

    package com.example.springcloudgatewaydemo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @EnableDiscoveryClient
    @SpringBootApplication
    public class SpringCloudGatewayDemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringCloudGatewayDemoApplication.class, args);
        }
    
        @Bean
        @LoadBalanced
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    校验gateway是否工作

    启动spring-cloud-hystrix-demo项目、spring-cloud-gateway-demo项目
    如下图(示例):
    在这里插入图片描述

    正常调用spring-cloud-hystrix-demo的接口,是这样的,请求地址:
    如下图(示例):
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    使用网关之后的请求地址:http://localhost:8806/hystrix-demo/hystrix/getNacosConfigure
    如下图(示例):

    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述
    可以发现已经经过网关分发到具体的服务了,gateway起作用了

  • 相关阅读:
    【Leetcode】面试题 02.02. 返回倒数第 k 个节点
    Capture One Pro 23:重新定义Raw图像处理的行业标准
    python Flask与微信小程序 统计管理
    多尺度残差超分辨率
    算法随想录第八天打卡|344.反转字符串,541. 反转字符串II, 卡码网:54.替换数字, 151.翻转字符串里的单词,卡码网:55.右旋转字符串
    DFS-4-N皇后问题
    六、MySql表的增删改查
    【FaceChain风格DIY手把手教程】无限场景风格LoRA与固定人物LoRA的融合(4Kstar!)
    GUI编程--PyQt5--QLineEdit
    持续测试(CT)实战经验分享
  • 原文地址:https://blog.csdn.net/java_wxid/article/details/126489878