Dubbo入门教程

服务端(dubbo-server)

1. pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.lwb</groupId><artifactId>dubbo-server</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- spring begin --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.1.6.RELEASE</version></dependency><!-- spring end --><!-- dubbo begin --><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.5.3</version></dependency><!-- dubbo end --><!-- 注册中心zookeeper begin --><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.3.6</version></dependency><!-- 注册中心zookeeper end --><!-- log begin --><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1.1</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.15</version><exclusions><exclusion><groupId>com.sun.jdmk</groupId><artifactId>jmxtools</artifactId></exclusion><exclusion><groupId>com.sun.jmx</groupId><artifactId>jmxri</artifactId></exclusion><exclusion><artifactId>jms</artifactId><groupId>javax.jms</groupId></exclusion><exclusion><artifactId>mail</artifactId><groupId>javax.mail</groupId></exclusion></exclusions></dependency><!-- log end --><!-- other begin --><dependency><groupId>org.jboss.netty</groupId><artifactId>netty</artifactId><version>3.2.0.Final</version></dependency><dependency><groupId>com.101tec</groupId><artifactId>zkclient</artifactId><version>0.8</version></dependency><!-- other end --></dependencies>
</project>

2. dubbo.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://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd"><dubbo:application name="dubbo-server" owner="lwb" /><!-- zookeeper注册中心 --><dubbo:registry address="zookeeper://127.0.0.1:2181" /><dubbo:protocol contextpath="dubbo" port="20881" /><dubbo:monitor protocol="registry"/><!-- 服务具体实现 --><bean id="elasticService" class="com.lwb.service.impl.ElasticServiceImpl" /><!-- 向注册中心注册暴漏服务地址,注册服务 --><dubbo:service interface="com.lwb.service.ElasticService" ref="elasticService" protocol="dubbo" timeout="50000"/>

3. 代码

package com.lwb.service;/*** @author liuweibo* @date 2018/5/9*/
public interface ElasticService {String helloDubbo(String msg);
}
package com.lwb.service.impl;import com.lwb.service.ElasticService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;/*** @author liuweibo* @date 2018/5/9*/
public class ElasticServiceImpl implements ElasticService {private static final Logger LOGGER = LoggerFactory.getLogger(ElasticServiceImpl.class);public String helloDubbo(String msg) {System.out.println(msg);return "hi!" + msg;}
}

4. 项目结构

在这里插入图片描述

5. 启动类(Application.java)

这里就直接用main方法启动了

package com.lwb;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.io.IOException;/*** @author liuweibo* @date 2018/5/9*/
public class Application {public static void main(String[] args) throws IOException {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "dubbo.xml" });context.start();System.out.println("任意按键退出");System.in.read();context.close();}
}

客户端(dubbo-client)

1. pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.lwb</groupId><artifactId>dubbo-client</artifactId><version>1.0-SNAPSHOT</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- spring begin --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.1.6.RELEASE</version></dependency><!-- spring end --><!-- dubbo begin --><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.5.3</version></dependency><!-- dubbo end --><!-- 注册中心zookeeper begin --><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.3.6</version></dependency><!-- 注册中心zookeeper end --><!-- log begin --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.15</version><exclusions><exclusion><groupId>com.sun.jdmk</groupId><artifactId>jmxtools</artifactId></exclusion><exclusion><groupId>com.sun.jmx</groupId><artifactId>jmxri</artifactId></exclusion><exclusion><artifactId>jms</artifactId><groupId>javax.jms</groupId></exclusion><exclusion><artifactId>mail</artifactId><groupId>javax.mail</groupId></exclusion></exclusions></dependency><!-- log end --><!-- other begin --><dependency><groupId>com.101tec</groupId><artifactId>zkclient</artifactId><version>0.8</version></dependency><dependency><groupId>com.lwb</groupId><artifactId>dubbo-server</artifactId><version>1.0-SNAPSHOT</version></dependency><!-- other end --></dependencies></project>

2. dubbo.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://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd"><dubbo:application name="consumer-of-dubbo-demo" /><dubbo:registry address="zookeeper://127.0.0.1:2181" /><!-- 向注册中心订阅服务 --><dubbo:reference id="elasticService" interface="com.lwb.service.ElasticService" /></beans>

3. 服务调用(这里为了简单,就在main方法里面处理了哈,Application.java)

package com.lwb;import com.lwb.service.ElasticService;
import com.lwb.service.impl.ElasticServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @author liuweibo* @date 2018/5/9*/
public class Application {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "dubbo.xml" });context.start();ElasticService service = (ElasticService) context.getBean("elasticService");System.out.println(service.helloDubbo("world"));context.close();}
}

4. 项目结构

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/249687.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

NSAssert和NSParameterAssert

2016.05.05 18:34* 字数 861 阅读 5127评论 0喜欢 17https://www.jianshu.com/p/3072e174554fNSAssert和NSParameterAssert在开发环境中经常被使用&#xff0c;调试和验证代码参数的完整性&#xff0c;断言为真&#xff0c;则表明程序运行正常&#xff0c;而断言为假&#xff0…

【PAT】B1070 结绳(25 分)

此题太给其他25分的题丢人了&#xff0c;只值15分 注意要求最终结果最长&#xff0c;而且向下取整 #include<stdio.h> #include<algorithm> using namespace std; float arr[10005]; int main(){int N;scanf("%d",&N);for(int i0;i<N;i)//输入数据…

Java代码实现负载均衡五种算法

前言&#xff1a; 负载均衡是为了解决并发情况下&#xff0c;多个请求访问&#xff0c;把请求通过提前约定好的规则转发给各个server。其中有好几个种经典的算法。在用java代码编写这几种算法之前&#xff0c;先来了解一下负载均衡这个概念。 1.概念 负载&#xff0c;从字面…

使用Nodejs发送邮件

尝试用了Nodemailer来发送邮件&#xff0c;结果成功了&#xff0c;虽然是相对比较简单的&#xff0c;但还是记录一下吧。 Nodemailer 是 Node.js 应用程序的一个模块&#xff0c;可以方便地发送电子邮件。 使用 # 初始化 pageage.json 文件 $ npm init # 安装依赖 $ npm ins…

HTTP同源策略

同源策略是web安全策略中的一种&#xff0c;非常重要。 同源策略明确规定&#xff1a;不同域的客户端在没有明确授权的情况下&#xff0c;不能读写对方的资源。 简单说来就是web浏览器允许第一个页面的脚本访问访问第二个页面的数据&#xff0c;但是也只有在两个页面有相同的…

Spring Cloud 微服务架构

一、分布式服务框架的发展 1.1 第一代服务框架   代表&#xff1a;Dubbo(Java)、Orleans(.Net)等 特点&#xff1a;和语言绑定紧密 1.2 第二代服务框架   代表&#xff1a;Spring Cloud等 现状&#xff1a;适合混合式开发&#xff08;例如借助Steeltoe OSS可以让ASP.Ne…

JZOJ 4421. aplusb

4421. aplusb Time Limits: 1000 ms Memory Limits: 524288 KB Detailed Limits Goto ProblemSetDescription SillyHook要给小朋友出题了&#xff0c;他想&#xff0c;对于初学者&#xff0c;第一题肯定是ab 啊&#xff0c;但当他出完数据后神奇地发现.in不见了&#xff0c…

跨域资源共享CORS详解

最近深入了解了CORS的相关东西&#xff0c;觉得阮一峰老师的文章写得最详细易懂了&#xff0c;所有转载作为学习笔记。 原文地址&#xff1a;跨域资源共享 CORS 详解 CORS是W3C的一个标准&#xff0c;全称是跨域资源共享&#xff08;Cross-origin resource sharing&#xff0…

计算机网络(十),HTTP的关键问题

目录 1.在浏览器地址栏键入URL&#xff0c;按下回车之后经历的流程 2.HTTP状态码 3.GET请求和POST请求的区别 4.Cookie和Session的区别 5.IPV4和IPV6 十、HTTP的关键问题 1.在浏览器地址栏键入URL&#xff0c;按下回车之后经历的流程 &#xff08;1&#xff09;DNS解析 &#x…

云技术

云技术是指在广域网或局域网内将硬件、软件、网络等系列资源统一起来&#xff0c;实现数据的计算、储存、处理和共享的一种托管技术。

vue中 mock使用教程

//mock/index.js import Mock from mockjs //引入mockjs&#xff0c;npm已安装 import { Random,toJSONSchema } from mockjs // 引入random对象,随机生成数据的对象&#xff0c;&#xff08;与占位符一样&#xff09; Mock.setup({timeout:1000 //设置请求延时时间 }) const …

前端开发掌握nginx常用功能之rewrite

上一篇博文对nginx最常用功能的server及location的匹配规则进行了讲解&#xff0c;这也是nginx实现控制访问和反向代理的基础。掌握请求的匹配规则算是对nginx有了入门&#xff0c;但是这些往往还是不能满足实际的需求场景&#xff0c;例如请求url重写、重定向等等&#xff0c;…

vue2.0脚手架的webpack 配置文件分析

前言 作为 Vue 的使用者我们对于 vue-cli 都很熟悉&#xff0c;但是对它的 webpack 配置我们可能关注甚少&#xff0c;今天我们为大家带来 vue-cli#2.0 的 webpack 配置分析 vue-cli 的简介、安装我们不在这里赘述&#xff0c;对它还不熟悉的同学可以直接访问 vue-cli 查看 …

一个可供中小团队参考的微服务架构技术栈

一个可供中小团队参考的微服务架构技术栈

WinSxS文件夹瘦身

WinSxS文件夹瘦身2014-5-8 18:03:32来源&#xff1a;IT之家作者&#xff1a;阿象责编&#xff1a;阿象 评论&#xff1a;27刚刚&#xff0c;我们分享了如何用DISM管理工具查看Win8.1 WinSxS文件夹实际大小。对于WinSxS文件夹&#xff0c;几乎每个Windows爱好者都认识到其重要性…

bcrypt的简单使用

前段时间在捣鼓个人项目的时候用到了nodejs做服务端&#xff0c;发现使用加密的方法和之前常用的加密方式不太一致&#xff0c;下面以demo的形式总结一下bcrypt对密码进行加密的方法。 一、简介 Bcrypt简介&#xff1a; bcrypt是一种跨平台的文件加密工具。bcrypt 使用的是布…

盒子居中

1、未脱标 margin&#xff1a;0 auto&#xff1b; 2、脱标&#xff08;absolute、fixed&#xff09; left&#xff1a;50%&#xff1b; margin-left&#xff1a;width/2&#xff1b; 转载于:https://www.cnblogs.com/liujianing/p/10356984.html

织梦无子栏目时禁止调用同级栏目

1. 修改文件 \include\taglib\channel.lib.php 把代码 if($typeson && $reid!0 && $totalRow0) 改为 if($typeson && $reid!0 && $totalRow0 && $noself) 2. 使用channel标签时添加noself属性 {dede:channel noselfyes} {/dede:channe…

nodejs实现文件上传

前段时间在做个人项目的时候&#xff0c;用到了nodejs服务端上传文件&#xff0c;现在回头把这个小结一下&#xff0c;作为记录。 本人上传文件时是基于express的multiparty&#xff0c;当然也可以使用connect-multiparty中间件实现&#xff0c;但官方似乎不推荐使用connect-m…