现在企业中使用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. 九度oj 题目1416:猴子吃坚果

    题目描述: 动物园的猴子吃坚果的顺序都是按强壮程度来定的,最强壮的吃完才能轮到下一个,现在我们给出各个猴子的名字,强壮程度,吃饱的量,然后查询对应的猴子必须要扔多少坚果才可以轮到. 输入: 输入有多组 ...

  2. 【bzoj2225】[Spoj 2371]Another Longest Increasing CDQ分治+树状数组

    题目描述 给定N个数对(xi, yi),求最长上升子序列的长度.上升序列定义为{(xi, yi)}满足对i<j有xi<xj且yi<yj. 样例输入 8 1 3 3 2 1 1 4 5 ...

  3. 刷题总结——树的同构(bzoj4337 树上hash)

    Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如 ...

  4. JS当中利用&&和||简化代码

    ; ){ add_level = ; } ){ add_level = ; } ){ add_level = ; } ){ add_level = ; } else { add_level = ; } ...

  5. bzoj 3190 [JLOI2013]赛车 半平面交+细节处理

    题目大意 这里有一场赛车比赛正在进行,赛场上一共有N辆车,分别称为g1,g2--gn.赛道是一条无限长的直线.最初,gi位于距离起跑线前进ki的位置.比赛开始后,车辆gi将会以vi单位每秒的恒定速度行 ...

  6. vue.js源码学习分享(六)

    /* */ /* globals MutationObserver *///全局变化观察者 // can we use __proto__?//我们能用__proto__吗? var hasProto ...

  7. 35深入理解C指针之---结构体基础

    一.结构体基础 1.定义:结构体大大加强了C的数据聚合能力,可以使得不同类型的数据进行结合 2.特征: 1).结构体可以使得不同类型的数据进行结合 2).结构体可以使用内置的数据类型,包括指针 3). ...

  8. dedecms--会员信息导出excel表格

    1:在dede/templets下面的member_main.htm,在全选按钮那里添加一个导出excel按钮:代码如下: <a href="toexcel.php" cla ...

  9. git commit或pull后恢复到原来版本

    https://blog.csdn.net/litao31415/article/details/87713712

  10. HDOJ 5360 Hiking 优先队列+贪心

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5360 题意: 大概的意思就是每个人有个人数接受范围$[l_i,r_i]$,现在你每次能从还未被选取的人 ...