使用maven搭建工程,maven工程中包括3個module 分別是eureka-server、gateway-server、service-demo。父pom文件如下
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>2.0.2.RELEASE</version> 5 <relativePath/> 6 </parent> 7 8 <modules> 9 <module>eureka-server</module> 10 <module>service-demo</module> 11 <module>gateway-server</module> 12 </modules> 13 14 <properties> 15 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 16 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 17 <java.version>1.8</java.version> 18 <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> 19 </properties> 20 21 <dependencies> 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-test</artifactId> 25 <scope>test</scope> 26 </dependency> 27 </dependencies> 28 29 <dependencyManagement> 30 <dependencies> 31 <dependency> 32 <groupId>org.springframework.cloud</groupId> 33 <artifactId>spring-cloud-dependencies</artifactId> 34 <version>${spring-cloud.version}</version> 35 <type>pom</type> 36 <scope>import</scope> 37 </dependency> 38 </dependencies> 39 </dependencyManagement> 40 41 <build> 42 <plugins> 43 <plugin> 44 <groupId>org.springframework.boot</groupId> 45 <artifactId>spring-boot-maven-plugin</artifactId> 46 </plugin> 47 </plugins> 48 </build>
1.eureka-server工程配置及代碼
1 <name>eureka-server</name> 2 <description>Demo project for Spring Boot</description> 3 4 <parent> 5 <groupId>com.xxx</groupId> 6 <artifactId>xxx</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 </parent> 9 10 <dependencies> 11 <dependency> 12 <groupId>org.springframework.cloud</groupId> 13 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 14 </dependency> 15 </dependencies>
application.yml

1 server: 2 port: 8761 3 4 eureka: 5 instance: 6 hostname: localhost 7 client: 8 registerWithEureka: false 9 fetchRegistry: false 10 serviceUrl: 11 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ 12 13 spring: 14 application: 15 name: eurka-server
EurekaServerApplication

1 @SpringBootApplication 2 @EnableEurekaServer 3 public class EurekaServerApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run( EurekaServerApplication.class, args ); 7 } 8 }
2.gateway-server代碼和配置
pom.xml

1 <parent> 2 <groupId>com.xxx</groupId> 3 <artifactId>xxx</artifactId> 4 <version>0.0.1-SNAPSHOT</version> 5 </parent> 6 7 <dependencies> 8 <dependency> 9 <groupId>org.springframework.cloud</groupId> 10 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 11 </dependency> 12 <dependency> 13 <groupId>org.springframework.cloud</groupId> 14 <artifactId>spring-cloud-starter-gateway</artifactId> 15 </dependency> 16 17 <dependency> 18 <groupId>org.springframework.boot</groupId> 19 <artifactId>spring-boot-starter-test</artifactId> 20 <scope>test</scope> 21 </dependency> 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-data-redis-reactive</artifactId> 25 </dependency> 26 </dependencies> 27 28 <build> 29 <plugins> 30 <plugin> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-maven-plugin</artifactId> 33 </plugin> 34 </plugins> 35 </build>
application.yml
- filter名稱必須是RequestRateLimiter
- redis-rate-limiter.replenishRate:允許用戶每秒處理多少個請求
- redis-rate-limiter.burstCapacity:令牌桶的容量,允許在一秒鍾內完成的最大請求數
- key-resolver:使用SpEL按名稱引用bean
設置 redis-rate-limiter 的 參數都為1 供測試 注意! redis不要使用windows版本 有的命令不存在 !

1 server: 2 port: 8888 3 4 spring: 5 application: 6 name: gateway-server 7 redis: 8 host: 127.0.0.1 9 port: 6379 10 cloud: 11 gateway: 12 discovery: 13 locator: 14 enabled: false 15 lowerCaseServiceId: true 16 routes: 17 - id: service-hi 18 uri: lb://SERVICE-HI 19 predicates: 20 - Path=/demo/** 21 filters: 22 - StripPrefix=1 23 - name: RequestRateLimiter 24 args: 25 key-resolver: '#{@uriKeyResolver}' 26 redis-rate-limiter.replenishRate: 1 27 redis-rate-limiter.burstCapacity: 1 28 - AddResponseHeader=X-Response-Foo, Bar 29 eureka: 30 client: 31 service-url: 32 defaultZone: http://localhost:8761/eureka/
UriKeyResolver 通過什么限流 實現 KeyResolver接口 ,可以實現其他方式 這里做測試 寫死返回值

1 public class UriKeyResolver implements KeyResolver { 2 3 @Override 4 public Mono<String> resolve(ServerWebExchange exchange) { 5 // return Mono.just(exchange.getRequest().getURI().getPath()); 6 String user = "1"; 7 return Mono.just(user); 8 } 9 10 }
GatewayApplication

1 @SpringBootApplication 2 @EnableEurekaClient 3 public class GatewayApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(GatewayApplication .class, args); 7 } 8 9 @Bean 10 public UriKeyResolver uriKeyResolver() { 11 return new UriKeyResolver(); 12 }
3.service-demo代碼和配置
pom.xml

1 <parent> 2 <groupId>com.xxx</groupId> 3 <artifactId>xxxx</artifactId> 4 <version>0.0.1-SNAPSHOT</version> 5 </parent> 6 7 <dependencies> 8 <dependency> 9 <groupId>org.springframework.cloud</groupId> 10 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 11 </dependency> 12 <dependency> 13 <groupId>org.springframework.boot</groupId> 14 <artifactId>spring-boot-starter-web</artifactId> 15 </dependency> 16 </dependencies> 17 18 <build> 19 <plugins> 20 <plugin> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-maven-plugin</artifactId> 23 </plugin> 24 </plugins> 25 </build>
application.yml

1 server: 2 port: 8762 3 4 spring: 5 application: 6 name: service-demo 7 8 eureka: 9 client: 10 serviceUrl: 11 defaultZone: http://localhost:8761/eureka/
ServiceDemoApplication 聲明hi接口

1 @SpringBootApplication 2 @EnableEurekaClient 3 @RestController 4 /** 5 * @author chaixg 6 */ 7 public class ServiceDemoApplication { 8 9 public static void main(String[] args) { 10 SpringApplication.run(ServiceDemoApplication.class, args); 11 } 12 13 @Value("${server.port}") 14 String port; 15 16 @RequestMapping("/hi") 17 public String home(@RequestParam(value = "name", defaultValue = "xxx") String name) { 18 return "hi " + name + " ,i am from port:" + port; 19 }
依次啟動三個工程
先在瀏覽器上測試下是否通了
如果頻繁的刷新會出現429 因為上面設置的是一個令牌一個請求
over....