-
Notifications
You must be signed in to change notification settings - Fork 12
/
RouterManagerEndpoint.java
63 lines (53 loc) · 1.99 KB
/
RouterManagerEndpoint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.weweibuy.gateway.router.endpoint;
import com.weweibuy.framework.common.core.model.dto.CommonCodeResponse;
import com.weweibuy.gateway.core.mode.event.CustomRefreshRoutesEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.event.RefreshRoutesResultEvent;
import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.EventListener;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
import java.util.Objects;
/**
* 动态路由 相关接口
*
* @author durenhao
* @date 2020/2/23 11:11
**/
@Slf4j
@RestController
@RequestMapping("/gateway/router")
public class RouterManagerEndpoint {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private RouteDefinitionLocator routeDefinitionLocator;
@PostMapping
public synchronized CommonCodeResponse refreshRoute() {
// 监听 com.weweibuy.gateway.router.dynamic.JdbcRouterDefinitionLocator.onApplicationEvent()
applicationContext.publishEvent(new CustomRefreshRoutesEvent(this));
return CommonCodeResponse.success();
}
@EventListener
public void onRefreshRoutesResultEvent(RefreshRoutesResultEvent event) {
Throwable throwable = event.getThrowable();
if (throwable != null) {
log.error("【路由刷新】>>> 刷新路由异常: ", throwable);
} else {
log.info("【路由刷新】>>> 刷新路由成功");
}
}
@GetMapping
public Mono listRoute() {
return routeDefinitionLocator.getRouteDefinitions()
.collectList();
}
@GetMapping("/{id}")
public Mono listRoute(@PathVariable String id) {
return routeDefinitionLocator.getRouteDefinitions()
.filter(r -> Objects.equals(r.getId(), id))
.collectList();
}
}