ludc
2023-03-16 86b6157299a50579f454e4fb45a09ff21d252dab
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
102
103
104
105
106
107
108
109
110
111
112
/*
 *      Copyright (c) 2018-2028, DreamLu All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: DreamLu 卢春梦 (596392912@qq.com)
 */
package org.springblade.core.prometheus.service;
 
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.prometheus.data.ChangeItem;
import org.springblade.core.prometheus.data.Service;
import org.springblade.core.prometheus.data.ServiceHealth;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import reactor.core.publisher.Mono;
 
import java.util.*;
import java.util.function.Supplier;
 
/**
 * Returns Services and List of Service with its last changed
 *
 * @author L.cm
 */
@Slf4j
@RequiredArgsConstructor
public class RegistrationService {
    private static final String[] NO_SERVICE_TAGS = new String[0];
    private final DiscoveryClient discoveryClient;
 
    public Mono<ChangeItem<Map<String, String[]>>> getServiceNames(long waitMillis, Long index) {
        return returnDeferred(waitMillis, index, () -> {
            List<String> services = discoveryClient.getServices();
            Set<String> set = new HashSet<>(services);
            Map<String, String[]> result = new HashMap<>();
            for (String item : set) {
                result.put(item, NO_SERVICE_TAGS);
            }
            return result;
        });
    }
 
    public Mono<ChangeItem<List<Service>>> getService(String appName, long waitMillis, Long index) {
        return returnDeferred(waitMillis, index, () -> {
            List<ServiceInstance> instances = discoveryClient.getInstances(appName);
            List<Service> list = new ArrayList<>();
            if (instances == null || instances.isEmpty()) {
                return Collections.emptyList();
            }
            Set<ServiceInstance> instSet = new HashSet<>(instances);
            for (ServiceInstance instance : instSet) {
                Service service = Service.builder()
                    .address(instance.getHost())
                    .node(instance.getServiceId())
                    .serviceAddress(instance.getHost())
                    .servicePort(instance.getPort())
                    .serviceName(instance.getServiceId())
                    .serviceId(instance.getHost() + ":" + instance.getPort())
                    .nodeMeta(Collections.emptyMap())
                    .serviceMeta(instance.getMetadata())
                    .serviceTags(Collections.emptyList())
                    .build();
                list.add(service);
            }
            return list;
        });
    }
 
    public ServiceHealth getServiceHealth(Service instanceInfo) {
        String address = instanceInfo.getAddress();
        ServiceHealth.Node node = ServiceHealth.Node.builder()
            .name(instanceInfo.getServiceName())
            .address(address)
            .meta(Collections.emptyMap())
            .build();
        ServiceHealth.Service service = ServiceHealth.Service.builder()
            .id(instanceInfo.getServiceId())
            .name(instanceInfo.getServiceName())
            .tags(Collections.emptyList())
            .address(address)
            .meta(instanceInfo.getServiceMeta())
            .port(instanceInfo.getServicePort())
            .build();
        ServiceHealth.Check check = ServiceHealth.Check.builder()
            .node(instanceInfo.getServiceName())
            .checkId("service:" + instanceInfo.getServiceId())
            .name("Service '" + instanceInfo.getServiceId() + "' check")
            // nacos 实时性很高,可认定为健康
            .status("UP")
            .build();
        return ServiceHealth.builder()
            .node(node)
            .service(service)
            .checks(Collections.singletonList(check))
            .build();
    }
 
    private static <T> Mono<ChangeItem<T>> returnDeferred(long waitMillis, Long index, Supplier<T> fn) {
        return Mono.just(new ChangeItem<>(fn.get(), System.currentTimeMillis()));
    }
}