美文网首页
服务框架—基于JAVA实现远程过程调用RMI实验

服务框架—基于JAVA实现远程过程调用RMI实验

作者: AISIC | 来源:发表于2017-10-17 08:49 被阅读0次

     最近做了个RMI远程调用实验,现记录一下,实验要求如下:

     (1) 两人一组,每人一台PC,一台客户端,一台服务端;

     (2) 服务端要求:要求实现如下远程方法,实现对登录提交的用户名密码进行远程校验。

     publicBooleanCheckLogin(String username, Stringpassword)

     (3) 服务端代码要求连接Mysql数据库进行数据校验;

     (4) 客户端要求:用RMI技术进行远程过程调用实现登录过程。根据远程返回值分别进入“登录成功”或“登录失败”提示信息或页面。

       原理就不多说了,直接上源码,源码如下。(不在同一网段下的两台电脑就需要花点时间去ping通了)

1、服务端源码 RMIServer

public class RMIServer {

public static void main(String[] args){

try {

System.setProperty("java.rmi.server.hostname", "192.168.1.105");

//注册服务

LocateRegistry.createRegistry(1099);

LoginService loginService = new LoginServiceImpl();

Naming.bind("rmi://192.168.1.105:1099/rmiServer", loginService);

System.out.println(">>>>>> INFO:远程RMIServer绑定【192.168.1.105】成功!");

} catch (RemoteException e) {

System.out.println("创建远程对象发生异常!");

e.printStackTrace();

} catch (MalformedURLException e) {

System.out.println("重复绑定发生异常!");

e.printStackTrace();

} catch (AlreadyBoundException e) {

System.out.println("URL异常!");

e.printStackTrace();

}

}

}

2、客户端源码 RMIClient

public class RMIClient {

public static void main(String[] args) {

try {

LoginService login = (LoginService)Naming.lookup("rmi://192.168.1.105:1099/rmiServer");

boolean isLogin = login.checkLogin("admin", "123");

if(isLogin){

System.out.println("登陆成功!");

}else{

System.out.println("登陆失败!");

}

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (RemoteException e) {

e.printStackTrace();

} catch (NotBoundException e) {

e.printStackTrace();

}

}

}

3、远程接口LoginService

public interface LoginService extends Remote{

//声明服务器端必须提供的服务

public boolean checkLogin(String username,String password) throws RemoteException;

}

4、接口实现类LoginServiceImpl

public class LoginServiceImpl extends UnicastRemoteObject implements LoginService{

public LoginServiceImpl() throws RemoteException {

super();

}

public boolean checkLogin(String username, String password) throws RemoteException {

DBUtil db = new DBUtil("select * from user where username = ? and password = ? ");

ResultSet rs = null;

System.out.println(">>>>>>开始查询数据库<<<<<<");

try {

db.pst.setString(1, username);

db.pst.setString(2, password);

rs = db.pst.executeQuery();

if(rs.next()){

System.out.println("用户名"+rs.getString("username")+"密码"+rs.getString("password"));

return true;

}

System.out.println(">>>>>>用户不存在<<<<<<");

} catch (SQLException e) {

e.printStackTrace();

}finally {

try {

rs.close();

db.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

return false;

}

}

5、DBUtil

public class DBUtil {

private static final String url = "jdbc:mysql://localhost:3306/testrmi";

private static final String user = "root";

private static final String password = "123456";

private static final String name="com.mysql.jdbc.Driver";

public Connection conn = null;

public PreparedStatement pst = null;

public DBUtil(String sql){

try {

Class.forName(name);

conn = DriverManager.getConnection(url, user, password);//获取连接

pst = conn.prepareStatement(sql);//准备执行语句

} catch (Exception e) {

e.printStackTrace();

}

}

public void close(){

try {

this.conn.close();

this.pst.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

6、实体类User

public class User {

private String username;

private String password;

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

}


CSDN地址: http://blog.csdn.net/xiaofeisong

相关文章

  • 服务框架—基于JAVA实现远程过程调用RMI实验

    最近做了个RMI远程调用实验,现记录一下,实验要求如下: (1) 两人一组,每人一台PC,一台客户端,一台...

  • 中间件技术——实验一:远程过程调用

    一、实验内容 实验目的掌握远程过程调用原理,基于java RMI进行远程编程和控制。要求定义远程接口类:定义相应的...

  • Java自带RPC实现,RMI框架入门

    Java自带RPC实现,RMI框架入门 首先RMI(Remote Method Invocation)是Java特...

  • 在Spring中配置RMI

    使用RMI服务 创建RMI服务的基本步骤 编写一个服务实现类,类中的方法必须抛出java.rmi.RemoteEx...

  • RMI注意事项

    RMI服务接口 提供服务的RMI服务接口必须实现Remote接口 RMI服务启动 RMI服务端口 RMI需要两个端...

  • dubbo和springcloud的区别

    两者都是微服务框架 1.dubbo是基于RPC(远程过程调用)的框架 springcloud是基于http的res...

  • Dubbo

    Dubbo(服务治理框架) RPC 各服务都要实现rpc协议,才能实现服务间的调用 rpc:远程过程调用协议,是一...

  • Axon框架-构建可扩展性微服务

    Axon框架是一个Java微服务框架,他主要作用是帮助你基于DDD来实现微服务架构。 除了DDD,Axon框架还可...

  • Dubbo系列1:Java RMI初见

    在正式开始Dubbo系列之前,先来介绍一下Java RMI服务; 一、Java RMI的原理 Java RMI是T...

  • Java RMI 实现

    一、RMI介t绍 基于Java 的RMI通信协议 基于Java的序列化机制 基于阻塞式Tcp通讯虽然无处不是透露着...

网友评论

      本文标题:服务框架—基于JAVA实现远程过程调用RMI实验

      本文链接:https://www.haomeiwen.com/subject/cbvquxtx.html