All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zyy.common.util.ApplicationUtil Maven / Gradle / Ivy

package com.zyy.common.util;

import lombok.AllArgsConstructor;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@AllArgsConstructor
public class ApplicationUtil {
    private final DiscoveryClient discoveryClient;
    private static DiscoveryClient discovery;

    @PostConstruct
    public void init() {
        discovery = discoveryClient;
    }

    public static Map> serviceUrl() {
        Map> msl = new HashMap<>();
        List services = discovery.getServices();
        for (String service : services) {
            List sis = discovery.getInstances(service);
            msl.put(service, sis);
        }
        return msl;
    }

    /**
     * 判断服务是否存在
     *
     * @param applicationName 服务名
     * @return true/false
     */
    public static boolean isExitService(String applicationName) {
        List services = discovery.getServices();
        for (String service : services) {
            if (service.toUpperCase().equals(applicationName.toUpperCase())) {
                return true;
            }
        }
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy