dubbo示例代码
准备条件
- zookeeper,maven(推荐阿里云仓库),java 运行环境
开始:
使用 maven新建接口项目 demoService,完成新建一个接口
public interface DemoInter {
String sayHello(String name);
List getUsers();
}
完成后打包接口为 jar 包,供 服务端和客户端使用
创建服务端,新建 server 项目,引入依赖和打包好的 jar 文件
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
新建类,实现DemoInter接口
public class SampleServiceImpl implements DemoInter {
@Override
public String sayHello(String name) {
return "hello == " + name;
}
@Override
public List getUsers() {
List<User> users = new ArrayList<>();
User user = new User();
user.setName("eric");
user.setPwd("234");
users.add(user);
return users;
}
}
新建spring 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<bean id="simpleService" class="com.dubbo.inter.impl.SampleServiceImpl"></bean>
<dubbo:application name="server"/>
<!-- 消息中心地址 -->
<dubbo:registry address="zookeeper://localhost:2181" check="false"/>
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 声明接口 -->
<dubbo:service retries="0" interface="com.dubbo.inter.DemoInter" ref="simpleService"/>
</beans>
新建可执行的服务端
public class App {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext
applicationContext =
new ClassPathXmlApplicationContext(new String[]{"classpath:spring.xml"});
applicationContext.start();
System.in.read();
}
}
此时,dubbo 服务端项目完成
下面创建客户端,同上,新建client 项目,引入 maven 依赖,和打包好的 jar 文件
新建客户端 spring 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="comsumer"/>
<!-- 消息中心地址 -->
<dubbo:registry address="zookeeper://localhost:2181" check="false"/>
<dubbo:reference interface="com.dubbo.inter.DemoInter" id="sampleService" check="false"/>
</beans>
创建客户端可执行文件
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext
= new ClassPathXmlApplicationContext(new String[]{"spring_consumer.xml"});
DemoInter inter = (DemoInter) classPathXmlApplicationContext.getBean("sampleService");
System.out.println(inter.sayHello("hees"));
}
}
运行客户端,可远程调用服务端代码了.