分散式架構基礎之Java RMI詳解
RMI簡介
Java RMI或遠端方法呼叫是用於遠端過程呼叫的Java API,它可以直接傳輸序列化Java物件和分散式垃圾收集。它的實現依賴於Java虛擬機器(JVM),因此它只支援從一個JVM到另一個JVM的呼叫。
rmi的實現
(1) 直接使用Registry實現rmi
服務端:
介面:
繼承Remote介面
public interface HelloRegistryFacade extends Remote { String helloWorld(String name) throws RemoteException; }
介面實現:
繼承UnicastRemoteObject
public class HelloRegistryFacadeImpl extends UnicastRemoteObject implements HelloRegistryFacade{ public HelloRegistryFacadeImpl() throws RemoteException { super(); } @Override public String helloWorld(String name) { return "[Registry] 你好! " + name; } }
客戶端:
public class RegistryClient { public static void main(String[] args) { try { Registry registry = LocateRegistry.getRegistry(1099); HelloRegistryFacade hello = (HelloRegistryFacade) registry.lookup("HelloRegistry"); String response = hello.helloWorld("ZhenJin"); System.out.println("=======> " + response + " <======="); } catch (NotBoundException | RemoteException e) { e.printStackTrace(); } } }
圖解:
出處:https://www.tutorialspoint.com/java_rmi/java_rmi_introduction.htm
Registry(登錄檔)是放置所有伺服器物件的名稱空間。
每次服務端建立一個物件時,它都會使用bind()或rebind()方法註冊該物件。
這些是使用稱為繫結名稱的唯一名稱註冊的。
要呼叫遠端物件,客戶端需要該物件的引用,如(HelloRegistryFacade)。
即通過服務端繫結的名稱(HelloRegistry)從登錄檔中獲取物件(lookup()方法)。
(2) 使用Naming方法實現rmi
服務端:
public class NamingService {
public static void main(String[] args) {
try {
// 本地主機上的遠端物件登錄檔Registry的例項
LocateRegistry.createRegistry(1100);
// 建立一個遠端物件
HelloNamingFacade hello = new HelloNamingFacadeImpl();
// 把遠端物件註冊到RMI註冊伺服器上,並命名為Hello
//繫結的URL標準格式為:rmi://host:port/name
Naming.bind("rmi://localhost:1100/HelloNaming", hello);
System.out.println("======= 啟動RMI服務成功! =======");
} catch (RemoteException | MalformedURLException | AlreadyBoundException e) {
e.printStackTrace();
}
}
}
介面和介面實現和Registry的方式一樣
客戶端:
public class NamingClient {
public static void main(String[] args) {
try {
String remoteAddr="rmi://localhost:1100/HelloNaming";
HelloNamingFacade hello = (HelloNamingFacade) Naming.lookup(remoteAddr);
String response = hello.helloWorld("ZhenJin");
System.out.println("=======> " + response + " <=======");
} catch (NotBoundException | RemoteException | MalformedURLException e) {
e.printStackTrace();
}
}
}
Naming部分原始碼:
public static Remote lookup(String name)
throws NotBoundException,java.net.MalformedURLException,RemoteException{
ParsedNamingURL parsed = parseURL(name);
Registry registry = getRegistry(parsed);
if (parsed.name == null)
return registry;
return registry.lookup(parsed.name);
}
Naming其實是對Registry的一個封裝
Scala實現rmi
上面說了rmi是通過JVM虛擬機器進行一個遠端呼叫的,我們通過Scala,kotlin等jvm語言印證下
服務端:
object ScalaRmiService extends App {
try {
val user:UserScalaFacade = new UserScalaFacadeImpl
LocateRegistry.createRegistry(1103)
Naming.rebind("rmi://localhost:1103/UserScala", user)
println("======= 啟動RMI服務成功! =======")
} catch {
case e: IOException => println(e)
}
}
介面
trait UserScalaFacade extends Remote {
/**
* 通過使用者名稱獲取使用者資訊
*/
@throws(classOf[RemoteException])
def getByName(userName: String): User
/**
* 通過使用者性別獲取使用者資訊
*/
@throws(classOf[RemoteException])
def getBySex(userSex: String): List[User]
}
介面實現:
class UserScalaFacadeImpl extends UnicastRemoteObject with UserScalaFacade {
/**
* 模擬一個數據庫表
*/
private lazy val userList = List(
new User("Jane", "女", 16),
new User("jack", "男", 17),
new User("ZhenJin", "男", 18)
)
override def getByName(userName: String): User = userList.filter(u => userName.equals(u.userName)).head
override def getBySex(userSex: String): List[User] = userList.filter(u => userSex.equals(u.userSex))
}
實體類:
實體類必須實現序列化(Serializable)才能進行一個遠端傳輸
class User(name: String, sex: String, age: Int) extends Serializable {
var userName: String = name
var userSex: String = sex
var userAge: Int = age
override def toString = s"User(userName=$userName, userSex=$userSex, userAge=$userAge)"
}
Scala客戶端:
object ScalaRmiClient extends App {
try {
val remoteAddr="rmi://localhost:1103/UserScala"
val userFacade = Naming.lookup(remoteAddr).asInstanceOf[UserScalaFacade]
println(userFacade.getByName("ZhenJin"))
System.out.println("--------------------------------------")
for (user <- userFacade.getBySex("男")) println(user)
} catch {
case e: NotBoundException => println(e)
case e: RemoteException => println(e)
case e: MalformedURLException => println(e)
}
}
Java客戶端:
public class JavaRmiClient {
public static void main(String[] args) {
try {
String remoteAddr="rmi://localhost:1103/UserScala";
UserScalaFacade userFacade = (UserScalaFacade) Naming.lookup();
User zhenJin = userFacade.getByName("ZhenJin");
System.out.println(zhenJin);
System.out.println("--------------------------------------");
List<User> userList = userFacade.getBySex("男");
System.out.println(userList);
} catch (NotBoundException | RemoteException | MalformedURLException e) {
e.printStackTrace();
}
}
}
上面試驗可以證明Scala和Java是可以互通的,Scala本身也是可以直接引用Java類的
序列化簡介
序列化(Serialization)是將資料結構或物件狀態轉換為可以儲存(例如,在檔案或儲存器緩衝區中)或傳輸(例如,通過網路連線)的格式的過程, 反序列化(Deserialization)則是從一系列位元組中提取資料結構的相反操作.
Kotlin實現rmi
服務端:
fun main(args: Array<String>) {
try {
val hello: HelloKotlinFacade = HelloKotlinFacadeImpl()
LocateRegistry.createRegistry(1102)
Naming.rebind("rmi://localhost:1101/HelloKotlin", hello)
println("======= 啟動RMI服務成功! =======")
} catch (e: IOException) {
e.printStackTrace()
}
}
客戶端:
fun main(args: Array<String>) {
try {
val hello = Naming.lookup("rmi://localhost:1102/HelloKotlin") as HelloKotlinFacade
val response = hello.helloWorld("ZhenJin")
println("=======> $response <=======")
} catch (e: NotBoundException) {
e.printStackTrace()
} catch (e: RemoteException) {
e.printStackTrace()
} catch (e: MalformedURLException) {
e.printStackTrace()
}
}
實現和介面省略...
SpringBoot實現rmi
StringBoot通過配置就可以簡單實現rmi了
服務端:
@Configuration
public class RmiServiceConfig {
@Bean
public RmiServiceExporter registerService(UserFacade userFacade) {
RmiServiceExporter rmiServiceExporter = new RmiServiceExporter();
rmiServiceExporter.setServiceName("UserInfo");
rmiServiceExporter.setService(userFacade);
rmiServiceExporter.setServiceInterface(UserFacade.class);
rmiServiceExporter.setRegistryPort(1101);
return rmiServiceExporter;
}
}
客戶端:
@Configuration
public class RmiClientConfig {
@Bean
public UserFacade userInfo() {
RmiProxyFactoryBean rmiProxyFactoryBean = new RmiProxyFactoryBean();
rmiProxyFactoryBean.setServiceUrl("rmi://localhost:1101/UserInfo");
rmiProxyFactoryBean.setServiceInterface(UserFacade.class);
rmiProxyFactoryBean.afterPropertiesSet();
return (UserFacade) rmiProxyFactoryBean.getObject();
}
}
客戶端測試類:
@Autowired
private UserFacade userFacade;
@Test
public void userBySexTest() {
try {
List<User> userList = userFacade.getBySex("男");
userList.forEach(System.out::println);
} catch (RemoteException e) {
e.printStackTrace();
}
}
通過測試類可以看出,這和我們平時的程式呼叫內部方法沒什麼區別!
rmi呼叫過程
大家可以通過下面文章加深瞭解:
-
有兩個遠端服務介面可供client呼叫,Factory和Product介面
-
FactoryImpl類實現了Factory介面,ProductImpl類實現了Product介面
1. FactoryImpl被註冊到了rmi-registry中 2. client端請求一個Factory的引用 3. rmi-registry返回client端一個FactoryImpl的引用 4. client端呼叫FactoryImpl的遠端方法請求一個ProductImpl的遠端引用 5. FactoryImpl返回給client端一個ProductImpl引用 6. client通過ProductImpl引用呼叫遠端方法
socket工廠文件: docs.oracle.com/javase/8/do…