修复bug
This commit is contained in:
+3
-1
@@ -3,6 +3,7 @@ package com.jero.handler;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.gateway.route.RouteLocator;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
import springfox.documentation.swagger.web.SwaggerResource;
|
||||
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
|
||||
@@ -13,6 +14,7 @@ import java.util.*;
|
||||
* 聚合各个服务的swagger接口
|
||||
*/
|
||||
@Component
|
||||
@Primary
|
||||
public class MySwaggerResourceProvider implements SwaggerResourcesProvider {
|
||||
/**
|
||||
* swagger2默认的url后缀
|
||||
@@ -44,7 +46,7 @@ public class MySwaggerResourceProvider implements SwaggerResourcesProvider {
|
||||
.filter(route -> !self.equals(route.getUri().getHost()))
|
||||
.subscribe(route -> routeHosts.add(route.getUri().getHost()));
|
||||
|
||||
// 记录已经添加过的server,存在同一个应用注册了多个服务在eureka上
|
||||
// 记录已经添加过的server,存在同一个应用注册了多个服务在nacos上
|
||||
Set<String> dealed = new HashSet<>();
|
||||
routeHosts.forEach(instance -> {
|
||||
// 拼接url
|
||||
|
||||
+12
-7
@@ -128,7 +128,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
* @return
|
||||
*/
|
||||
private void loadRoutesByRedis() {
|
||||
List<RouteDefinition> routes = Lists.newArrayList();
|
||||
List<MyRouteDefinition> routes = Lists.newArrayList();
|
||||
configService = createConfigService();
|
||||
if (configService == null) {
|
||||
log.warn("initConfigService fail");
|
||||
@@ -143,9 +143,14 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
for (RouteDefinition definition : routes) {
|
||||
for (MyRouteDefinition definition : routes) {
|
||||
log.info("update route : {}", definition.toString());
|
||||
dynamicRouteService.add(definition);
|
||||
Integer status=definition.getStatus();
|
||||
if(status.equals(0)){
|
||||
dynamicRouteService.delete(definition.getId());
|
||||
}else{
|
||||
dynamicRouteService.add(definition);
|
||||
}
|
||||
}
|
||||
this.publisher.publishEvent(new RefreshRoutesEvent(this));
|
||||
}
|
||||
@@ -160,13 +165,13 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
* @param array
|
||||
* @return
|
||||
*/
|
||||
|
||||
public static List<RouteDefinition> getRoutesByJson(JSONArray array) throws URISyntaxException {
|
||||
List<RouteDefinition> ls = new ArrayList<>();
|
||||
public static List<MyRouteDefinition> getRoutesByJson(JSONArray array) throws URISyntaxException {
|
||||
List<MyRouteDefinition> ls = new ArrayList<>();
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
JSONObject obj = array.getJSONObject(i);
|
||||
RouteDefinition route = new RouteDefinition();
|
||||
MyRouteDefinition route = new MyRouteDefinition();
|
||||
route.setId(obj.getString("routerId"));
|
||||
route.setStatus(obj.getInteger("status"));
|
||||
Object uri = obj.get("uri");
|
||||
if (uri == null) {
|
||||
route.setUri(new URI("lb://" + obj.getString("name")));
|
||||
|
||||
+7
-8
@@ -47,14 +47,13 @@ public class DynamicRouteService implements ApplicationEventPublisherAware {
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public synchronized Mono<ResponseEntity<Object>> delete(String id) {
|
||||
return this.repository.delete(Mono.just(id)).then(Mono.defer(() -> {
|
||||
return Mono.just(ResponseEntity.ok().build());
|
||||
})).onErrorResume((t) -> {
|
||||
return t instanceof NotFoundException;
|
||||
}, (t) -> {
|
||||
return Mono.just(ResponseEntity.notFound().build());
|
||||
});
|
||||
public synchronized void delete(String id) {
|
||||
try {
|
||||
repository.delete(Mono.just(id)).subscribe();
|
||||
this.publisher.publishEvent(new RefreshRoutesEvent(this));
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.jero.loader;
|
||||
|
||||
import org.springframework.cloud.gateway.route.RouteDefinition;
|
||||
|
||||
/**
|
||||
* 自定义RouteDefinition
|
||||
* @author zyf
|
||||
*/
|
||||
public class MyRouteDefinition extends RouteDefinition {
|
||||
/**
|
||||
* 路由状态
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Integer status) {
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user