dubbo hello world
3/3/2017来源:C/C++教程人气:3522
随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进。
1、dubbo架构:
2、节点角色说明
PRovider: 暴露服务的服务提供方。 Consumer: 调用远程服务的服务消费方。 Registry: 服务注册与发现的注册中心。 Monitor: 统计服务的调用次调和调用时间的监控中心。 Container: 服务运行容器。3、调用关系说明
服务容器负责启动,加载,运行服务提供者。 服务提供者在启动时,向注册中心注册自己提供的服务。 服务消费者在启动时,向注册中心订阅自己所需的服务。 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。4、Helloworld
其实dubbo里面最主要的角色就是:
Provider: 暴露服务的服务提供方。 Consumer: 调用远程服务的服务消费方。 Registry: 服务注册与发现的注册中心。 Container: 服务运行容器。服务注册官方提供4种注册方式:
Zookeeper注册中心:依赖于Zookeeper的稳定性,可用于生产环境。 Redis注册中心: 要求服务器时间同步,用于检查心跳过期脏数据,可用于生产环境。 Multicast注册中心:依赖于网络拓普和路由,跨机房有风险,小规模应用或开发测试环境 Simple注册中心:没有集群支持,可能单点故障,试用。因为我们只是一个简单的例子,就不引进其它组件,我们使用Multicast方式来做为我们的注册中心,使用Spring来做为我们的容器。
1、provider
1) 项目结构图:
提供一个简单的远程服务DemoService,只有一个接口那就是sayHello.
2) pom.xml
使用maven的方式来管理Jar包,主要是引用spring与dubbo。
<?xml version="1.0" encoding="UTF-8"?> <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.weimob.o2o.carl</groupId> <artifactId>dubbo-service</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <spring.version>4.2.6.RELEASE</spring.version> <dubbo.version>2.5.3</dubbo.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</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-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </project>3) DemoService.java
定义一个接口,声明sayHello方法。
public interface DemoService { String sayHello(String name); }4) DemoServiceImpl.java
实现DemoService,提供服务的具体实现。
public class DemoServiceImpl implements DemoService { public String sayHello(String name) { return "hello," + name; } }5) provider-bean.xml
使用Spring配置文件配置provider类型的bean.
<?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="hello-world-app" /> <!-- 使用multicast广播注册中心暴露服务地址 --> <dubbo:registry address="multicast://224.5.6.7:1234" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.weimob.o2o.carl.provider.DemoService" ref="demoService" /> <!-- 和本地bean一样实现服务 --> <bean id="demoService" class="com.weimob.o2o.carl.provider.impl.DemoServiceImpl" /> </beans>6) Provider.java
使用Spring容器启动服务,并注册到Multicast注册中心里面,以提Consumer方调用。
public class Provider { public static void main(String[] args) throws InterruptedException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider-bean.xml"); context.start(); System.out.println("service provider start..."); new CountDownLatch(1).await(); } }7) 启动Spring容器
运行Provider.java,如果控制台如下显示,则表示服务发布成功:
2、Consumer
1) 项目结构图
调用Provider提供的远程服务DemoService.
2) pom.xml
服务调用方的pom配置,主要是引入服务提供方的接口。
<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.weimob.o2o.carl</groupId> <artifactId>dubbo-client</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>dubbo-client</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.2.6.RELEASE</spring.version> <dubbo.version>2.5.3</dubbo.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.weimob.o2o.carl</groupId> <artifactId>dubbo-service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</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-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>3) consumer-bean.xml
使用Spring的Bean的方式来配置consumer,并引入需要调用的远程服务的Class.
<?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="consumer-of-helloworld-app" /> <!-- 使用multicast广播注册中心暴露发现服务地址 --> <dubbo:registry address="multicast://224.5.6.7:1234" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.weimob.o2o.carl.provider.DemoService" /> </beans>4) Consumer
consumer调用暴露在multicast注册中心中的服务。
public class Consumer { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer-bean.xml"); context.start(); DemoService demoService = (DemoService)context.getBean("demoService", DemoService.class); // 获取远程服务代理 String hello = demoService.sayHello("carl"); // 执行远程方法 System.out.println( hello ); // 显示调用结果 } }5) 调用远程服务
启动Consumer.java,测试远程调用服务:
如果控制台显示:hello, carl.那么恭喜你,你成功了。
最新文章推荐