dubbo本地搭建实例
概述
其核心部分包含
- 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
- 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
- 自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。
Dubbo能做什么
透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。
软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。
主要核心部件
Remoting: 网络通信框架,实现了sync-over-async 和 request-response 消息机制.
RPC: 一个远程过程调用的抽象,支持负载均衡、容灾和集群功能
Registry: 服务目录框架用于服务的注册和服务事件发布和订阅。
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
实例
搭建maven web项目
不会搭建maven项目的可以参考我的博客:http://blog.csdn.net/aqsunkai/article/details/51286373
本例我搭建了两个项目:dubbo-provider和dubbo-customer
修改配置文件
dubbo-provider项目
在pom.xml文件中增加dubbo、zookeeper、zkclient的jar包:
<!-- http://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- http://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.8</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
<!-- <type>pom</type> -->
</dependency>
因为要作为web项目启动,web.xml文件中需要增加:
必须有ContextLoaderListener监听器,applicationContext.xml才会成功加载
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
下面是DemoService和DemoServiceImpl的内容
public interface DemoService {
String getName(String firstName,String lastName);
}
public class DemoServiceImpl implements DemoService{
@Override
public String getName(String firstName, String lastName) {
return "hello, "+firstName+" " +lastName;
}
}
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
"> <!-- 具体的实现bean -->
<bean id="demoService" class="com.cn.provider.impl.DemoServiceImpl" /> <!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="provider" /> <!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address="multicast://127.0.0.1:1234" /> --> <!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.cn.provider.DemoService"
ref="demoService"/>
</beans>
package com.cn.provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
private final static Logger logger = LoggerFactory.getLogger(Provider.class);
public static void main(String[] args) throws Exception {
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.start();
logger.info("Provider Context start success");
} catch (Exception e) {
logger.error("Provider Context start error\n"+e.getMessage());
}
synchronized (Provider.class) {
while (true) {
try{
Provider.class.wait();
}catch(InterruptedException e){
logger.error("synchronized error\n"+e.getMessage());
}
}
}
}
}
dubbo-customer项目
因为dubbo-customer需要引入dubbo-provider项目中DemoService的jar包,pom.xml文件内容要加上:
<!-- http://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- http://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.8</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
<!-- <type>pom</type> -->
</dependency>
<dependency>
<groupId>javabuilder</groupId>
<artifactId>javabuilder</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/dubbo-provider.jar</systemPath>
</dependency>
记得把dubbo-provider.jar放到项目WEB-INF/lib下,生成jar包的方法可参考我的博客:http://blog.csdn.net/aqsunkai/article/details/51711580
整个项目我想既可以用main方法启动加载配置文件,也可以作为web项目用tomcat启动,在浏览器中看到结果,那么我一定需要在pom.xml中引入spring的jar包吗,答案是no,我只需要写servlet,直接进入doGet方法即可验证,那么就需要修改web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>servletDemo</servlet-name>
<servlet-class>com.cn.customer.Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletDemo</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
"> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="customer" /> <!-- 使用zookeeper注册中心暴露服务地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
<dubbo:reference id="demoService"
interface="com.cn.provider.DemoService"/> <!-- 目的是用ApplicationContext获取bean,与dubbo项目无关 -->
<bean class="com.cn.customer.AppContext"/>
</beans>
servlet.java文件的内容为:
public class Servlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
//初始化
public void init() throws ServletException {
System.out.println("我是init()方法!用来进行初始化工作");
}
//处理GET请求
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("我是doGet()方法!用来处理GET请求");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<BODY>");
/*
* 通过Spring提供的工具类获取ApplicationContext对象
*/
//ServletContext sc = this.getServletContext(); //和下面一行一样,都能获取ServletContext
ServletContext sc = request.getSession().getServletContext();
//第一种获取bean方法,获取失败时抛出异常
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
DemoService demoService1 = (DemoService)ac1.getBean("demoService");
String name1 = demoService1.getName("tom", "Edison");
out.println(name1);
out.println("<br>");
//第二种获取bean方法,获取失败时返回null
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(sc);
DemoService demoService2 = (DemoService)ac2.getBean("demoService");
String name2 = demoService2.getName("tom", "Edison");
out.println(name2);
out.println("<br>");
//第三种获取bean方法
WebApplicationContext wac = (WebApplicationContext)sc.getAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
DemoService demoService3 = (DemoService)wac.getBean("demoService");
String name3 = demoService3.getName("tom", "Edison");
out.println(name3);
out.println("<br>");
//第四种获取bean方法,实现ApplicationContextAware接口
AppContext aContext = new AppContext();
DemoService demoService4 = (DemoService)aContext.getBean("demoService");
String name4 = demoService4.getName("tom", "Edison");
out.println(name4);
out.println("</BODY>");
out.println("</HTML>");
}
//处理POST请求
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("我是doPost()方法!用来处理POST请求");
doGet(request, response);
}
//销毁实例
public void destroy() {
super.destroy();
System.out.println("我是destroy()方法!用来进行销毁实例的工作");
}
}
public class AppContext implements ApplicationContextAware{
private static ApplicationContext applicationContext;
/**
* 当继承了ApplicationContextAware类之后,那么程序在调用
* getBean(String)的时候会自动调用该方法,不用自己操作
*/
@Override
public void setApplicationContext(
org.springframework.context.ApplicationContext applicationContext)
throws BeansException {
this.applicationContext= applicationContext;
}
public Object getBean(String beanName){
return this.applicationContext.getBean(beanName);
}
}
Customer.java内容为:
public class Customer{
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
context.start();
DemoService demoService = (DemoService) context.getBean("demoService");
String name = demoService.getName("tom", "Edison");
System.out.println(name);
System.in.read();
}
}
启动项目
dubbo本地搭建实例的更多相关文章
- DUBBO本地搭建及小案例
DUBBO的介绍部分我这里就不介绍了,大家可参考官方文档. DUBBO的注册中心安装 DUBBO的注册中心支持好几种,公司用到zookeeper注册中心,所以我这边只说明zookeeper注册中心如何 ...
- DUBBO本地搭建及小案例 (转)
DUBBO的介绍部分我这里就不介绍了,大家可参考官方文档. DUBBO的注册中心安装 DUBBO的注册中心支持好几种,公司用到zookeeper注册中心,所以我这边只说明zookeeper注册中心如何 ...
- java 学习笔记(三)ZooKeeper集群搭建实例,以及集成dubbo时的配置 (转)
ZooKeeper集群搭建实例,以及集成dubbo时的配置 zookeeper是什么: Zookeeper,一种分布式应用的协作服务,是Google的Chubby一个开源的实现,是Hadoop的分布式 ...
- 【2020-03-21】Dubbo本地环境搭建-实现服务注册和消费
前言 本周主题:加班工作.本周内忙于CRUD不能自拔,基本每天都是九点半下班,下周上线,明天还要加班推进进度.今天是休息日,于是重拾起了dubbo,打算近期深入了解一下其使用和原理.之所以说是重拾,是 ...
- 超快速使用docker在本地搭建hadoop分布式集群
超快速使用docker在本地搭建hadoop分布式集群 超快速使用docker在本地搭建hadoop分布式集群 学习hadoop集群环境搭建是hadoop入门的必经之路.搭建分布式集群通常有两个办法: ...
- Spring boot dubbo+zookeeper 搭建------基于gradle项目的消费端与服务端分离实战
1. Dubbo简介 Dubbo是Alibaba开源的分布式框架,是RPC模式的一种成熟的框架,优点是可以与Spring无缝集成,应用到我们的后台程序中.具体介绍可以查看Dubbo官网. 2. Why ...
- Dubbo本地存根是什么,Dubbo本地伪装又是什么?
真正的大师永远怀着一颗学徒的心 哈喽!大家好,我是小奇,一位程序员界的学徒 小奇打算以轻松幽默的对话方式来分享一些技术,如果你觉得通过小奇的文章学到了东西,那就给小奇一个赞吧 前言 书接上回,昨天打了 ...
- Hibernate框架搭建实例
一,Hibernate是一个持久层,是一个专门负责管理数据库连接的框架: 二,Hibernate的搭建实例: 1.在Hibernate的官方网站(http://www.hibernate.org)可以 ...
- nodejs,node原生服务器搭建实例
nodejs,node原生服务器搭建实例
随机推荐
- jsp:jsp包含文件的两种方式
第一种:include指令 include指令:当JSP转换成Servlet时引入指定文件(指令元素),这是一种静态包含,它运行的时候不会单独编译成.class文件,它生成一个新的整体.class文件 ...
- Tomcat配置文件server.xml分析
本文力求,分析清楚 tomcat 的 server.xml 文件,逐步完善更新 常用来,配置tomcat启动,端口号:配置编码等. apache-tomcat-9.0.10/conf/server.x ...
- shell脚本学习(2)比较两个数字大小
注意:shell中对比字符串只能使用==.<.>.!=.-z.-n.对比字符串时,末尾一定要加上x(或者a.b等)一个字符,因为if [ $1x == "ab"x ]时 ...
- Sublime Text3 + Markdown + 实时预览
Sublime Text3是一款给力的文本编辑器,通过安装插件可以编辑Markdown文本,在编辑Markdown文本的同时可以实时预览编辑效果. 安装准备: 找到菜单栏:Preferences → ...
- POJ-2002 Squares---绕点旋转+Hash
题目链接: https://vjudge.net/problem/POJ-2002 题目大意: 有一堆平面散点集,任取四个点,求能组成正方形的不同组合方式有多少. 相同的四个点,不同顺序构成的正方形视 ...
- 既然红黑树那么好,为啥hashmap不直接采用红黑树,而是当大于8个的时候才转换红黑树?
因为红黑树需要进行左旋,右旋操作, 而单链表不需要,以下都是单链表与红黑树结构对比.如果元素小于8个,查询成本高,新增成本低如果元素大于8个,查询成本低,新增成本高 https://bbs.csdn. ...
- npy数据的保存与读取
保存 利用这种方法,保存文件的后缀名字一定会被置为.npy x = numpy.save("data_x.npy",x) 读取 data = numpy.load("da ...
- 第33章 TIM—电容按键检测—零死角玩转STM32-F429系列
第33章 TIM—电容按键检测 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fir ...
- powerdesigner15 反向工程
- Apache 负载均衡 端口转发 配置
转载自:https://blog.csdn.net/snihcel/article/details/38844323 [端口转发配置] 通过http_proxy做tomcat的端口转发: ...