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

网友投稿 682 2023-02-21

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

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

1. 将 actuator 端点暴露出来

management:

endpoints:

web:

exposure:

include: "*"

2. redis 配置

https://jb51-/article/203766.htm

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 getRouteDefinitions() {

List 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 save(Mono route) {

return route.flatMap(routeDefinition -> {

redisTemplate.opsForHash().put(GATEWAY_ROUTES, routeDefinition.getId(), JSONObject.toJSONString(routeDefinition));

return Mono.empty();

});

}

@Override

public Mono 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> delete(String id) {

return redisRouteDefinitionRepository.delete(Mono.just(id)).then(Mono.defer(() -> Mono.just(ResponseEntity.ok().build())))

.onErrorResume(t -> t instanhttp://ceof 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> 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/(?.*)",

"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/(?.*)",

"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/(?.*)",

"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/(?.*)",

"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  , 都可以看到配置成功。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:如何用idea数据库编写快递e站
下一篇:vue小程序如何开发(vue怎么开发微信小程序)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~