spring集成dubbo

现在将系统按照服务划分成多个部分。代码功能分为前端和后台服务已经很流行。下面我将介绍基于maven项目结构的spring集成dubbo功能。

1、首先找zookeeper网站下载zookeeper-3.4.6.tar.gz包,将其放置到服务器上。

2、解压zookeeper包 ,安装。并将/confg/zoo.sample.cfg更名成zoo.cfg

3、修改配置文件内容,如图

4、在spring服务器端导入需要的dubbo包和zookeeper包:

 <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>dubbo</artifactId>
   <version>${dubbo.version}</version>
   <exclusions>
      <exclusion>
         <groupId>org.springframework</groupId>
         <artifactId>spring</artifactId>
      </exclusion>
   </exclusions>
</dependency>
<dependency>
   <groupId>org.apache.zookeeper</groupId>
   <artifactId>zookeeper</artifactId>
   <version>3.4.6</version>
</dependency>

5、客户端也要导入所需要的包:

  <dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>

6、服务器端提服务接口和服务实现

package com.ucf.dubbo;

public interface DemoService {
    String sayHello(String name);
}
package com.ucf.dubbo;

import org.springframework.stereotype.Component;

@Component
public class DemoServiceImpl implements DemoService {
@Override
    public String sayHello(String name) {
return "Hello Dubbo,Hello " + name;
    }

}

7、编写dubbo配置文件  dubbo-provider.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/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="hello-world-app"  />

<!-- 使用multicast广播注册中心暴露服务地址 -->       <!--  <dubbo:registry address="multicast://224.5.6.7:1234" /> -->

      <!-- 使用zookeeper注册中心暴露服务地址 -->       <dubbo:registry address="zookeeper://192.168.1.224:2181" />

<!-- 用dubbo协议在20880端口暴露服务 -->       <dubbo:protocol name="dubbo" port="20880" />

<!-- 声明需要暴露的服务接口 -->       <dubbo:service interface="com.ucf.dubbo.DemoService" ref="demoService" />

<!-- 和本地bean一样实现服务 -->       <bean id="demoService" class="com.ucf.dubbo.DemoServiceImpl" /></beans>

8、客户端编写接口(注意这个接口一定要和服务提供方接口的路径一致)

package com.ucf.dubbo;

import org.springframework.stereotype.Component;

/**
 * Created by user on 2015/11/3.
 */
@Component
public interface DemoService {
    String sayHello(String name);
}

9、客户端的配置文件如下:dubbo-customer.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/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="consumer-of-helloworld-app"/>       <!-- 使用multicast广播注册中心暴露发现服务地址 -->       <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->       <dubbo:registry address="zookeeper://192.168.1.224:2181" />       <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->       <dubbo:reference id="demoService" interface="com.ucf.dubbo.DemoService" /></beans>

9、服务器端将配置文件放置到spring上下文中

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:util="http://www.springframework.org/schema/util"     xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:aop="http://www.springframework.org/schema/aop"   xmlns:context="http://www.springframework.org/schema/context"   xmlns:jdbc="http://www.springframework.org/schema/jdbc"    xmlns:tx="http://www.springframework.org/schema/tx"

  xmlns:task="http://www.springframework.org/schema/task"   xsi:schemaLocation="       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"   default-lazy-init="true">

<context:property-placeholder location="classpath:/META-INF/app.properties" />   <util:properties id="appSettings" location="classpath:/META-INF/app.properties" />   <context:component-scan base-package="com.ucf,com.mybatis.sys.dto" />

<import resource="classpath:ucf-jdbc.xml"/>   <import resource="classpath:applicationContext-aop.xml" />   <import resource="classpath:radis.xml"/>   <import resource="dubbo-provider.xml"/>

</beans>

10、客户端将其将配置文件放置到spring上下文中

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:util="http://www.springframework.org/schema/util"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:jdbc="http://www.springframework.org/schema/jdbc"       xmlns:tx="http://www.springframework.org/schema/tx"

      xmlns:task="http://www.springframework.org/schema/task"       xsi:schemaLocation="       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"       default-lazy-init="true">       <context:property-placeholder location="/META-INF/app.properties" />       <util:properties id="appSettings" location="/META-INF/app.properties" />

<import resource="dubbo-customer.xml"/>

</beans>

11、写个demo启动测试

hdb.dubbo;

com.ucf.dubbo.DemoService;
org.springframework.context.support.ClassPathXmlApplicationContext;

Consumer {
    main([] args) {
        ClassPathXmlApplicationContext context = ClassPathXmlApplicationContext();
        context.start();
        DemoService demoService = (DemoService)context.getBean();  hello = demoService.sayHello(); System..println(hello);
    }
}

结果如图:

时间: 2024-08-14 22:26:50

spring集成dubbo的相关文章

Spring 集成 Dubbo

Duboo是什么 DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点.可以看出在阿里内部广泛应用,类似的还有Spring Cloud. 准备工作 准备两虚机,我这里用了CentOS7.2,加上本机可组成多提供者和消费者(当然一个虚机和不用虚机也可以)我准备的两台IP为:192.168.124.129(用于 dubbo

集成Dubbo服务(Spring)

Dubbo是什么? Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点. Dubbo[]是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. 其核心部分包含: 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式. 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以

深入Spring Boot:快速集成Dubbo + Hystrix

背景Hystrix 旨在通过控制那些访问远程系统.服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力.Hystrix具备拥有回退机制和断路器功能的线程和信号隔离,请求缓存和请求打包,以及监控和配置等功能.Dubbo是Alibaba开源的,目前国内最流行的java rpc框架.本文介绍在spring应用里,怎么把Dubbo和Hystrix结合起来使用.Spring Boot应用生成dubbo集成spring boot的应用配置spring-cloud-starter-netflix-hys

spring boot 集成 dubbo 企业完整版

一.什么是Spring Boot ? 现阶段的 Spring Boot 可谓是太火了,为什么呢?因为使用方便.配置简洁.上手快速,那么它是什么?从官网上我们可以看到,它是 Spring 开源组织下的一个子项目,主要简化了 Spring 繁重的配置,而且 Spring Boot 内嵌了各种 Servlet 容器,如:Tomcat.Jetty 等 官方网站:http://projects.spring.io/spring-boot/ GitHub源码:https://github.com/sprin

RabbitMQ及Spring集成

部分转载自https://blog.csdn.net/whoamiyang/article/details/54954780 1.背景 RabbitMQ是一个由erlang开发的AMQP(Advanved Message Queue)的开源实现. 2.应用场景 2.1异步处理 场景说明:用户注册后,需要发注册邮件和注册短信,传统的做法有两种1.串行的方式;2.并行的方式 (1)串行方式:将注册信息写入数据库后,发送注册邮件,再发送注册短信,以上三个任务全部完成后才返回给客户端. 这有一个问题是,

SpringBoot系列之集成Dubbo的方式

本博客介绍Springboot框架集成Dubbo实现微服务的3种常用方式,对于Dubbo知识不是很熟悉的,请先学习我上一篇博客:SpringBoot系列之集成Dubbo实现微服务教程,本博客只是对上篇博客的补充,上篇博客已经介绍过的就不重复介绍 还是使用上篇博客的例子,业务场景: 某个电商系统,订单服务需要调用用户服务获取某个用户的所有地址: 我们现在 需要创建两个服务模块进行测试 模块 功能 订单服务模块 创建订单等 用户服务模块 查询用户地址等 测试预期结果: 订单服务web模块在A服务器,

RabbitMQ安装和使用(和Spring集成)

一.安装Rabbit MQ Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装Rabbit MQ的前提是安装Erlang.通过下面两个连接下载安装3.2.3 版本: 下载并安装 Eralng OTP For Windows (vR16B03) 运行安装 Rabbit MQ Server Windows Installer (v3.2.3) 具体操作步骤参考:在 Windows 上安装Rabbit MQ 指南 本人遇到的问题 当安装RabbitMQ后,使用rabbitmqctl

spring集成quartz

spring集成quartz 注意:出现异常"Caused by: java.lang.IncompatibleClassChangeError: class org.springframework.scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class" Spring3.0不支持Quartz2.0,因为org.quartz.CronTrigger在2.0从class变成

Hessian入门(包括与Spring集成)

A.纯Hessian接口开发步骤 Hessian-JAVA服务器端必须具备以下几点: * 1.包含Hessian的jar包(hessian-4.0.37.jar) * 2.设计一个接口,用来给客户端调用(IHessian.java) * 3.实现该接口的功能(IHessianImpl.java) * 4.配置web.xml,配好相应的Servlet(web.xml) * 5.对象必须实现Serializable接口(Foo.java) Hessian-JAVA服务端代码预览图: 1.包含Hess