普通接口与实现类
public interface DemoService { String sayHello(String msg);}
public class DemoServiceImpl implements DemoService { @Override public String sayHello(String msg) { return "hello " + msg; }}
dubbo服务提供者配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="hello-world-app"/> <!-- 使用multicast广播注册中心暴露服务地址 --> <!--<dubbo:registry address="multicast://224.5.6.7:1234"/>--> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="rmi" port="9123" codec="spring"/> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.dubbo.rmi.DemoService" ref="demoService" registry="N/A" path="hello"/> <!-- 和本地bean一样实现服务 --> <bean id="demoService" class="com.dubbo.rmi.DemoServiceImpl"/> </beans>
dubbo服务消费者配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="hello-world-app-consumer"/> <!-- 使用multicast广播注册中心暴露服务地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234"/> --> <!-- 声明需要暴露的服务接口 --> <dubbo:reference interface="com.dubbo.rmi.DemoService" id="demoService" url="rmi://localhost:9123/hello" registry="N/A"/> </beans>
springRMI服务配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!--RMI的服务实现类--> <bean id="demoService" class="com.dubbo.rmi.DemoServiceImpl" /> <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> <!--配置RMI的服务实现类--> <property name="service" ref="demoService" /> <!--配置RMI的服务接口--> <property name="serviceInterface" value="com.dubbo.rmi.DemoService"/> <!--暴露的对外服务名--> <property name="serviceName" value="hello" /> <!--服务本地注册端口--> <property name="registryPort" value="9123" /> <!--服务对外暴露端口--> <property name="servicePort" value="9123" /> <!--注册服务--> <property name="alwaysCreateRegistry" value="true" /> </bean> </beans>
springRMI客户端配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="demoService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl" value="rmi://localhost:9123/hello" /> <property name="serviceInterface" value="com.dubbo.rmi.DemoService"/> </bean> </beans>
dubboRMI与springRMI相互调用注意点
- 端口须匹配
- <dubbo:service path="hello"> 与 springRMI配置<property name="serviceName" value="hello" /> 要一致, dubbo默认为接口全路径
时间: 2024-10-07 13:32:04