欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringCloud Gateway使用redis实现动态路由的方法

程序员文章站 2022-06-19 09:47:12
1. 将 actuator 端点暴露出来management:endpoints:web:exposure:include: "*"2. redis 配置3. 将原内存路由持久化到 redis@com...

1. 将 actuator 端点暴露出来

management:

endpoints:

web:

exposure:

include: "*"

2. redis 配置

3. 将原内存路由持久化到 redis

@component

public class redisroutedefinitionrepository implements routedefinitionrepository {

/**

* hash存储的key

*/

public static final string gateway_routes = "gateway_dynamic_route";

@resource

private stringredistemplate redistemplate;

/**

* 获取路由信息

* @return

*/

@override

public flux<routedefinition> getroutedefinitions() {

list<routedefinition> routedefinitions = new arraylist<>();

redistemplate.opsforhash().values(gateway_routes).stream()

.foreach(routedefinition -> routedefinitions.add(json.parseobject(routedefinition.tostring(), routedefinition.class)));

return flux.fromiterable(routedefinitions);

}

@override

public mono<void> save(mono<routedefinition> route) {

return route.flatmap(routedefinition -> {

redistemplate.opsforhash().put(gateway_routes, routedefinition.getid(), jsonobject.tojsonstring(routedefinition));

return mono.empty();

});

}

@override

public mono<void> delete(mono<string> routeid) {

return routeid.flatmap(id -> {

if (redistemplate.opsforhash().haskey(gateway_routes, id)) {

redistemplate.opsforhash().delete(gateway_routes, id);

return mono.empty();

}

return mono.defer(() -> mono.error(new notfoundexception("route definition is not found, routeid:" + routeid)));

});

}

}

4. 重写动态路由服务

@service

public class gatewaydynamicrouteservice implements applicationeventpublisheraware {

@resource

private redisroutedefinitionrepository redisroutedefinitionrepository;

private applicationeventpublisher applicationeventpublisher;

/**

* 增加路由

* @param routedefinition

* @return

*/

public int add(routedefinition routedefinition) {

redisroutedefinitionrepository.save(mono.just(routedefinition)).subscribe();

applicationeventpublisher.publishevent(new refreshroutesevent(this));

return 1;

}

/**

* 更新

* @param routedefinition

* @return

*/

public int update(routedefinition routedefinition) {

redisroutedefinitionrepository.delete(mono.just(routedefinition.getid()));

redisroutedefinitionrepository.save(mono.just(routedefinition)).subscribe();

applicationeventpublisher.publishevent(new refreshroutesevent(this));

return 1;

}

/**

* 删除

* @param id

* @return

*/

public mono<responseentity<object>> delete(string id) {

return redisroutedefinitionrepository.delete(mono.just(id)).then(mono.defer(() -> mono.just(responseentity.ok().build())))

.onerrorresume(t -> t instanceof notfoundexception, t -> mono.just(responseentity.notfound().build()));

}

@override

public void setapplicationeventpublisher(applicationeventpublisher applicationeventpublisher) {

this.applicationeventpublisher = applicationeventpublisher;

}

}

5. 对外暴露接口

@restcontroller

@requestmapping("/gateway")

public class gatewaydynamicroutecontroller {

@resource

private gatewaydynamicrouteservice gatewaydynamicrouteservice;

@postmapping("/add")

public string create(@requestbody routedefinition entity) {

int result = gatewaydynamicrouteservice.add(entity);

return string.valueof(result);

}

@postmapping("/update")

public string update(@requestbody routedefinition entity) {

int result = gatewaydynamicrouteservice.update(entity);

return string.valueof(result);

}

@deletemapping("/delete/{id}")

public mono<responseentity<object>> delete(@pathvariable string id) {

return gatewaydynamicrouteservice.delete(id);

}

}

测试

测试前删除我们配置的静态路由,因为静态路由和 redis 动态路由同时存在时取并集。

访问 http://localhost:2000/actuator/gateway/routes , 可以看到只有默认路由。

[

{

"route_id": "compositediscoveryclient_consul",

"route_definition": {

"id": "compositediscoveryclient_consul",

"predicates": [

{

"name": "path",

"args": {

"pattern": "/consul/**"

}

}

],

"filters": [

{

"name": "rewritepath",

"args": {

"regexp": "/consul/(?<remaining>.*)",

"replacement": "/${remaining}"

}

}

],

"uri": "lb://consul",

"order": 0

},

"order": 0

},

{

"route_id": "compositediscoveryclient_idc-gateway",

"route_definition": {

"id": "compositediscoveryclient_idc-gateway",

"predicates": [

{

"name": "path",

"args": {

"pattern": "/idc-gateway/**"

}

}

],

"filters": [

{

"name": "rewritepath",

"args": {

"regexp": "/idc-gateway/(?<remaining>.*)",

"replacement": "/${remaining}"

}

}

],

"uri": "lb://idc-gateway",

"order": 0

},

"order": 0

},

{

"route_id": "compositediscoveryclient_idc-provider1",

"route_definition": {

"id": "compositediscoveryclient_idc-provider1",

"predicates": [

{

"name": "path",

"args": {

"pattern": "/idc-provider1/**"

}

}

],

"filters": [

{

"name": "rewritepath",

"args": {

"regexp": "/idc-provider1/(?<remaining>.*)",

"replacement": "/${remaining}"

}

}

],

"uri": "lb://idc-provider1",

"order": 0

},

"order": 0

},

{

"route_id": "compositediscoveryclient_idc-provider2",

"route_definition": {

"id": "compositediscoveryclient_idc-provider2",

"predicates": [

{

"name": "path",

"args": {

"pattern": "/idc-provider2/**"

}

}

],

"filters": [

{

"name": "rewritepath",

"args": {

"regexp": "/idc-provider2/(?<remaining>.*)",

"replacement": "/${remaining}"

}

}

],

"uri": "lb://idc-provider2",

"order": 0

},

"order": 0

}

]

这个时候访问 http://192.168.124.5:2000/idc-provider1/provider1/1 根据结果可以推测能正确路由到 provider1, 测试结果一致。

创建 provider1 路由,将路径设置为 /p1/**,测试是否生效。

post 请求 http://localhost:2000/gateway/add

{

"id":"provider1",

"predicates":[

{

"name":"path",

"args":{

"_genkey_0":"/p1/**"

}

},

{

"name":"remoteaddr",

"args":{

"_genkey_0":"192.168.124.5/16"

}

}

],

"filters":[

{

"name":"stripprefix",

"args":{

"_genkey_0":"1"

}

}

],

"uri":"lb://idc-provider1",

"order":0

}

查看 redis 存储,或者请求 http://localhost:2000/actuator/gateway/routes  , 都可以看到配置成功。

到此这篇关于springcloud gateway使用redis实现动态路由的文章就介绍到这了,更多相关springcloud gateway动态路由内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!