dubbo进阶--入门实例
来源:互联网 发布:英国可以用淘宝吗 编辑:程序博客网 时间:2024/05/21 09:30
注册中心搭建好之后,现在开始进行一个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的优点。
0 0
- dubbo进阶--入门实例
- Dubbo入门实例
- Dubbo入门实例
- Dubbo入门实例
- Dubbo入门实例
- dubbo入门实例
- Dubbo入门实例--转载
- dubbo入门实例代码
- Dubbo+Zookeeper入门实例
- Dubbo快速入门实例
- dubbo入门实例-demo
- Dubbo-入门指南+实例
- Dubbo入门实例
- Dubbo官方入门实例
- Dubbo+Zookeeper入门实例
- dubbo+Springboot入门实例
- dubbo系统学习(一)-dubbo入门实例
- Dubbo入门基础与实例讲解
- ArduPilot Copter Release Notes 中文翻译
- java基础—I/O流
- 面试机试问题之备考
- hdoj-2566-统计硬币(解题报告)
- SQL语言
- dubbo进阶--入门实例
- LeetCode70. Climbing Stairs
- 魔力手环
- 蓝桥杯☆难度题目(6,7题)
- 2017.04.09:Mysql编程
- 数据结构—栈应用(一)数制转换
- JavaScript中linux时间戳与日期的转换
- Edittext文字飞入飞出效果
- Isomap