dubbox服务提供者是REST风格的,消费者可能是从dubbox过来的,也可能是从第三方外部系统过来的。后者的话我们没得选,只能以服务提供者直连,服务治理平台和监控中心手再长,也管不到别人的地盘。但如果是dubbox自己作为消费端,那么就可以很容易配置各种服务治理参数了,也能被监控中心实时盯着。有利必有弊,本来dubbox提供REST风格的http调用就是为了方便异构客户端到服务提供者来交流的,为了纳入dubbo系统再套一层消费者岂不是脱裤子放屁,多此一举?多了一个人传话,对话就多了一层转换,多了一份丢话和失真的危险。

  消费端用spring的REST注解是最简单的,但因为服务端已经用dubbox的RESTEasy框架了,所以消费端还得重新用spring自己的REST实现一遍。这里为了偷懒,消费端还是用RESTEasy来搞,直接把服务端用到的公共接口复制到消费端作为控制类即可。首先我们用JAX-RS定义接口和bean,放到公用API包里,然后服务端实现这些API,消费端新增一个controller类来调用API,最后在web配置文件里指定使用RESTEasy作为servlet容器。

1、公用接口,使用JAX-RS注解,暴露服务端所提供的服务


import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import com.wlf.api.bean.Page;


@Path("/hello/world")
@Produces({MediaType.APPLICATION_JSON})
public interface MobileZoneResourceService
{
      
    @GET
    @Path("/nihao.jsp")
    public Page queryToday(@QueryParam("param") String param);
   
}


公用bean,使用JAX-RS注解

import java.io.Serializable;
import java.util.List;
import java.util.Map; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement
public class Page implements Serializable
{ private static final long serialVersionUID = -680237712312747858L; private Integer start; private Integer pageSize; private Integer totalSize; public Integer getStart()
{
return start;
} public void setStart(Integer start)
{
this.start = start;
} public Integer getPageSize()
{
return pageSize;
} public void setPageSize(Integer pageSize)
{
this.pageSize = pageSize;
} public Integer getTotalSize()
{
return totalSize;
} public void setTotalSize(Integer totalSize)
{
this.totalSize = totalSize;
}
}

2、服务提供者service类,注入DAO接口

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired; import com.inspur.zoneresource.api.MobileZoneResourceService;
import com.inspur.zoneresource.api.bean.Page;
import com.inspur.zoneresource.provider.bean.BpmUserInfo;
import com.inspur.zoneresource.provider.dao.ZoneResourceService; public class MobileZoneResourceServiceImpl implements MobileZoneResourceService
{
@Autowired
private ZoneResourceService zoneResourceService; public Page queryToday(String param)
{
BpmUserInfo userinfo = null;
ObjectMapper om = new ObjectMapper();
try
{
userinfo = om.readValue(param, BpmUserInfo.class);
}
catch (Exception e)
{
e.printStackTrace();
} Page page = zoneResourceService.queryToday(userinfo);
return page;
}
}

DAO层接口

import com.inspur.zoneresource.api.bean.Page;
import com.inspur.zoneresource.provider.bean.BpmUserInfo; public interface ZoneResourceService { public Page queryToday(BpmUserInfo userinfo);
}

DAO层实现类就略过了

3、服务消费者控制类,提供http接口给外部调用,注入公共API

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import org.springframework.beans.factory.annotation.Autowired; import com.inspur.zoneresource.api.MobileZoneResourceService;
import com.inspur.zoneresource.api.bean.Page; @Path("/hello/world")
@Produces({MediaType.APPLICATION_JSON})
public class MobileZoneResourceController
{
@Autowired
private MobileZoneResourceService mobileZoneResourceService; @GET
@Path("/nihao.jsp")
public Page queryToday(@QueryParam("param") String param)
{
return mobileZoneResourceService.queryToday(param);
}
}

4、web配置

消费者web.xml,使用resteasy作为web容器分发控制器

<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:META-INF/spring/consumer.xml</param-value>
</context-param> <listener>
<listener-class>com.alibaba.dubbo.remoting.http.servlet.BootstrapListener
</listener-class>
</listener> <listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
  
<listener>
<listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener
</listener-class>
</listener>
 
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

提供者web.xml,使用dubbo作为web容器分发控制器

<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:META-INF/spring/provider.xml</param-value>
</context-param> <listener>
<listener-class>com.alibaba.dubbo.remoting.http.servlet.BootstrapListener
</listener-class>
</listener> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> <servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

5、dubbo配置

消费者,通过把控制器配置为bean来加载

<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config /> <!-- 消费方应用信息,用于计算依赖关系 -->
<dubbo:application name="zr_consumer" /> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 消费者接口 -->
<dubbo:reference id="mobileZoneResourceService"
interface="com.inspur.zoneresource.api.MobileZoneResourceService" /> <!-- 对外control -->
<bean id="mobileZoneResourceController"
class="com.inspur.zoneresource.consumer.control.MobileZoneResourceController" /> </beans>

提供者

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config /> <!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="zr_provider" /> <context:property-placeholder location="classpath:global.properties" /> <!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="${zk.servers}" /> <!-- 采用dubbox提供的REST风格服务(基于resteasy) -->
<dubbo:protocol name="rest" server="servlet" port="${rest.server.port}" /> <!-- timeout远程服务调用超时时间(毫秒),适用所有服务 -->
<!-- threads该服务线程池大小 ,适用所有服务 -->
<!-- accepts限制服务器端接受的连接不能超过个数,适用所有服务 -->
<dubbo:provider timeout="${timeout}" threads="${threads}"
accepts="${accepts}" /> <!-- 暴露服务接口 一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心 -->
<dubbo:service interface="com.inspur.zoneresource.api.MobileZoneResourceService"
ref="mobileZoneResourceService" /> <!-- 和本地bean一样实现服务 -->
<bean id="mobileZoneResourceService"
class="com.inspur.zoneresource.provider.service.MobileZoneResourceServiceImpl" /> <bean id="zoneResourceService"
class="com.inspur.zoneresource.provider.dao.ZoneResourceServiceImpl" /> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver.class}" />
<property name="url" value="${driver.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean> <bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
</beans>

global.properties

zk.servers=zookeeper://127.0.0.1:2181
rest.server.port=8098 timeout=5000
threads=100
accepts=1000 driver.class=oracle.jdbc.driver.OracleDriver
driver.url=jdbc:oracle:thin:@10.211.95.152:1521:testdba
db.username=ues
db.password=ues2017

  

  消费者使用RESTEasy框架需要的maven依赖:

        <dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.7.Final</version>
</dependency> <dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.7.Final</version>
</dependency> <dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-netty</artifactId>
<version>3.0.7.Final</version>
</dependency> <dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jdk-http</artifactId>
<version>3.0.7.Final</version>
</dependency> <dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.0.7.Final</version>
</dependency> <dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>3.0.7.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-spring</artifactId>
<version>3.0.7.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>3.0.7.Final</version>
</dependency>

  

使用resteasy作为dubbox消费者的更多相关文章

  1. dubbox消费者启动成功,却无法连接注册中心

    使用dubbox作为服务提供端很好实现,因为git的说明和网上有很多的例子可供参考,但是消费端都一笔带过,简单得很,初学者往往以为只要配置如下3样东西就够了: <?xml version=&qu ...

  2. dubbox2.8.4例子教程二

    简介 上篇博客写了个dubbox生产者,也用HttpClient代码测试了rest服务,下面记录dubbox消费者工程  一.工程结构    一.Simple.java package bhz.ent ...

  3. dubbox系列【三】——简单的dubbox提供者+消费者示例

    1.dubbox-provider示例 在eclipse中建立maven project,名为provider-parent,包含两个maven medule:provider-api 和 provi ...

  4. dubbox生产者与消费者案例

    一.首先要将dubbox添加到本地maven仓库     参考: https://blog.csdn.net/try_and_do/article/details/83383861     二.目录结 ...

  5. dubbo rest服务(消费者) java.lang.ClassNotFoundException: org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine 错误问题

    1.版本 dubbo 2.7.3 2.描述 java.lang.ClassNotFoundException: org.jboss.resteasy.client.jaxrs.engines.Apac ...

  6. 分布式服务框架 dubbo/dubbox 入门示例

    dubbo是一个分布式的服务架构,可直接用于生产环境作为SOA服务框架. 官网首页:http://dubbo.io/ ,官方用户指南 http://dubbo.io/User+Guide-zh.htm ...

  7. 分布式服务框架 dubbo/dubbox 入门示例(转)

    dubbo是一个分布式的服务架构,可直接用于生产环境作为SOA服务框架. 官网首页:http://dubbo.io/ ,官方用户指南 http://dubbo.io/User+Guide-zh.htm ...

  8. 使用Dubbox构架分布式服务

    第一部分:Dubbo的背景分析及工作原理 1. Dubbo是什么?Dubbo是一个来自阿里巴巴的开源分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. 简单的说 ...

  9. spring boot2整合dubbox全注解

    前题 dubbox是dubbo的一个升级版,简单说就是本来dubbo是阿里开发的,现在阿里不维护了,当当网拿过去继续开发.本来阿里的dubbo维护到2.6版本,而再高版本的都是当当网维护的就叫成dub ...

随机推荐

  1. spring security使用hibernate进行查询数据库验证

    前面查询数据库采用的都是jdbc方式,如果系统使用的是hibernate,该如何进行呢,下面就是实现步骤,关键还是实现自定义的UserDetailsService 项目结构如下: 使用hibernat ...

  2. Floyd算法 - 最短路径

    2017-07-27 22:21:04 writer:pprp 该算法的本质是动态规划,形式简单,复杂度高为O(n^3): d[i][j] = max(d[i][k]+d[k][j],d[i][j]) ...

  3. PermutationSequence,求第k个全排列

    问题描述:给定一个数组,数组里面元素不重复,求第k个全排列. 算法分析:这道题就是用到取商取模运算. public String getPermutation(int n, int k) { // i ...

  4. 在mybatis中使用存储过程报错java.sql.SQLException: ORA-06550: 第 1 行, 第 7 列: PLS-00905: 对象 USER1.HELLO_TEST 无效 ORA-06550: 第 1 行, 第 7 列:

    hello_test是我的存储过程的名字,在mapper.xml文件中是这么写的 <select id="getPageByProcedure" statementType= ...

  5. D3.js学习笔记(一)——DOM上的数据绑定

    开始学习D3.js,网上没有找到很满意的中文教程,但是发现了一个很好的英文教程,讲解的非常详细.从一个初始简单的HTML网页开始,逐步加入D3.js的应用,几乎是逐句讲解.学习的时候,就顺便翻译成中文 ...

  6. Coundn't load memtrack module (No such file or directory)

    Coundn't load memtrack module (No such file or directory) 去仔细看日志,是包名有问题 一.出现症状 提示找logcat logcat里面发现C ...

  7. java-给微信推送消息 利用企业微信

    目的:给关注用户推送消息 场景:自动化测试,运维监控,接口访问等报错预警.例如线上接口报错,发送提醒消息 准备工作: 1:注册企业号(为什么不用公众号呢?) 企业号注册 2:常用参数介绍: 1:COR ...

  8. python中的set类型

    一. 定义 set是一个无序且不重复的元素集合 set和dict类似,是一组key的集合,但不存储value set有以下特性: 1. 由于key不能重复,所有set中没有重复的key 2. 元素为不 ...

  9. html 压缩成一行

    文章java gulp-htmlmin 减少下载的时间

  10. 【SQL查询】模糊查询_like

    [格式]:SELECT 字段 FROM 表 WHERE 某字段 Like 条件 [说明]: 1. %表示任意0个或多个字符,可匹配任意类型和长度的字符. 2. _ 表示任意单个字符.匹配单个任意字符. ...