ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//package jnpf.config;
//
//import com.github.xiaoymin.knife4j.spring.gateway.Knife4jGatewayProperties;
//import com.github.xiaoymin.knife4j.spring.gateway.discover.ServiceDiscoverHandler;
//import com.github.xiaoymin.knife4j.spring.gateway.enums.OpenApiVersion;
//import com.github.xiaoymin.knife4j.spring.gateway.spec.v2.OpenAPI2Resource;
//import com.github.xiaoymin.knife4j.spring.gateway.utils.PathUtils;
//import lombok.Getter;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.config.GatewayProperties;
//import org.springframework.cloud.gateway.route.RouteDefinition;
//import org.springframework.cloud.gateway.support.NameUtils;
//import org.springframework.context.EnvironmentAware;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//import org.springframework.core.env.Environment;
//import org.springframework.util.StringUtils;
//
//import jakarta.annotation.Resource;
//import java.nio.charset.StandardCharsets;
//import java.util.*;
//
//@Slf4j
//public class MyServiceDiscoverHandler extends ServiceDiscoverHandler {
//
//    /**
//     * Knife4j gateway properties
//     */
//    @Autowired
//    private Knife4jGatewayProperties gatewayProperties;
//
//    /**
//     * 聚合内容
//     */
//    @Getter
//    private List<OpenAPI2Resource> gatewayResources;
//
//    @Autowired
//    private GatewayProperties gatewayPropertiess;
//
//    /**
//     * Spring Environment
//     */
//    private Environment environment;
//
//    public MyServiceDiscoverHandler(Knife4jGatewayProperties gatewayProperties) {
//        super(gatewayProperties);
//    }
//
//    /**
//     * 处理注册中心的服务
//     * @param service 服务列表集合
//     */
//    @Override
//    public void discover(List<String> service) {
//
//        log.debug("service has change.");
//        Set<String> excludeService = getExcludeService();
//        // 版本
//        OpenApiVersion apiVersion = this.gatewayProperties.getDiscover().getVersion();
//        // 判断当前类型
//        String url = this.gatewayProperties.getDiscover().getUrl();
//        // 个性化服务的配置信息
//        Map<String, Knife4jGatewayProperties.ServiceConfigInfo> configInfoMap = this.gatewayProperties.getDiscover().getServiceConfig();
//        List<OpenAPI2Resource> resources = new ArrayList<>();
//        if (service != null && !service.isEmpty()) {
//            for (String serviceName : service) {
//                if (!serviceName.startsWith("jnpf-") || serviceName.equals("jnpf-datareport") || excludeService.contains(serviceName)) {
//                    continue;
//                }
//                int order = 0;
//                String groupName = serviceName;
//                String contextPath = "/";
////                Knife4jGatewayProperties.ServiceConfigInfo serviceConfigInfo = configInfoMap.get(serviceName);
////                if (serviceConfigInfo != null) {
////                    order = serviceConfigInfo.getOrder();
////                    groupName = serviceConfigInfo.getGroupName();
////                    contextPath = PathUtils.append(contextPath, serviceConfigInfo.getContextPath());
////                }
//                OpenAPI2Resource resource = new OpenAPI2Resource(order, true);
//                resource.setName(groupName);
//                RouteDefinition routeDefinition = gatewayPropertiess.getRoutes().stream().filter(t -> t.getId().equals(serviceName)).findFirst().orElse(new RouteDefinition());
//                String replace = routeDefinition.getPredicates().get(0).getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0").replace("/**", "");
//                resource.setContextPath(replace);
////                // 判断版本
////                if (apiVersion == OpenApiVersion.OpenAPI3) {
////                    if (contextPath.equalsIgnoreCase("/")) {
////                        // 自动追加一个serviceName
////                        resource.setContextPath("/" + serviceName);
////                    }
////                }
//                resource.setUrl(PathUtils.append(PathUtils.append(replace, url), contextPath + serviceName));
//                resource.setId(Base64.getEncoder().encodeToString((resource.getName() + resource.getUrl() + resource.getContextPath()).getBytes(StandardCharsets.UTF_8)));
//                resources.add(resource);
//            }
//        }
//        super.discover(service);
//    }
//
//}