Dubbo学习之旅二-Dubbo在spring中的使用
下面我将重点介绍一下dubbo在实际项目当中的应用,以demo的形式来说明:
定义服务接口,单独建一个maven项目名为dubbo-demo-service:
package com.demo;
public interface DemoService {
String sayHello(String name);
}
pom.xml中加入以下内容:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-demo</artifactId>
<version>2.5.4-SNAPSHOT</version>
</parent>
<artifactId>dubbo-demo-api</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>The demo module of dubbo project</description>
<properties>
<skip_maven_deploy>true</skip_maven_deploy>
</properties>
</project>
在服务方实现接口,新建一个maven的项目dubbo-demo-provider
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
}
配置dubbo-provider.xml:
<?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-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />
</beans>
启动provider:
public class DemoProvider {
public static void main(String[] args) {
com.alibaba.dubbo.container.Main.main(args);
}
}
服务消费者:
public class DemoAction {
private DemoService demoService;
public void setDemoService(DemoService demoService) {
this.demoService = demoService;
}
public void start() throws Exception {
for (int i = 0; i < Integer.MAX_VALUE; i ++) {
try {
String hello = demoService.sayHello("world" + i);
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + hello);
} catch (Exception e) {
e.printStackTrace();
}
Thread.sleep(2000);
}
}
}
定义dubbo-consumer.xml:
<?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-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" />
</beans>
启动consumer服务:
public class DemoConsumer {
public static void main(String[] args) {
com.alibaba.dubbo.container.Main.main(args);
}
}
运行之后的控制台,可以看到下面的界面:
configurators,routers&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=7012&side=consumer×tamp=1461833464831, urls: [dubbo://10.249.74.95:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=1992&side=provider×tamp=1461833444581], dubbo version: 2.0.0, current host: 10.249.74.95
[28/04/16 04:51:05:005 CST] main INFO config.AbstractConfig: [DUBBO] Refer dubbo service com.alibaba.dubbo.demo.DemoService from url multicast://224.5.6.7:1234/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=demo-consumer&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=7012&side=consumer×tamp=1461833464831, dubbo version: 2.0.0, current host: 10.249.74.95
[16:51:06] Hello world0, response form provider: 10.249.74.95:20880
[16:51:08] Hello world1, response form provider: 10.249.74.95:20880
[16:51:10] Hello world2, response form provider: 10.249.74.95:20880
[16:51:12] Hello world3, response form provider: 10.249.74.95:20880
[16:51:14] Hello world4, response form provider: 10.249.74.95:20880
[16:51:16] Hello world5, response form provider: 10.249.74.95:20880
[16:51:18] Hello world6, response form provider: 10.249.74.95:20880
[16:51:20] Hello world7, response form provider: 10.249.74.95:20880
[16:51:22] Hello world8, response form provider: 10.249.74.95:20880
[16:51:24] Hello world9, response form provider: 10.249.74.95:20880
[16:51:26] Hello world10, response form provider: 10.249.74.95:20880
[16:51:28] Hello world11, response form provider: 10.249.74.95:20880
[16:51:30] Hello world12, response form provider: 10.249.74.95:20880
[16:51:32] Hello world13, response form provider: 10.249.74.95:20880
[16:51:34] Hello world14, response form provider: 10.249.74.95:20880
[16:51:36] Hello world15, response form provider: 10.249.74.95:20880
[16:51:38] Hello world16, response form provider: 10.249.74.95:20880
[16:51:40] Hello world17, response form provider: 10.249.74.95:20880
[16:51:42] Hello world18, response form provider: 10.249.74.95:20880
[16:51:44] Hello world19, response form provider: 10.249.74.95:20880
[16:51:46] Hello world20, response form provider: 10.249.74.95:20880