上次springboot集成dubbo写过一个简单的demo调用的方式,由于spring版本的问题,这次使用duboox,完全基于注解的方式。
dubbox 地址:https://github.com/dangdangdotcom/dubbox
由于dubbox没有发布到maven中央仓库,需要自己安装到本地maven库或者私库
git clone https://github.com/dangdangdotcom/dubbox
在dubbox目录执行mvn install -Dmaven.test.skip=true来尝试编译一下dubbo(并将dubbo的jar安装到本地maven库)
1.导入maven依赖
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.7</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
2.配置dubbo
dubbo.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-3.1.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"default-lazy-init="false">
<dubbo:protocol name="dubbo" host="${dubbo.protocol.dubbo.host}" port="${dubbo.protocol.dubbo.port}" charset="UTF-8" threadpool="fixed" threads="500" />
<dubbo:application name="${dubbo.application.name}"></dubbo:application>
<dubbo:registry address="${dubbo.registry.address}" check="true" username="${dubbo.admin.username}" password="${dubbo.admin.password}"></dubbo:registry>
<dubbo:provider timeout="6000" /> //表示为提供者
<dubbo:consumer timeout="6000" check="false"/>//表示为消费者 check是检查是否有对应的服务提供
<dubbo:annotation package="com.example" />
</beans>
.properties
#服务名
dubbo.application.name=dubbo-01
#注册的组
dubbo.registry.group=test2
#注册服务的域名
dubbo.protocol.dubbo.host=
#注册服务的端口
dubbo.protocol.dubbo.port=-1
#zookeeper地址
dubbo.registry.address=zookeeper://:2181
#用户名
dubbo.admin.username=dubbo
#密码
dubbo.admin.password=
3.创建接口
package com.example.dubbo;
/**
* Created by 王念 on 2016/4/21.
*/
public interface Iservice {
String test();
}
该接口提供者和消费者都需要依赖。 提供者实现该方法,消费者调用该方法
4.提供者实现
package com.example.dubbo;
import com.alibaba.dubbo.config.annotation.Service; //使用该注解注册dubbo服务
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
/**
* Created by 王念 on 2016/4/21.
*/
@Service
@EnableAutoConfiguration
public class ServiceImpl implements Iservice {
@Override
public String test() {
System.out.printf("RPC调用成功");
return "RPC调用成功";
}
}
4.消费者调用
package com.example;
import com.alibaba.dubbo.config.annotation.Reference;
import com.example.dubbo.Iservice;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = DemoApplication.class)
public class DemoApplicationTests {
@Reference//使用该注解注入服务
Iservice iservice;
@Test
public void contextLoads() {
System.out.println(iservice.test());//调用
}
}
5.main方法
@SpringBootApplication
@ImportResource("classpath:/dubbo-provider.xml")//启动加在dubbo配置文件
public class DemoApplication{
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
try {
System.out.println("启动成功,按任意键关闭");
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
} }
本文地址(防爬虫不标注来源):http://my.oschina.net/wangnian/blog/665299
来源:oschina
链接:https://my.oschina.net/u/2408834/blog/665299