Bootstrap

项目集成GateWay

1.环境搭建

1.创建sunrays-common-cloud-gateway-starter模块

CleanShot 2025-01-07 at 17.16.33@2x

2.目录结构

CleanShot 2025-01-07 at 19.02.19@2x

3.自动配置
1.GateWayAutoConfiguration.java
package com.sunxiansheng.cloud.gateway.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * Description: GateWay自动配置类
 *
 * @Author sun
 * @Create 2025/1/7 17:19
 * @Version 1.0
 */
@Slf4j
@Configuration
public class GateWayAutoConfiguration {

    /**
     * 自动配置成功日志
     */
    @PostConstruct
    public void logConfigSuccess() {
        log.info("GateWayAutoConfiguration has been loaded successfully!");
    }
}
2.spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.sunxiansheng.cloud.gateway.config.GateWayAutoConfiguration
3.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.sunxiansheng</groupId>
        <artifactId>sunrays-common-cloud</artifactId>
        <version>2.0.0</version>
    </parent>

    <artifactId>sunrays-common-cloud-gateway-starter</artifactId>

    <dependencies>
        <!-- geteway -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <!-- 排除logging -->
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-logging</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 负载均衡器,网关使用lb方式时引入 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
        </dependency>
        <!-- Nacos的服务发现,如果GateWay需要动态路由时引入 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>
</project>
4.注意:GateWay不能跟Web一起引入!
;