package cn.reghao.tnb.eureka.server.rpc;

import cn.reghao.tnb.eureka.api.dto.EurekaInstance;
import cn.reghao.tnb.eureka.api.iface.EurekaService;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.shared.Application;
import com.netflix.eureka.EurekaServerContext;
import com.netflix.eureka.EurekaServerContextHolder;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author reghao
 * @date 2025-04-24 15:10:40
 */
@DubboService
@Service
public class EurekaServiceImpl implements EurekaService {
    @Override
    public List<EurekaInstance> getRegistryServices() {
        EurekaServerContext context = EurekaServerContextHolder.getInstance().getServerContext();
        List<Application> applications = context.getRegistry().getSortedApplications();

        List<EurekaInstance> list = new ArrayList<>();
        for (Application application : applications) {
            String name = application.getName();
            List<InstanceInfo> instanceInfos = application.getInstances();
            EurekaInstance eurekaInstance = new EurekaInstance();
            eurekaInstance.setName(name);
            list.add(eurekaInstance);
        }

        return list;
    }
}
