发表于: 2019-10-16 23:44:43
1 801
今日完成
1.找了个Demo跑跑RMI
有两种方法,我这里就选了.xml配置文件配置
1.1Server
1.server.xml
<bean id="hello11" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="hello11"/>
<property name="service" ref="helloServiceImpl"/>
<property name="serviceInterface" value="com.jnshu.service.IHelloService"/>
<property name="registryPort" value="1120"/>
</bean>
<bean id="hello2" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="hello2"/>
<property name="service" ref="thelloServiceImpl"/>
<property name="serviceInterface" value="com.jnshu.service.THelloService"/>
<property name="registryPort" value="1199"/>
</bean>
<bean id="helloServiceImpl" class="com.jnshu.service.impl.HelloServiceImpl"/>
<bean id="thelloServiceImpl" class="com.jnshu.service.impl.THelloServiceImpl"/>
2.Service/Impl
public interface THelloService {
public String sayHello2(String msg);
}
public interface IHelloService {
public String sayHello(String msg);
}
@Service
public class THelloServiceImpl implements THelloService {
@Override
public String sayHello2(String msg) {
System.out.println("服务端2222接受消息:"+msg);
return "hello2222-->>"+msg;
}
}
@Service
public class HelloServiceImpl implements IHelloService {
@Override
public String sayHello(String msg) {
System.out.println("服务端1111接受消息:"+msg);
return "hello11111-->>"+msg;
}
}
3.test
public class Publish
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("server.xml");
}
}
1.2Client
1.client.xml
<bean class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost:1120/hello11"/>
<property name="serviceInterface" value="com.jnshu.service.IHelloService"/>
</bean>
<bean class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost:1199/hello2"/>
<property name="serviceInterface" value="com.jnshu.service.THelloService"/>
<property name="lookupStubOnStartup" value="false"/>
<property name="refreshStubOnConnectFailure" value="true"/>
</bean>
2.Service/Impl
public interface IHelloService {
public String sayHello(String msg);
}
public interface THelloService {
public String sayHello2(String msg);
}
@Service
public class UserServiceImpl {
@Resource
private IHelloService helloService;
@Resource
private THelloService tHelloService;
public void test(){
System.out.println("客户端11111发送消息:"+helloService.sayHello("hello111"));
}
public void test1(){
System.out.println("客户端22222发送消息:"+tHelloService.sayHello2("hello222"));
}
}
3.test
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("client.xml");
UserServiceImpl userServiceImpl = (UserServiceImpl) ac.getBean("userServiceImpl");
userServiceImpl.test();
userServiceImpl.test1();
}
}
碰到问题
配置问题,改到这样能跑,rmi感觉不会使用啊
明日计划
把任务七的项目拆开来试试
启发
评论