现在企业中使用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. BZOJ 4817 [Sdoi2017]树点涂色 ——LCT 线段树

    同BZOJ3779. SDOI出原题,还是弱化版的. 吃枣药丸 #include <map> #include <cmath> #include <queue> # ...

  2. ubuntu下卸载python2和升级python3.5

    卸载python只需一条语句就可以实现 sudu apt-get remove python ubuntu下安装python3 sudo apt-get install python3 但这样只安装了 ...

  3. 关于console.log() 打印得引用类型得数据得相关问题

    console.log()打印出来得是这个引用类型最终得结果,而不是在打印得时候当前得值 ,b:} console.log(json) json.a = ; 如上  ,打印得将是  {a:3,b:2} ...

  4. 使用electron将单页面vue webapp 打包成 PC端应用

    在看张鑫旭博客得时候看到了electron这个东西,来了兴趣,就按照上面写的将已经做好得vue项目拿来试了试,出乎意料得顺利 electron简单说下electron,就是把 chrome内核和你的项 ...

  5. google jib容器打包工具

    简介 Jib 是 Google 开发的可以直接构建 Java 应用的 Docker 和 OCI 镜像的类库,以 Maven 和 Gradle 插件形式提供. 通过 Jib,Java 开发者可以使用他们 ...

  6. mongodb window安装学习

    https://blog.csdn.net/u011692780/article/details/81223525 教程:http://www.runoob.com/mongodb/mongodb-t ...

  7. WebRTC 介绍 (转)

    google开源了WebRTC项目,网址是:http://code.google.com/p/webrtc/. WebRTC实现了基于网页的视频会议,标准是WHATWG 协议,目的是通过浏览器提供简单 ...

  8. 致命错误:ext/standard/php_smart_str.h:没有那个文件或目录

    致命错误:ext/standard/php_smart_str.h:没有那个文件或目录 参考文章:https://blog.csdn.net/jartins/article/details/80371 ...

  9. Struts2的上传与下载

    转自:http://blog.csdn.net/Mark_LQ/article/details/49822821 10.1.1 文件上传基本案例   第一步:上传组件依赖与commons-fileup ...

  10. AC日记——让我们异或吧 洛谷 P2420

    题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中…xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor( B是否是男生)=A和B是否能 ...