现在企业中使用dubbo的越来越多,今天就简单的学习一下dubbo,写了一个hello world,教程仅供入门,如要深入学习请上官网

服务提供方:

首先将提供方和消费方都引入jar包,如果使用的是maven管理项目,可以直接加入dubbo的配置

<!—dubbo start -->

<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> <!—dubbo end --> <!-- zookeeper start -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version></version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient_version}</version>
</dependency>
<!-- zookeeper end –>

声明接口:

public interface UserInfoService { 

    String sayHello(String name);

}

实现接口:

public class UserInfoServiceImpl implements UserInfoService{

    public String sayHello(String name) {
return name + " Hello !";
} }

配置applicationContext.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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <dubbo:application name="hello-world-app"/>
<!-- 注册地址 -->
<dubbo:registry address="zookeeper://192.168.0.123:2181" ></dubbo:registry> <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol> <dubbo:service ref="userInfoService" interface="com.zhiyi.service.UserInfoService" />
<!-- designate implementation -->
<bean id="userInfoService" class="com.zhiyi.service.impl.UserInfoServiceImpl" /> </beans>

程序启动入口:

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.google.common.util.concurrent.AbstractIdleService;

public class BootStart extends AbstractIdleService{

    ClassPathXmlApplicationContext context = null; 

    public static void main(String[] args) {
BootStart bootStart = new BootStart();
bootStart.startAsync();
try {
Object lock = new Object();
synchronized (lock) {
while(true){
lock.wait();
}
}
} catch (Exception e) {
e.printStackTrace();
} } @Override
protected void shutDown() throws Exception {
if( context != null){
context.stop();
}
} @Override
protected void startUp() throws Exception {
String configure = "applicationContext.xml";
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configure); String[] beans = context.getBeanDefinitionNames(); for( String bean : beans){
System.out.println("beanName:"+bean);
}
context.start();
context.registerShutdownHook();
System.out.println("provider is start!"); }
}

服务消费方:

配置applicationContext.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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <dubbo:application name="consumer-of-hello-world"/> <dubbo:registry address="zookeeper://192.168.0.123:2181"></dubbo:registry> <dubbo:reference id="userInfoService" interface="com.zhiyi.service.UserInfoService"></dubbo:reference> </beans>

服务消费方入口:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zhiyi.service.UserInfoService; public class BootStart { public static void main(String[] args) {
BootStart bootStart = new BootStart();
bootStart.start();
} public void start(){
String applicationConfig = "applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(applicationConfig);
String[] beans = context.getBeanDefinitionNames();
for(String bean : beans){
System.out.println("beanName:"+bean);
} UserInfoService userInfoService = (UserInfoService) context.getBean("userInfoService");
System.out.println(userInfoService.sayHello("zhangsan"));
}
}

好了,就是这么简单,dubbo的hello world就完成了,欢迎大神拍砖~

dubbo学习之Hello world的更多相关文章

  1. Dubbo学习小记

    前言 周一入职的新公司,到了公司第一件事自然是要熟悉新公司使用的各种技术,搭建本地的环境. 熟悉新公司技术的过程中,首先就是Maven,这个前面已经写过文章了,然后就是Dubbo----公司的服务都是 ...

  2. dubbo学习(zz)

    dubbo学习 博客分类: 开源软件   Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站 ...

  3. Dubbo学习(六) dubbo 架构图 以及调用过程

    一.Dubbo结构图   duubo结构图 我们解释以下这个架构图:Consumer服务消费者,Provider服务提供者.Container服务容器.消费当然是invoke提供者了,invoke这条 ...

  4. Dubbo学习(一) Dubbo原理浅析

    一.初入Dubbo Dubbo学习文档: http://dubbo.incubator.apache.org/books/dubbo-user-book/ http://dubbo.incubator ...

  5. dubbo学习小结

    dubbo学习小结 参考: https://blog.csdn.net/paul_wei2008/article/details/19355681 https://blog.csdn.net/liwe ...

  6. Dubbo学习系列之十三(Mycat数据库代理)

    软件界有只猫,不用我说,各位看官肯定知道是哪只,那就是大名鼎鼎的Tomcat,现在又来了一只猫,据说是位东方萌妹子,暂且认作Tom猫的表妹,本来叫OpencloudDB,后又改名为Mycat,或许Ca ...

  7. Dubbo学习系列之八(分布式事务之MQ方案)

    自从小王玩起了微服务,发现微服务果然很强大,好处真是太多,心中暗喜,然而,却也遇到了分布式中最棘手的问题:分布式事务.小王遍访各路神仙,也无个完美开源解决方案,当然,也有些实际可行的手法,虽不算完美, ...

  8. Dubbo学习系列之九(Shiro+JWT权限管理)

    村长让小王给村里各系统来一套SSO方案做整合,隔壁的陈家村流行使用Session+认证中心方法,但小王想尝试点新鲜的,于是想到了JWT方案,那JWT是啥呢?JavaWebToken简称JWT,就是一个 ...

  9. Dubbo学习系列之十五(Seata分布式事务方案TCC模式)

    上篇的续集. 工具: Idea201902/JDK11/Gradle5.6.2/Mysql8.0.11/Lombok0.27/Postman7.5.0/SpringBoot2.1.9/Nacos1.1 ...

  10. Dubbo学习系列之十六(ELK海量日志分析框架)

    外卖公司如何匹配骑手和订单?淘宝如何进行商品推荐?或者读者兴趣匹配?还有海量数据存储搜索.实时日志分析.应用程序监控等场景,Elasticsearch或许可以提供一些思路,作为业界最具影响力的海量搜索 ...

随机推荐

  1. 【mysql 优化 5】左连接和右连接优化

    原文地址:8.2.1.8 Left Join and Right Join Optimization mysql以下列方式实现一个A left join B 连接条件: 1,表B设置为依赖于表A和A所 ...

  2. poj 1734 Sightseeing trip判断最短长度的环

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5590   Accepted: 2151 ...

  3. Key-value数据库:Redis缓存服务

    Redis 是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.其提供了多种主流语言的客户端,方便使用:同时Redis支持主 ...

  4. java面试题之Thread和Runnable是什么关系?

    Thread实现了Runnable接口,使得run方法支持多线程: 因类的单一继承原则,推荐多实用Runnable接口

  5. Docker 通俗易懂的入门

    这篇转的文章讲的通俗易懂,算个入门的东西了- 转自:http://www.csdn.net/article/2014-07-02/2820497-what's-docker 尽管之前久闻Docker的 ...

  6. FZOJ 2102 Solve equation

                                                                                                        ...

  7. UVa1476 Error Curves

    画出函数图像后,发现是一个类似V字型的图. 可以用三分法找图像最低点 WA了一串,之后发现是读入优化迷之被卡. /*by SilverN*/ #include<iostream> #inc ...

  8. JavaScript基础深入之----参数传递的分析与总结

    JS的数值类型是分为两类:基本数据类型和引用数据类型. 基本类型占据的内存栈空间,引用类型被保存在堆空间.引用类型赋值的变量也是被保存在栈空间的,它的作用类似于电视遥控器,负责操作堆空间内指向的对象. ...

  9. 基于css3翻牌效果

    <div class="map_block float_l lineItem"> <a href="javascript:;" class=& ...

  10. 共享内存之——mmap内存映射

    共享内存允许两个或多个进程共享一给定的存储区,因为数据不需要来回复制,所以是最快的一种进程间通信机制.共享内存可以通过mmap()映射普通文件 (特殊情况下还可以采用匿名映射)机制实现,也可以通过sy ...