dubbo 快速入门

dubbo 阿里开源的一个SOA服务治理框架,从目前来看把它称作是一个RCP远程调用框架更为贴切。单从RPC框架来说,功能较完善,支持多种传输和序列化方案,其核心功能如下:

1.远程通讯:提供对多种基于长连接的NIO框架的封装,包括多种线程模型、序列化,以及"请求-响应"模式的信息交换方式。

2.集群容错:提供基于接口方法的透明远程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。

3.自动发现:基于注册中心目录服务,使服务消费当能动态的查找服务 提供方,使地址透明,使服务提供方可以平滑增加或减少机器。

具体参考用户指南http://dubbo.apache.org/books/dubbo-user-book/

项目所需jar包:

<dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.8</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.3.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.22</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty</artifactId>
            <version>6.1.26</version>
        </dependency>
        <dependency>
            <groupId>com.caucho</groupId>
            <artifactId>hessian</artifactId>
            <version>4.0.51</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
    </dependencies>

服务提供方:

SampleService接口:

public interface SampleService {
	String sayHello(String name);
}

SampleServiceImpl实现类:

public class SampleServiceImpl implements SampleService {

	public String sayHello(String name) {
		return "Hello " + name;
	}

}

配置文件:spring-dubbo-provider.xml

<?xml version="1.0" encoding="GBK" ?>
<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="sample-provider"/>
   
    <dubbo:registry protocol="zookeeper" address="192.168.50.110:2181" check="true"/>

     <!-- 用dubbo协议在20880端口暴露协议 -->
 	<dubbo:protocol name="dubbo" port="20880"/>
    
    <!-- 声明要暴露的接口 -->
    <dubbo:service interface="com.ty.sample.service.SampleService" ref="sampleServiceImpl"/>
    <bean id="sampleServiceImpl" class="com.ty.sample.service.impl.SampleServiceImpl"/>
</beans>

log4j.properties:

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%p] [%c] - %m%n
log4j.rootCategory=info, console

服务发布 测试类:

public class DubboProviderTest {
	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "config/spring-dubbo-provider.xml" });
		context.start();
		System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
	}

}

服务消费方:

配置文件:spring-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.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
	<dubbo:application name="sample-consumer" />
	<dubbo:registry protocol="zookeeper" address="192.168.50.110:2181" check="true"/>

	<dubbo:reference id="sampleService" timeout="2000"
		interface="com.ty.sample.service.SampleService"  />
</beans>

测试类:DubboConsumerTest

public class DubboConsumerTest {
	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "config/spring-dubbo-consumer.xml" });
		context.start();
		SampleService sampleService = context.getBean("sampleService", SampleService.class);
		String hello = sampleService.sayHello("wyy");
		System.err.println(hello);
		System.in.read();

	}

}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页