dubbo学习之Hello world
现在企业中使用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的更多相关文章
- Dubbo学习小记
前言 周一入职的新公司,到了公司第一件事自然是要熟悉新公司使用的各种技术,搭建本地的环境. 熟悉新公司技术的过程中,首先就是Maven,这个前面已经写过文章了,然后就是Dubbo----公司的服务都是 ...
- dubbo学习(zz)
dubbo学习 博客分类: 开源软件 Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站 ...
- Dubbo学习(六) dubbo 架构图 以及调用过程
一.Dubbo结构图 duubo结构图 我们解释以下这个架构图:Consumer服务消费者,Provider服务提供者.Container服务容器.消费当然是invoke提供者了,invoke这条 ...
- Dubbo学习(一) Dubbo原理浅析
一.初入Dubbo Dubbo学习文档: http://dubbo.incubator.apache.org/books/dubbo-user-book/ http://dubbo.incubator ...
- dubbo学习小结
dubbo学习小结 参考: https://blog.csdn.net/paul_wei2008/article/details/19355681 https://blog.csdn.net/liwe ...
- Dubbo学习系列之十三(Mycat数据库代理)
软件界有只猫,不用我说,各位看官肯定知道是哪只,那就是大名鼎鼎的Tomcat,现在又来了一只猫,据说是位东方萌妹子,暂且认作Tom猫的表妹,本来叫OpencloudDB,后又改名为Mycat,或许Ca ...
- Dubbo学习系列之八(分布式事务之MQ方案)
自从小王玩起了微服务,发现微服务果然很强大,好处真是太多,心中暗喜,然而,却也遇到了分布式中最棘手的问题:分布式事务.小王遍访各路神仙,也无个完美开源解决方案,当然,也有些实际可行的手法,虽不算完美, ...
- Dubbo学习系列之九(Shiro+JWT权限管理)
村长让小王给村里各系统来一套SSO方案做整合,隔壁的陈家村流行使用Session+认证中心方法,但小王想尝试点新鲜的,于是想到了JWT方案,那JWT是啥呢?JavaWebToken简称JWT,就是一个 ...
- Dubbo学习系列之十五(Seata分布式事务方案TCC模式)
上篇的续集. 工具: Idea201902/JDK11/Gradle5.6.2/Mysql8.0.11/Lombok0.27/Postman7.5.0/SpringBoot2.1.9/Nacos1.1 ...
- Dubbo学习系列之十六(ELK海量日志分析框架)
外卖公司如何匹配骑手和订单?淘宝如何进行商品推荐?或者读者兴趣匹配?还有海量数据存储搜索.实时日志分析.应用程序监控等场景,Elasticsearch或许可以提供一些思路,作为业界最具影响力的海量搜索 ...
随机推荐
- javascript学习笔记 - 引用类型 RegExp
四 RegExp 格式: var expression = / pattern / flags; 1.flags 为标志.分别为g.i.m. g:表示全局模式.即模式将匹配所有的字符串,而不是在发现第 ...
- java jstl标签
转自:http://blog.csdn.net/liushuijinger/article/details/9143793 JSTL(JSP Standard Tag Library ,JSP标准标签 ...
- 【转】PhpStorm 提交代码到远程GitHub仓库
转载地址:http://my.oschina.net/lujianing/blog/180728 1.下载github for window http://windows.github.com/ 2. ...
- iOS学习笔记40-日志重定向
一.日志重定向 我们在iOS开发过程中,我们时常会使用NSLog打印到控制台的日志信息进行代码调试,但这样调试的前提是连接上Xcode.如果进行真机调试但同时又不能连接Xcode的时候,就不能直接在x ...
- CS231n笔记 Lecture 5 Convolutional Neural Networks
一些ConvNets的应用 Face recognition 输入人脸,推测是谁 Video classfication Recognition 识别身体的部位, 医学图像, 星空, 标志牌, 鲸.. ...
- NOJ——1659求值(log10取对数+floor取整数部分+可有可无的快速幂)
[1659] 求值 时间限制: 1000 ms 内存限制: 65535 K 问题描述 给你三个数a,b,c,求a的b次的前c位数(不够c位输出全部即可) 输入 输入数据有多组,每组占一行,有三个整数, ...
- PHP中的验证码类(完善验证码)
运行结果: <!--vcode.class.php--> <?php class Vcode { private $width; //宽 private $height; //高 p ...
- Problem 2111 Min Number
...
- 长度rem的使用
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- Office文件上传自动生成缩略图-C#开发
原文: http://www.knowsky.com/898407.html 上传office文件的时候需要将首页自动截图,用于显示文件列表的时候将文件第一页缩略图展示给用户.实现的方式有多种,这里给 ...