package com.x.hello.api;
public interface helloservice
{
string sayhello(string name);
}
接口的实现类:(helloserviceimpl.java)
package com.x.hello.impl;
import com.x.hello.api.helloservice;
public class helloserviceimpl implements helloservice {
public string sayhello(string name){
return "hello" + name;
}
}
spring配置:(provider.xml)
<?xmlversion="1.0"encoding="utf-8"?>
<beans>
<!--application name-->
<dubbo:applicationname="hello-world-app"/>
<!--registry address, used for service to register itself-->
<dubbo:registryaddress="multicast://224.5.6.7:1234"/>
<!--expose this service through dubbo protocol, through port 20880-->
<dubbo:protocolname="dubbo" port="20880"/>
<!--which service interface do we expose?-->
<dubbo:serviceinterface="com.x.hello.api.helloservice" ref="helloservice"/>
<!--designate implementation-->
<beanid="helloservice" class="com.x.hello.impl.helloserviceimpl"/>
</beans>
测试代码:(provider.java)
import org.springframework.context.support.classpathxmlapplicationcontext;
public class provider {
public static void main(string[] args){
// 启动成功,监听端口为20880
classpathxmlapplicationcontext context = new classpathxmlapplicationcontext(newstring[]{"provider.xml"});
// 按任意键退出
system.in.read();
}
}
客户端
spring配置文件:(consumer.xml)
<?xmlversion="1.0"encoding="utf-8"?>
<beans>
<!--consumer application name-->
<dubbo:applicationname="consumer-of-helloworld-app"/>
<!--registry address, used for consumer to discover services-->
<dubbo:registryaddress="multicast://224.5.6.7:1234"/>
<!--whichservicetoconsume?-->
<dubbo:referenceid="helloservice" interface="com.x.hello.api.helloservice"/>
</beans>
客户端测试代码:(consumer.java)
import org.springframework.context.support.classpathxmlapplicationcontext;
import com.alibaba.hello.api.helloservice;
public class consumer {
public static void main(string[] args) {
classpathxmlapplicationcontext context = new classpathxmlapplicationcontext(newstring[]{"consumer.xml"});
// get service invocation proxy
helloservice helloservice =(helloservice)context.getbean("helloservice");
// do invoke!
string hello = helloservice.sayhello("world");
system.out.println(hello);
}
}