dubbo SPI
作者:互联网
RMI
rmi clinet:
public class RmiClient { public static void main(String[] args) throws RemoteException, NotBoundException, MalformedURLException { String remoteAddr = "rmi://localhost:8080/UserService"; UserService userService = (UserService) Naming.lookup(remoteAddr); System.out.println(String.format("引用远程服务成功,当前主机:%s ", ManagementFactory.getRuntimeMXBean().getName())); String response = userService.getName(11); System.out.println("=======> " + response + " <======="); } }
/** * @author Tommy * Created by Tommy on 2019/12/1 **/ public class RmiRegister { public static void main(String[] args) throws IOException { // 本地主机上的远程对象注册表Registry的实例 LocateRegistry.createRegistry(8080); System.out.println("======= 注册中心启动成功! ======="); System.in.read(new byte[1024]); } }
/** * @author Tommy * Created by Tommy on 2019/12/1 **/ public class RmiServer { public static void main(String[] args) throws IOException, AlreadyBoundException { // 创建一个远程对象 UserService hello = new UserServiceImpl(); //绑定的URL标准格式为:rmi://host:port/name Naming.bind("rmi://localhost:8080/UserService3", hello); System.out.println("======= 启动RMI服务注册成功! ======="); System.in.read(new byte[1024]); } }
/** * @author Tommy * Created by Tommy on 2019/12/1 **/ public interface UserService extends Remote { String getName(Integer id) throws RemoteException; }
/** * @author Tommy * Created by Tommy on 2019/12/1 **/ public class UserServiceImpl extends UnicastRemoteObject implements UserService { protected UserServiceImpl() throws RemoteException { } @Override public String getName(Integer id) { return String.format("myName is luban :%s", ManagementFactory.getRuntimeMXBean().getName()); } }
spi 思想:
/** * @author Tommy * Created by Tommy on 2019/12/1 **/ public class Main { public static void main(String[] args) { Iterator<UserService> iterator = ServiceLoader.load(UserService.class).iterator(); UserService service = iterator.next(); System.out.println(service.getName(11)); // Class.forName("com.mysql.jdbc.Driver"); // DriverManager.getConnection(""); } }
public interface UserService { public String getName(int id); }
/** * @author Tommy * Created by Tommy on 2019/12/1 **/ public class UserServiceImpl implements UserService { @Override public String getName(int id) { return "luban good "; } }
thoughtCodes 发布了495 篇原创文章 · 获赞 10 · 访问量 9万+ 关注
标签:dubbo,String,getName,System,Tommy,SPI,UserService,public 来源: https://blog.csdn.net/xiamaocheng/article/details/104182529