pom引入依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><!-- 服务注册 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>
application.yml文件内容
server:port: 8099
spring:application:name: serviceGateway# nacos服务地址cloud:nacos:discovery:server-addr: localhost:8848gateway:#使用服务发现路由discovery:locator:enabled: trueroutes:- id: service-hosp #设置路由id #设置路由断言,代理servicerId为auth-service的/auth/路径uri: lb://serviceHosp #设置路由的uripredicates:- Path=/*/hosp/**- id: service-cmn uri: lb://service-cmnpredicates:- Path=/*/cmn/**
处理跨越问题
@Configuration
public class CorsConfig {@Beanpublic CorsWebFilter corsFilter() {CorsConfiguration config = new CorsConfiguration();config.addAllowedMethod("*");config.addAllowedOrigin("*");config.addAllowedHeader("*");UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());source.registerCorsConfiguration("/**", config);return new CorsWebFilter(source);}
}
前端访问后端端口直接配置为网关的端口,路径不变
nacos服务列表为