解决技术方案--
找具体的service在那台机器 service下线了怎么办? 请求负载倾斜了怎么办? 服务治理
// rmi
import java.rmi.Remote;
public interface ISayService extends Remote{
// 记住要抛出异常,否则启动报错
public String say(String name) throws Exception;
}
public class SayServiceImpl implements ISayService {
@Override
public String say(String name) {
return name + " say : haha";
}
}
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class ServiceMain {
public static void main(String[] args) throws RemoteException {
// 实例化 要暴露给远程的方法/接口
ISayService impl = new SayServiceImpl();
// 开启本地服务
ISayService sayService = (ISayService) UnicastRemoteObject.exportObject(impl, 666);
// 服务注册中心
Registry registry = LocateRegistry.createRegistry(999);
// 注册服务
registry.rebind("sayService", sayService);
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class CustomerMain {
public static void main(String[] args) throws Exception {
// 注册中心
Registry registry = LocateRegistry.getRegistry(999);
// 发现服务
ISayService sayService = (ISayService) registry.lookup("sayService");
// 调起服务
String str = sayService.say("三哥");
System.out.println("str:" + str);
}
}