依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
启动类
@SpringBootApplication @EnableEurekaClient public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
application.yml 文件
spring: application: name: sysgateway cloud: gateway: routes: - id: goods uri: lb://goods predicates: - Path=/goods/** filters: - StripPrefix= 1 - id: system uri: lb://system predicates: - Path=/system/** filters: - StripPrefix= 1 server: port: 9101 eureka: client: service-url: defaultZone: http://127.0.0.1:6868/eureka instance: prefer-ip-address: true
微服务网关跨域
globalcors: cors-configurations: '[/**]': # 匹配所有请求 allowedOrigins: "*" #跨域处理 允许所有的域 allowedMethods: # 支持的方法 - GET - POST - PUT - DELETE
微服务网关过滤器
@Component public class IpFilter implements GlobalFilter, Ordered { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { System.out.println("经过第1个过滤器IpFilter"); ServerHttpRequest request = exchange.getRequest(); InetSocketAddress remoteAddress = request.getRemoteAddress(); System.out.println("ip:"+remoteAddress.getHostName()); return chain.filter(exchange); } @Override public int getOrder() { return 1; } }
标签:网关,服务,exchange,spring,springframework,public,cloud,搭建 From: https://www.cnblogs.com/Rover20230226/p/17523273.html