网关

# 网关

添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
1
2
3
4

配置:

server:
  port: 40000
spring:
  application:
    name: my-gateway
  cloud:
    gateway:
      discovery:
        locator:
          lowerCaseServiceId: true
      routes:
        - id: Ucenter
          uri: lb://u-center
          predicates:
            - Path=/uc/**
          filters:
            - StripPrefix=1
        - id: Device
          uri: lb://mo-yu-jun
          predicates:
            - Path=/my/**
          filters:
            - StripPrefix=1
    nacos:
      discovery:
        server-addr: 192.168.220.128:8848
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

允许跨域的配置

@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(Boolean.TRUE);
        config.addAllowedMethod("*");
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.setMaxAge(3600L);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
上次更新: 2022/03/28, 23:04:38