标签:dubbo helloworld
林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka
           Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。
调用关系说明:
0 服务容器负责启动,加载,运行服务提供者。
1. 服务提供者在启动时,向注册中心注册自己提供的服务。
2. 服务消费者在启动时,向注册中心订阅自己所需的服务。
3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
下面以一个实例来做说明,看这个实例前,建议先看看这里 Dubbo-Admin管理平台和Zookeeper注册中心的搭建
提供者的目录如下:工程下载地址:http://download.csdn.net/detail/evankaka/9054253
其中初始化工程的创建如下:
首先,选择创建
然后,next,记得红框打上,这里直接快速
最后,输入项目名等
好了,项目有了,接下来就是加代码了
1、service代码
接口层如下:ProviderService.java
package com.lin.service;
/**
 * 功能概要:提供者service接口层
 * 
 * @author linbingwen
 * @since  2015年8月26日 
 */
public interface ProviderService {
	public String sayHello(String name);
}
实现层如下:ProviderServiceImpl.java
package com.lin.service;
/**
 * 功能概要:功能概要:提供者service实现层
 * 
 * @author linbingwen
 * @since  2015年8月26日 
 */
public class ProviderServiceImpl implements ProviderService {
	public String sayHello(String name) {		
		return "Hello:~~~~~~~~~~~~~~~~~~~~~~~~"+name+"你好,你好~~";
	}
}
2、Spring配置文件-applicationProvider.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 ">   
    <!-- 具体的实现bean -->  
    <bean id="providerService" class="com.lin.service.ProviderServiceImpl" />  
    <!-- 提供方应用信息,用于计算依赖关系 -->  
    <dubbo:application name="dubbo_provider"  />    
    <!-- 使用multicast广播注册中心暴露服务地址   
    <dubbo:registry address="multicast://localhost:1234" />-->   
    <!-- 使用zookeeper注册中心暴露服务地址 -->  
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />     
    <!-- 用dubbo协议在20880端口暴露服务 -->  
    <dubbo:protocol name="dubbo" port="29014" />  
    <!-- 声明需要暴露的服务接口 -->  
    <dubbo:service interface="com.lin.service.ProviderService" ref="providerService" />  
</beans> 3、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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.lin</groupId>
	<artifactId>dubbo_provider</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<properties>
		<spring.version>3.2.8.RELEASE</spring.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
		</dependency>
		<!-- spring相关 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>${spring.version}</version>
		</dependency>
	</dependencies>
</project>在src/test/java下添加
ProviderServiceTest.java内容如下:
package com.lin.service;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 功能概要:
 * 
 * @author linbingwen
 * @since  2015年8月26日 
 */
public class ProviderServiceTest {
	/**
	 * @author linbingwen
	 * @since  2015年8月26日 
	 * @param args    
	 */
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
                new String[]{"applicationProvider.xml"});  
        context.start(); 
        System.out.println("提供者服务已注册成功");  
        System.out.println("请按任意键取消提供者服务");  
        try {
			System.in.read();//让此程序一直跑,表示一直提供服务
		} catch (IOException e) {		
			e.printStackTrace();
		}  
	}
}
运行结果:
因为我这里使用的是本地的zookeeper注册中心,所以一定要先打开它!!!一定要先打开它!!!一定要先打开它!!!打开D:\Java\Tool\zookeeper-3.4.6\bin下的zkServer.cmd,然后一直开着,不要关了!!!!!!!!!!!!!然后一直开着,不要关了!!!!!!!!!!!!!
接着运行上面的main方法,输出如下:
然后我们可以到注册中心去看看服务注册上去了没有。(注意,这里的zookeper注册中心在我本地电脑已搭建好,搭建过程看这里Dubbo-Admin管理平台和Zookeeper注册中心的搭建)
可以看到,提供者已注册成功。
1、还是一个maven项目,整个结构如下,工程下载地址:http://download.csdn.net/detail/evankaka/9054253
2、Spring配置文件applicationConsumer.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="dubbo_consumer" />     
      <!-- 使用multicast广播注册中心暴露发现服务地址 -->  
    <dubbo:registry  protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />       
      <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->  
    <dubbo:reference id="providerService" interface="com.lin.service.ProviderService" />  
</beans> <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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.lin</groupId>
	<artifactId>dubbo_consumer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<properties>
		<spring.version>3.2.8.RELEASE</spring.version>
	</properties>
	<dependencies>
		<!-- 添加provider的jar包 -->
		<dependency>
			<groupId>com.lin</groupId>
			<artifactId>dubbo_provider</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<!-- 添加dubbo依赖 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- 添加zk客户端依赖 -->
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
		</dependency>
		<!-- spring相关 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>${spring.version}</version>
		</dependency>
	</dependencies>
</project>和提供者的POM文件基本上是一样的,只不过加了对提供者的依赖。4、消费者调用提供者
在src/test/java写一个ConsumerServiceTest.java
package com.lin.service;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 功能概要:
 * 
 * @author linbingwen
 * @since  2015年8月26日 
 */
public class ConsumerServiceTest {
	/**
	 * @author linbingwen
	 * @since  2015年8月26日 
	 * @param args    
	 */
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "applicationConsumer.xml" });
		
		context.start();
		ProviderService providerService = (ProviderService) context.getBean("providerService");
		System.out.println(providerService.sayHello("林炳文Evankaka"));
		System.out.println("Press any key to exit.");  
        try {
			System.in.read();
		} catch (IOException e) {		
			e.printStackTrace();
		}  
	}
}
然后启动:再打开注册中心,发面有消费者在使用提供者,因为都是一个电脑,所以IP都一样。当然。其它电脑也可以访问这个提供者的!
版权声明:本文为博主林炳文Evankaka原创文章,转载请注明出处http://blog.csdn.net/evankaka
标签:dubbo helloworld
原文地址:http://blog.csdn.net/evankaka/article/details/48009645