dubbo 快速启动

<!-- dubbo-parent pom.xml -->
	<dependencies>
		<!-- dubbo 会自动加载spring的依赖包 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.6.7</version>
		</dependency>
		<!-- 连接zookeeper的客户端 -->
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-framework</artifactId>
			<version>2.12.0</version>
		</dependency>
		<!-- netty -->
		<dependency>
			<groupId>io.netty</groupId>
			<artifactId>netty-all</artifactId>
			<version>4.1.32.Final</version>
		</dependency>
		<!-- 单元测试 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.3.16.RELEASE</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
//dubbo-interface 定义的接口

//这个接口使用xml配置发布服务
public interface HelloService {
	
	String sayHello(String name);
}

//这个接口类使用dobbo注解发布服务
public interface HiService {
	
	String sayHi(String name);
}
<!--dubbo-provider pom.xml 添加接口依赖 -->	
<dependencies>
		<dependency>
			<groupId>cn.fg</groupId>
			<artifactId>dubbo-interface</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
//dubbo-provider 接口实现

//该接口实现类使用xml发布服务
public class HelloServiceImpl implements HelloService {

	public String sayHello(String name) {
		// TODO Auto-generated method stub
		return "Hello " + name;
	}
}

//该接口使用dobbo注解发布服务
@Service //com.alibaba.dubbo.config.annotation.Service
public class HiServiceImpl implements HiService {

	public String sayHi(String name) {
		return "Hello " + name;
	}
}
<?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://dubbo.apache.org/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- dubbo-provider spring-provider.xml 提供者配置 -->

	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="dubbo-provider" />

	<!-- 使用zookeeper广播注册中心暴露服务地址 -->
	<dubbo:registry address="zookeeper://192.168.1.2:2181" />

	<!-- 
		可以把dubbo:application dubbo:registry 抽取到公共properties中 ,在类路径下创建dubbo.properties,如:
		dubbo.application.name=dubbo-provider
		dubbo.registry.address=zookeeper://192.168.1.2:2181
		具体参考官方文档配置覆盖策略http://dubbo.apache.org/zh-cn/docs/user/configuration/properties.html
	-->

	<!-- 用dubbo协议在20880端口暴露服务,其他协议参考官方文档 -->
	<dubbo:protocol name="dubbo" port="20880" />

	<!-- 接口实现类 -->
	<bean id="helloService" class="cn.fg.service.impl.HelloServiceImpl" />

	<!-- 声明需要暴露的服务接口 -->
	<!-- retries:重试次数(不包含第一调用),建议非幂等的操作不要添加该操作,如数据库的insert,由于网络连接慢,超时了就会重试,这时候会向数据库发送3次数据连接请求的操作 -->
	<!-- 
		version:定义该服务的版本号,消费者调用的时候可以只调用该服务的某个版本号,如果不指定将随机调用 
		我们对HelloService的做了升级,并部署到了其中一台服务器上,这时候我们可以指定version进行测试,其他服务器仍然是老的版本
	-->
	<dubbo:service interface="cn.fg.service.HelloService" ref="helloService" version="0.0.1" retries="3" />

	<!-- 服务提供者统一参数配置 timeout:消费者调用服务的超时时间(该参数也可以在dubbo:service配置),其他参数参考官方文档 -->
	<!-- 参数优先顺序:局部优先,全局次之,消费者优先,提供者次之,具体参考官方文档 -->
	<dubbo:provider timeout="10000"></dubbo:provider>

	<!-- 扫描@service注解发布的服务实现类 -->
	<dubbo:annotation package="cn.fg.service.impl" />
	
	<!-- 从zookeeper注册中心发现监控中心 -->
	<dubbo:monitor protocol="registry"></dubbo:monitor>
	
	<!-- 直连监控中心 -->
	<!-- <dubbo:monitor address="192.168.1.2:7070"></dubbo:monitor> -->
	
</beans>
//简单启动容器,发布服务
public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-provider.xml");
		context.start();
		System.in.read(); // 按任意键退出
	}
<!--dubbo-customer pom.xml 添加接口依赖 -->	
<dependencies>
		<dependency>
			<groupId>cn.fg</groupId>
			<artifactId>dubbo-interface</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
<?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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!--dubbo-customer spring-customer.xml 消费者配置 -->
 
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="dubbo-consumer"  />
 
    <!-- zookeeper注册中心地址,file:dubbo缓存,默认空。如果zookeeper宕机,可以从缓存中加载服务提供者的服务地址,注意多个进程不要同时访问缓存文件 -->
    <!-- java.io.tmpdir:java系统属性,值为操作系统临时文件夹 -->
    <dubbo:registry file="${java.io.tmpdir}/dubbo.cache" address="zookeeper://192.168.1.2:2181" />
 
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService,version:该服务存在多个版本时,可以指定调用某个版本 -->
	<!-- url:采用直连模式,不通过注册中心,如果服务有多个协议,一定要指定协议-->
    <dubbo:reference id="helloService" interface="cn.fg.service.HelloService" version="0.0.1" url="dubbo://127.0.0.1:20880">
    	<!-- 配置该接口下某个方法的调用参数,这里我们指定调用sayHello的超时时间为3秒 -->
    	<dubbo:method name="sayHello" timeout="3000"></dubbo:method> 
    </dubbo:reference>

    <!-- 消费者的统一参数配置,check:启动程序时不检查服务是否可用,timeout:调用服务的超时时间,其他参数参考官方文档,同时该配置参数又是dubbo:reference缺省配置值 -->
    <dubbo:consumer check="false" timeout="10000"></dubbo:consumer>

    <!-- 扫描@Reference注解所在的包名 -->
    <dubbo:annotation package="cn.fg.test" 
    
    <!-- 从zookeeper注册中心发现监控中心 -->
	<dubbo:monitor protocol="registry"></dubbo:monitor>
	
	<!-- 直连监控中心 -->
	<!-- <dubbo:monitor address="192.168.1.2:7070"></dubbo:monitor> -->
    
</beans>
package cn.fg.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.dubbo.config.annotation.Reference;

import cn.fg.service.HelloService;
import cn.fg.service.HiService;

//消费者调用服务单元测试
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration("classpath:spring-consumer.xml")
public class TestService {
	
	@Autowired  //使用xml配置好的 <dubbo:reference /> 注入远程服务
	HelloService helloService;
	
	@Reference //该注解配合xml配置<dubbo:annotation />一起使用, 申明该类为远程服务代理类
	HiService hiService;
	
	@Test
	public void testHelloService() {
		System.out.println(helloService.sayHello("hello"));
    }
	
	@Test
	public void testHiService() {
		System.out.println(hiService.sayHi("hi"));
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值