dubbo-server
- 创建父工程dubbo-server
- 以同样的方法创建两个子模块, server-api和server-provider. 其中server-api存放dubbo服务暴露的接口, server-provider为服务的实现模块.
- server-api模块实现
public interface IGpHello {
String sayHello(String msg);
}
- server-provider模块实现
// 引入依赖
<dependency>
<groupId>org.example</groupId>
<artifactId>server-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
// 服务的实现类
public class IGpHelloImpl implements IGpHello {
@Override
public String sayHello(String msg) {
return "hello, " + msg;
}
}
// 服务端的配置文件
<?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="dubbo-server" owner="jzx"/>
<!-- <dubbo:monitor protocol="registry"/>-->
<!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
<!-- <dubbo:registry address="zookeeper://localhost:2181" />-->
<dubbo:registry address="N/A"/>
<!--当前服务发布所依赖的协议;webserovice、Thrift、Hessain、http-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--服务发布的配置,需要暴露的服务接口-->
<dubbo:service
interface="org.example.IGpHello"
ref="iGpHello"/>
<!--Bean bean定义-->
<bean id="iGpHello" class="org.example.IGpHelloImpl"/>
</beans>
// 启动类
public class Bootstrap {
public static void main(String[] args) throws IOException {
//加载xml配置文件启动
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
context.start();
System.in.read(); // 按任意键退出
}
}
dubbo-client
- 代码实现
// 引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>server-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
// 客户端配置
<?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="dubbo-client" owner="jzx"/>
<!-- <dubbo:monitor protocol="registry"/>-->
<!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
<!-- <dubbo:registry address="zookeeper://localhost:2181" />-->
<dubbo:registry address="N/A"/>
<!--当前服务发布所依赖的协议;webserovice、Thrift、Hessain、http-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--服务发布的配置,需要暴露的服务接口, 有注册中心无需配置url属性, 若无则必须配置-->
<dubbo:reference id="gpHelloService"
interface="org.example.IGpHello"
url="dubbo://127.0.0.1:20880/org.example.IGpHello"/>
</beans>
// 启动类
import java.io.IOException;
/**
* Hello world!
*/
public class App {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
context.start();
IGpHello iGpHello = (IGpHello) context.getBean("gpHelloService");
String str = iGpHello.sayHello("lm");
System.out.println(str);
System.in.read();
}
}
测试