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原生服务器搭建实例
随机推荐
- iBrand 教程:Git 软件安装过程截图
下载 教程中使用的相关软件下载网盘: https://pan.baidu.com/s/1bqVD5MJ 密码:4lku 安装 请右键 以管理员身份运行 进行软件安装,安装过程如下: 使用 安装完成后, ...
- 【js基础修炼之路】- 手把手教你实现bind
手写bind前我们先回顾一下bind有哪些特性,以便更好的理解bind和实现bind. bind的特性 var obj = { a: 100, say(one, two) { console.log( ...
- 2018.9.8pat秋季甲级考试
第一次参加pat考试,结果很惨,只做了中间两道题,还有一个测试点错误,所以最终只得了不到50分.题目是甲级练习题的1148-1151. 考试时有些紧张,第一题第二题开始测试样例都运行不正确,但是调试程 ...
- Responsive设计 (响应式设计)
一.什么是响应式设计 维基百科是这样对响应式作的描述:“Responsive设计简单的称为RWD,是精心提供各种设备都能浏览网页的一种设计方法,RWD能让你的网页在不同的设备中展现不同的设计风格.” ...
- Java 截屏工具类
PrintScreenUtils.java package javax.utils; import java.awt.AWTException; import java.awt.Dimension; ...
- 奇异值分解(SVD)和最小二乘解在解齐次线性超定方程中的应用
奇异值分解,是在A不为方阵时的对特征值分解的一种拓展.奇异值和特征值的重要意义相似,都是为了提取出矩阵的主要特征. 对于齐次线性方程 A*X =0;当A的秩大于列数时,就需要求解最小二乘解,在||X| ...
- 动画利器animate.css
使用过CSS3编写动画的同学一定感叹CSS3的强大,但是也会感到书写的麻烦.每次都要计算动画的各个参数,十分麻烦.有没有一个库能封装一些常用的CSS3动画效果.答案是肯定的,animate.css就是 ...
- 微信端H5页面问题总结
1.div元素不确定高度的情况下背景图片显示问题,解决后可以自适应不同分辨率的屏幕大小,div元素的background-size设置100%后,其自身的高度和宽度不能再设置. .register-t ...
- React后台管理系统-订单管理
1.订单管理页面和商品管理页面类似,都是一个搜索组件+列表组件 2.搜索框search组件 import React from 'react'; class ListSearch extends ...
- hdu_1573_X问题 (分段之中国剩余
求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], …, X mod a[i] = b[i], … ...