• SpringBoot整合Gateway 的Demo(附源码)


    源码,可直接下载

    Gateway模块

    Gateway 的父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>
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.6.12</version>
    		<relativePath/> <!-- lookup parent from repository -->
    	</parent>
    	<packaging>pom</packaging>
    	<groupId>com.example</groupId>
    	<artifactId>gateway-demo</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<name>gateway-demo</name>
    	<description>gateway-demo</description>
    	<properties>
    		<java.version>1.8</java.version>
    	</properties>
    
    	<dependencyManagement>
    		<dependencies>
    
    			<dependency>
    				<groupId>com.alibaba.cloud</groupId>
    				<artifactId>spring-cloud-alibaba-dependencies</artifactId>
    				<version>2021.0.4.0</version>
    				<type>pom</type>
    				<scope>import</scope>
    			</dependency>
    
    			<dependency>
    				<groupId>org.springframework.cloud</groupId>
    				<artifactId>spring-cloud-dependencies</artifactId>
    				<version>2021.0.4</version>
    				<type>pom</type>
    				<scope>import</scope>
    			</dependency>
    
    		</dependencies>
    	</dependencyManagement>
    	<modules>
    		<module>gateway</module>
    	</modules>
    
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    			</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

    Gateway 的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>
    <!--    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.7.17</version>
            <relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;
        </parent>-->
        <parent>
            <artifactId>gateway-demo</artifactId>
            <groupId>com.example</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <groupId>com.example</groupId>
        <artifactId>gateway</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>gateway</name>
        <description>gateway</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-loadbalancer</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-gateway</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-web</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-bootstrap</artifactId>
            </dependency>
    
            <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-openfeign</artifactId>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </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

    添加注册中心和配置中心配置

    spring:
      application:
        name: gateway-demo
      cloud:
        nacos:
          discovery:
            server-addr: localhost:8848
          config:
            file-extension: yml
            server-addr: localhost:8848
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    添加路由配置信息(放在配置中心)

    # 此文件内容最好放到配置中心nacos中
    spring:
      cloud:
        gateway:
          routes:
            - id: serviceDemo
              uri: lb://service-demo
              # uri: http://www.baidu.com
              predicates:
                - Path=/springboottest/**
              filters:
                # 校验
                # 去除一个前缀 springboottest
                # 请求方式为 http://localhost:8081/springboottest/test
                - StripPrefix=1
    logging:
      level:
        springfox: error
    server:
      port: 8081
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述

    服务模块

    添加一个service服务

    # 使用和gateway相同的父pom.xml
    
    • 1
    
        
            org.springframework.boot
            spring-boot-starter
        
    
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            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
        
    
    
    • 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
    // controller
    @RestController
    @RequestMapping(("test"))
    public class TestController {
    
        @Value("${server.port}")
        private String port;
    
    
        @GetMapping()
        public String getPort() {
            return port;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    配置注册中心和配置中心

    spring:
      application:
        name: service-demo
      cloud:
        nacos:
          config:
            server-addr: localhost:8848
            file-extension: yml
          discovery:
            server-addr: localhost:8848
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    配置中心的配置文件内容

    #此文件内容最好放到配置中心nacos中
    server:
      port: 8888
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    效果

    不通过gateway访问

    localhost:8888/test
    
    • 1

    在这里插入图片描述

    通过gateway访问

    在这里插入图片描述

    本文由博客一文多发平台 OpenWrite 发布!

  • 相关阅读:
    安卓毕业设计app项目成品在线投票app毕业设计作品
    单片机矩阵键盘
    Android - Monkey 测试应用出现Crash报错IllegalStateException
    在Webpack 5 中如何进行 CSS 常用配置?
    Python操作AST解JS混淆
    ubuntu22.04远程控制桌面的工具
    Java程序员常用的Eclipse键盘快捷键,建议收藏
    软件测试:功能测试常用的测试用例大全
    mybatis plus框架的@TableField注解不生效问题总结
    风力发电一键求助可视对讲终端
  • 原文地址:https://blog.csdn.net/weixin_42551369/article/details/134069767