发表于: 2021-04-16 23:01:53
3 1264
拆分任务7service
拆分任务7service
这些一定要理清楚,才动手收开始做
收获:
将service从任务7的项目中拆分出来。
<!-- 服务端 -->
<bean id="UserService" class="com.kbk.service.Impl.UserServiceImpl"></bean>
<!-- 使用RmiServiceExporter将RMIServiceImpl的对象导出为RMI服务对象 -->
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<!--服务的接口类型-->
<property name="serviceInterface" value="com.kbk.service.Impl.UserServiceImpl"/>
<!--对外的名称,即客户端访问时用这个名字找到这个服务-->
<property name="serviceName" value="ServiceA"/>
<!--服务占用的端口-->
<property name="registryPort" value="1234"/>
<property name="servicePort" value="1234"/>
<!--要发布成服务的类-->
<property name="service" ref="UserService"/>
</bean>
<!--RMI客户端配置-->
<!-- 客户端调用 -->
<bean name="UserServiceA" class="org.springframework.remoting.rmi.RmiProxyFactoryBean" lazy-init="true">
<!-- 根据服务端的serviceName(服务名)和registryPort(端口)组成的访问地址 -->
<property name="serviceUrl" value="rmi://127.0.0.1:1234/ServiceA"></property>
<property name="serviceInterface" value="com.kbk.service.UserService"></property>
<!-- 预查找远程对象 默认为true -->
<!-- <property name="lookupStubOnStartup" value="false"/>-->
<!-- 是否刷新远程调用缓存的stub -->
<property name="refreshStubOnConnectFailure" value="true"></property>
</bean>
服务端注册:
public class ServerDo {
public static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
public static void main(String[] args) {
try {
//System.setProperty("java.rmi.server.hostname", "xxxxxx");
System.setProperty("java.rmi.server.hostname","127.0.0.1");
// System.setProperty("java.rmi.server.hostname","172.21.0.2");
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/spring/spring-mybatis.xml");
applicationContext.getBean("UserServiceImpl");
logger.info("\n" + "spingservice服务已经启动,请准备!");
} catch (Exception e) {
logger.error("报错信息" + e.getMessage());
e.printStackTrace();
}
}
}
运行报错:
org.springframework.remoting.RemoteLookupFailureException: Lookup of RMI stub failed; nested exception is java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
java.net.ConnectException: Connection refused: connect
试了很多,还是无法解决
评论