You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
789/BasicExample/ServiceRegistry.java

129 lines
4.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 服务注册中心
* 支持服务的注册和发现功能
*/
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ServiceRegistry {
// 使用ConcurrentHashMap存储服务信息确保线程安全
private final Map<String, List<ServiceInstance>> services;
// 单例模式实现
private static volatile ServiceRegistry instance;
private ServiceRegistry() {
services = new ConcurrentHashMap<>();
System.out.println("服务注册中心初始化完成");
}
// 获取单例实例
public static ServiceRegistry getInstance() {
if (instance == null) {
synchronized (ServiceRegistry.class) {
if (instance == null) {
instance = new ServiceRegistry();
}
}
}
return instance;
}
// 注册服务
public synchronized void register(String serviceName, String host, int port) {
ServiceInstance instance = new ServiceInstance(host, port);
services.computeIfAbsent(serviceName, k -> new ArrayList<>()).add(instance);
System.out.println("服务注册成功: " + serviceName + " at " + host + ":" + port);
}
// 注销服务
public synchronized void deregister(String serviceName, String host, int port) {
List<ServiceInstance> instances = services.get(serviceName);
if (instances != null) {
boolean removed = instances.removeIf(instance ->
instance.getHost().equals(host) && instance.getPort() == port);
if (removed) {
System.out.println("服务注销成功: " + serviceName + " at " + host + ":" + port);
// 如果该服务没有实例了,移除服务
if (instances.isEmpty()) {
services.remove(serviceName);
}
} else {
System.out.println("服务不存在: " + serviceName + " at " + host + ":" + port);
}
} else {
System.out.println("服务不存在: " + serviceName);
}
}
// 发现服务(获取所有实例)
public List<ServiceInstance> discover(String serviceName) {
List<ServiceInstance> instances = services.get(serviceName);
if (instances != null && !instances.isEmpty()) {
System.out.println("发现服务: " + serviceName + ",实例数量: " + instances.size());
return new ArrayList<>(instances); // 返回副本避免并发修改问题
}
System.out.println("未发现服务: " + serviceName);
return Collections.emptyList();
}
// 获取随机实例(简单的负载均衡)
public ServiceInstance getRandomInstance(String serviceName) {
List<ServiceInstance> instances = discover(serviceName);
if (!instances.isEmpty()) {
Random random = new Random();
ServiceInstance instance = instances.get(random.nextInt(instances.size()));
System.out.println("选择随机实例: " + serviceName + " at " + instance.getHost() + ":" + instance.getPort());
return instance;
}
return null;
}
// 获取所有已注册的服务名称
public Set<String> getAllServices() {
return new HashSet<>(services.keySet());
}
// 服务实例内部类
public static class ServiceInstance {
private final String host;
private final int port;
private final long registerTime;
public ServiceInstance(String host, int port) {
this.host = host;
this.port = port;
this.registerTime = System.currentTimeMillis();
}
public String getHost() {
return host;
}
public int getPort() {
return port;
}
public long getRegisterTime() {
return registerTime;
}
@Override
public String toString() {
return host + ":" + port;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ServiceInstance that = (ServiceInstance) o;
return port == that.port && Objects.equals(host, that.host);
}
@Override
public int hashCode() {
return Objects.hash(host, port);
}
}
}