package com.soa.microservice.core; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * 服务实例信息类 * 用于存储和管理微服务实例的基本信息 */ public class ServiceInstance { private String instanceId; private String serviceName; private String host; private int port; private boolean healthy; private long lastHeartbeat; private Map metadata = new ConcurrentHashMap<>(); public ServiceInstance(String instanceId, String serviceName, String host, int port) { this.instanceId = instanceId; this.serviceName = serviceName; this.host = host; this.port = port; this.healthy = true; this.lastHeartbeat = System.currentTimeMillis(); } // Getters and setters public String getInstanceId() { return instanceId; } public String getServiceName() { return serviceName; } public String getHost() { return host; } public int getPort() { return port; } public boolean isHealthy() { return healthy; } public void setHealthy(boolean healthy) { this.healthy = healthy; } public long getLastHeartbeat() { return lastHeartbeat; } public void updateHeartbeat() { this.lastHeartbeat = System.currentTimeMillis(); } public Map getMetadata() { return metadata; } public void addMetadata(String key, String value) { this.metadata.put(key, value); } public String getMetadata(String key) { return this.metadata.get(key); } @Override public String toString() { return "ServiceInstance{" + "instanceId='" + instanceId + '\'' + ", serviceName='" + serviceName + '\'' + ", host='" + host + '\'' + ", port=" + port + ", healthy=" + healthy + "}"; } }