springcloud 中gateway的搭建
- 创建maven工程添加pom依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.7.RELEASE</version> </parent> <!-- 还需要添加springcloud依赖 --> <dependencyManagement> <dependencies> <!-- springcloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${springcloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
- 修改启动类
@SpringBootApplication @EnableEurekaClient public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
- 创建Resources 资源目录,创建
application.yml
文件,并添加如下配置server: port: 7004 spring: application: name: gateway cloud: gateway: globalcors: cors-configurations: '[/**]': # 匹配所有请求 allowCredentials: true allowedOrigins: "*" # 支持跨域,允许所有的域 allowedMethods: "*" # 支持的方法,允许所有的方法 allowedHeaders: "*" discovery: locator: enabled: true routes: - id: CLIENT01 uri: lb://CLIENT01 predicates: - Path=/CLIENT01/user/** filters: - StripPrefix=1 eureka: client: service-url: defaultZone: http://127.0.0.1:7001/eureka instance: prefer-ip-address: true
- 启动
eureka
启动gateway
即可
有什么不同见解可以在评论区共同讨论