准备做RedisRequestLimit

This commit is contained in:
huangsimin 2019-07-22 18:30:56 +08:00
parent 149d9def89
commit 93c1f1742e
7 changed files with 187 additions and 11 deletions

View File

@ -71,6 +71,11 @@
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
</dependencies>
<dependencyManagement></dependencyManagement>
@ -112,12 +117,10 @@
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
</project>

View File

@ -14,6 +14,7 @@ import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.RequestRateLimiterGatewayFilterFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
@ -97,7 +98,7 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dubb
// }
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
ServerHttpRequest req = exchange.getRequest();
HttpHeaders headers = req.getHeaders();

View File

@ -390,16 +390,40 @@ public class ConfigGateway implements RouteDefinitionLocator {
private void parseAndAddDubboFilters(String dubboUri, List<FilterDefinition> filters,
LinkedHashMap<String, List<String>> iter, String yamlField) {
List<String> filtersYaml = iter.get(yamlField);
filters.addAll(defaultFilters);
if (filtersYaml != null) {
for (String filterString : filtersYaml) {
FilterDefinition fd = new FilterDefinition(filterString);
log.info(fd.getName());
if (!fd.getName().equals("Dubbo")) {
filters.add(fd);
for (Object filterObject : filtersYaml) {
// 现阶段只支持两种情况
if(filterObject.getClass() == String.class) {
String filterString = (String)filterObject;
FilterDefinition fd = new FilterDefinition(filterString);
log.info(fd.getName());
if (!fd.getName().equals("Dubbo")) {
filters.add(fd);
}
} else if (filterObject.getClass() == LinkedHashMap.class) {
Map<String, Object> filterMap = (LinkedHashMap)filterObject;
FilterDefinition fd = new FilterDefinition();
fd.setName((String)filterMap.get("name"));
if(filterMap.containsKey("args")) {
Object args = filterMap.get("args");
for( Entry<String, Object> kv : ((Map<String, Object>)args).entrySet()) {
if(kv.getValue().getClass() != String.class) {
kv.setValue( String.valueOf(kv.getValue()));
}
}
fd.setArgs((Map<String,String>)args);
}
log.info(fd.getName());
if (!fd.getName().equals("Dubbo")) {
filters.add(fd);
}
}
}
}

View File

@ -1,10 +1,12 @@
package cn.ecpark.service.usergw.config.bean;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import cn.ecpark.service.usergw.biz.filters.bean.GenericServicePool;
import cn.ecpark.service.usergw.biz.filters.factory.DubboGatewayFilterFactory;
import reactor.core.publisher.Mono;
@Component
public class ConfigBean {
@ -17,4 +19,9 @@ public class ConfigBean {
public DubboGatewayFilterFactory dubboFilterFactory() {
return new DubboGatewayFilterFactory();
}
@Bean
KeyResolver ipResolver() {
return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostString());
}
}

View File

@ -0,0 +1,58 @@
default-filters:
- AddResponseHeader=X-Response-Default-Foo, Test-Default-Bar
restful: # filters 必须 有predicates uri只是预期到达的地址
routes:
- id: haha
uri: http://localhost:4444
order: 0
predicates:
- Path=/http/hello
- id: rewrite
uri: http://localhost:4444
order: 0
predicates:
- Path=/http/rewrite
filters:
- RewritePath=/http/rewrite, /http/hello
- id: xixi
uri: http://localhost:8888
order: 11
predicates:
- Path=/http/xixi
filters:
- RedirectTo=302, http://localhost:4444/http/hello
dubbo:
routes:
- id: test
application: dubbo-exchange
# methods: # 如果没填就从 request拿 意味着所有接口都可以使用
# - name: Say
# param-types:
# - java.lang.String
# - name: Hello
connections: 4
group: test
registry: zookeeper://127.0.0.1:2181
interface: ocean.demo.api.IExchange
version: 1.0.0
predicates:
- Path=/dubbo/hello
filters:
- name: RequestRateLimiter
args:
key-resolver: '#{@ipResolver}' #SPEL表达式去的对应的bean
redis-rate-limiter.replenishRate: 81 # 令牌桶的容积
redis-rate-limiter.burstCapacity: 40 # 流速 每秒
# - name: RequestRateLimiter1
# args:
# redis-rate-limiter.replenishRate: 1 # 令牌桶的容积
# redis-rate-limiter.burstCapacity: 3 # 流速 每秒
# key-resolver: "#{@ipResolver}" #SPEL表达式去的对应的bean

View File

@ -0,0 +1,62 @@
package cn.ecpark.service.usergw;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.cloud.gateway.filter.factory.RequestRateLimiterGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.SetRequestHeaderGatewayFilterFactory;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import reactor.netty.http.client.HttpClient;
import reactor.netty.http.client.HttpClient.ResponseReceiver;
import reactor.netty.http.client.HttpClientResponse;
// TODO: 添加附加Dubbo Service for Test启动进程
@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
@TestPropertySource(locations = { "application4.properties" })
public class TestHttp2DubboConfig4 {
@LocalServerPort
private int serverPort;
@Test
public void Test1RequestHttp2DubboSetHeader() {
// Test Base Url
for(int i = 0; i < 2;i++) {
HttpClient client = HttpClient.create();
ResponseReceiver<?> receiver;
String content;
receiver = client.baseUrl("http://localhost:" + serverPort + "/dubbo/hello")
.headers(h -> h.set("method", "Hello")).get();
HttpClientResponse response = receiver.response().block();
Assert.assertNotNull(response);
Assert.assertEquals(response.status().code(), 200);
content = receiver.responseContent().asString().blockLast();
Assert.assertNotNull(content);
Assert.assertEquals(content, "Hello Dubbo");
}
}
@Test
@SuppressWarnings("unchecked")
public void Test3RestfulFilters() {
}
}

View File

@ -0,0 +1,21 @@
spring.redis.host=localhost
spring.redis.port=8379
spring.redis.database: 0
spring.application.name=gateway
dubbo.scan.base-packages=cn.ecpark.service.usergw.impl
dubbo.protocol.name=dubbo
dubbo.protocol.port=20999
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.config-center.address=zookeeper://127.0.0.1:2181
dubbo.metadata-report.address=zookeeper://127.0.0.1:2181
server.port=8888
# logging.level.org.springframework.cloud.gateway=debug
logging.file=logs/log
yame.config=test-gateway4.yaml