随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进。
其实dubbo里面最主要的角色就是:
服务注册官方提供4种注册方式:
因为我们只是一个简单的例子,就不引进其它组件,我们使用Multicast方式来做为我们的注册中心,使用Spring来做为我们的容器。
1) 项目结构图:
提供一个简单的远程服务DemoService,只有一个接口那就是sayHello.
2) pom.xml
使用maven的方式来管理Jar包,主要是引用spring与dubbo。
<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.0modelVersion>
<groupId>com.weimob.o2o.carlgroupId>
<artifactId>dubbo-serviceartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>jarpackaging>
<properties>
<spring.version>4.2.6.RELEASEspring.version>
<dubbo.version>2.5.3dubbo.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-expressionartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
<exclusions>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>springartifactId>
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.
<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" />
<dubbo:registry address="multicast://224.5.6.7:1234" />
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:service interface="com.weimob.o2o.carl.provider.DemoService" ref="demoService" />
<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,如果控制台如下显示,则表示服务发布成功:
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.0modelVersion>
<groupId>com.weimob.o2o.carlgroupId>
<artifactId>dubbo-clientartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>jarpackaging>
<name>dubbo-clientname>
<url>http://maven.apache.orgurl>
<properties>
<spring.version>4.2.6.RELEASEspring.version>
<dubbo.version>2.5.3dubbo.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>com.weimob.o2o.carlgroupId>
<artifactId>dubbo-serviceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-expressionartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
<exclusions>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>springartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
dependencies>
project>
3) consumer-bean.xml
使用Spring的Bean的方式来配置consumer,并引入需要调用的远程服务的Class.
<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" />
<dubbo:registry address="multicast://224.5.6.7:1234" />
<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.那么恭喜你,你成功了。