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或许可以提供一些思路,作为业界最具影响力的海量搜索 ...
随机推荐
- Lyft Level 5 Challenge 2018 - Final Round (Open Div. 2) A. The King's Race
http://codeforces.com/contest/1075/problem/A On a chessboard with a width of nn and a height of nn, ...
- 【Vjudge】P558E A Simple Task(线段树暴力)
题目链接 这题……太暴力了吧…… 开二十六棵线段树维护l到r字符i出现的次数,然后修改的时候暴力修改,输出的时候暴力输出……就过了…… 然后我还没想到…… qwq #include<cstdio ...
- HDU——2191悼念512汶川大地震遇难同胞(多重背包转化为01背包或二进制优化)
悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- Resource 定位、BeanDefinition 的载入和解析,BeanDefinition 注册。
在前文提过,IOC 容器的初始化过程分为三步骤:Resource 定位.BeanDefinition 的载入和解析,BeanDefinition 注册. Resource 定位.我们一般用外部资源来描 ...
- BZOJ3507 [Cqoi2014]通配符匹配 【哈希 + 贪心】
题目 几乎所有操作系统的命令行界面(CLI)中都支持文件名的通配符匹配以方便用户.最常见的通配符有两个,一个 是星号(""'),可以匹配0个及以上的任意字符:另一个是问号(&quo ...
- java面试题之为什么hashmap的数组初始化大小都是2的N次方?
当数组长度为2的N次方时,不同的key算出的index相同的几率小,数据在数组上分配均匀,hash碰撞的几率小,提升查询效率,从大O(N)提升至O(1):
- Linux目录结构简析
Linux目录结构简析 Linux继承了unix操作系统结构清晰的特点.在linux下的文件结构非常有条理.但是,上述的优点只有在对linux相当熟悉时,才能体会到.现在,虫虫就把linux下的目录结 ...
- vue 按需加载
vue 构建单页面应用,但是问题是随着系统的体积变大,js文件也体积太大了,这时候就需要按需要进行加载了 vue-router提供了懒加载的方式 const Foo = resolve => r ...
- net5:动态修改内存中的站点地图节点
原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- php接口开发时,数据解析失败问题,字符转义,编码问题
php接口开发时,数据解析失败问题,字符转义,编码问题 情景: A平台--->向接口请求数据---->接口向B平台请求数据---->B平台返回数据给接口---->接口返回数据给 ...