注册中心搭建好之后,现在开始进行一个dubbo实例,来体验一下dubbo的魅力。
为了方便的管理jar包,本次使用的是maven项目。
项目主要结构:
提供者:
消费者:
实现过程:
本次的demo属于简单入门,所以从代码上来看没有多少,主要是看dubbo是如何进行提供分布式服务的。
服务提供者:
首先配置pom文件,引入必要的jar包:
<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.wpb.dubbo</groupId> <artifactId>dubbo-provide</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <spring.version>4.1.3.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</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-webmvc</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-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.4.10</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.3</version> </dependency> </dependencies> </project>
接口,就定义了一个sayHello方法:
public interface DubboProvide { void sayHello(); }接口实现方法:
public class DubboProvideImpl implements DubboProvide { public void sayHello() { System.out.println("this is my first dubbo program"); } }方法定义好之后,进行核心配置文件的配置ApplicaionContextProducror.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服务者起个名 --> <dubbo:application name="productor"/> <!-- 用zookeeper注册服务中心暴露服务地址 --> <dubbo:registry protocol="zookeeper" address="zookeeper://192.168.91.130:2181"/> <!-- 暴露dubbo的通信端口 --> <dubbo:protocol name="dubbo" port="20880"/> <!-- 给消费者提供服务的接口 --> <dubbo:service ref="bubboProdutor" interface="com.dubbo.service.DubboProvide"></dubbo:service> <!-- 提供服务的实现类 --> <bean id="bubboProdutor" class="com.dubbo.service.impl.DubboProvideImpl"></bean> </beans>然后创建main方法来启动服务,方法如下:
public class DubboProvideStart { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext cfg = new ClassPathXmlApplicationContext("classpath:config/ApplicationContextProducror.xml"); cfg.start(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); System.out.println("provider service start time:"+sdf.format(date)); //保证服务始终开启 System.in.read(); } }
服务提供方启动之后,再来看一下消费者的主要代码:
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.wpb.dubbo</groupId> <artifactId>dubbo-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <spring.version>4.1.3.RELEASE</spring.version> </properties> <dependencies> <dependency> <!-- 引入提供方服务接口 --> <groupId>com.wpb.dubbo</groupId> <artifactId>dubbo-provide</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</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-webmvc</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-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.4.10</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.3</version> </dependency> </dependencies> </project>配置文件ApplicationContextCustomer.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="customer"/> <!-- 用zookeeper注册服务中心发现服务地址 --> <dubbo:registry protocol="zookeeper" address="zookeeper://192.168.91.130:2181"/> <!-- 调用远程的接口 --> <dubbo:reference id="dubboProvider" interface="com.dubbo.service.DubboProvide"/> </beans>配置好之后,就可以创建main方法来启动消费者了,方法如下:
public class CustomerMain { public static void main(String[] args) throws InterruptedException{ ClassPathXmlApplicationContext cfig = new ClassPathXmlApplicationContext("classpath:config/ApplicationContextCustomer.xml"); DubboProvide test = (DubboProvide) cfig.getBean("dubboProvider"); System.out.println("consumer connect to service begin"); test.sayHello(); Thread.sleep(100000); System.out.println("consumer connect to service end"); } }demo下载地址:dubbo+zookeeper入门实例
至此,dubbo的提供方和消费方已经创建好,我们也可以通过启动程序来看到真实的效果,然后就可以通过代码来感受一下dubbo的优点。