创建网关服务Gateway
本文需要对IDEA有一定的了解,清楚IDEA中项目与文件的创建,后续迭代补充基础知识
前期准备
- 通过IDEA创建module dsz-gateway
- 依赖项目dsz-root,dsz-eureka
Spring Cloud版本选型
- Greenwich SR2
- Spring Boot 2.1.6.RELEASE
- Spring 5.1.8.RELEASE
- jdk 1.8.0_172
最终展示pom.xml和项目结构
项目结构
pom.xml
dsz-root
com.dsz.platform
1.0-SNAPSHOT
4.0.0
dsz-gateway
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-gateway
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-data-redis-reactive
application.yml
server:
port: 8763
spring:
application:
name: dsz-gateway
cloud:
gateway:
##跨域
globalcors:
cors-configurations:
'[/**]':
allowedHeaders: "*"
allowedOrigins: "*"
allowedMethods:
- GET
- POST
- DELETE
- PUT
- OPTION
discovery:
locator:
## discovery路由,false可以自己控制每个路由
enabled: false
## 启用serviceId启用小写
lower-case-service-id: true
default-filters:
- name: Hystrix
args:
name: fallbackcmd
fallbackUri: forward:/fallbackCmd
eureka:
instance:
hostname: 127.0.0.1
## 心跳间隔-5秒
lease-renewal-interval-in-seconds: 5
## 没有心跳的淘汰时间-10秒
lease-expiration-duration-in-seconds: 10
client:
## 刷新本地缓存-5秒
registry-fetch-interval-seconds: 5
service-url:
defaultZone: http://admin:admin@${eureka.instance.hostname}:8761/eureka/
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000
logging:
level:
org.springframework.cloud.gateway: trace
org.springframework.http.server.reactive: debug
org.springframework.web.reactive: debug
reactor.ipc.netty: debug
com.dsz.base.gateway.GatewayApplication
package com.dsz.base.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class , args);
}
}
com.dsz.base.gateway.router.CustomerRouter
package com.dsz.base.gateway.router;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import reactor.core.publisher.Mono;
@Configuration
public class CustomerRouter {
@Bean
RedisRateLimiter redisRateLimiter() {
return new RedisRateLimiter(10, 20);
}
/**
* 获取请求用户ip作为限流key
* @return
*/
@Bean
@Primary
public KeyResolver hostKeyResolver() {
return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
}
/**
* 获取请求用户id作为限流key
* @return
*/
@Bean
public KeyResolver userKeyResolver() {
return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("userId"));
}
/**
* 获取请求地址的uri作为限流key
* @return
*/
@Bean
KeyResolver apiKeyResolver() {
return exchange -> Mono.just(exchange.getRequest().getPath().value());
}
/**
*
* Fluent Java Routes API
*
* @param builder
* @return
*/
@Bean
public RouteLocator demoRouteLocatorBuilder(RouteLocatorBuilder builder) {
return builder
.routes()
.route("dsz-demo",
r -> r.path("/demo/**","/dsz-demo/**")
//添加过滤器
.filters(f ->f.stripPrefix(1)
.requestRateLimiter(c -> c.setRateLimiter(redisRateLimiter())
.setKeyResolver(hostKeyResolver()))
)
.uri("lb://dsz-demo")
)
.build();
}
}
com.dsz.base.gateway.controller.HystrixController
package com.dsz.base.gateway.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HystrixController {
@RequestMapping("/fallbackCmd")
public String fallbackCmd() {
return "fail fast!";
}
}