dubbox 在实际项目中的使用
来源:互联网 发布:cocos2d.js sdk 编辑:程序博客网 时间:2024/06/07 07:23
dubbox的github地址: https://github.com/dangdangdotcom/dubbox
先下载下来,在pom.xml文件中修改jdk版本
mvn clean package install -Dmaven.test.skip=true
如果报错,查看缺少什么jar包,删除jar包,重新执行上面的命令
dubbox是需要zookeeper的,所以必须要先安装zk。
dubbox可以将服务既发布成dubbo,也可以发布成rest。
在build.gradle中配置,这里只是简单的配了一下
import org.gradle.plugins.ide.eclipse.model.Facetapply plugin:'war'apply plugin:'eclipse-wtp'sourceCompatibility=1.8targetCompatibility=1.8[compileJava,compileTestJava]*.options*.encoding='UTF-8'ext{springVersion='4.3.6.RELEASE'}repositories {maven {url "http://maven.aliyun.com/nexus/content/groups/public/"}maven { url "http://repo.maven.apache.org/maven2" }}dependencies {compile fileTree(dir:'src/main/webapp/WEB-INF/lib',include:'*.jar',exclude:'commons-httpclient-3.0.jar')compile 'org.apache.axis:axis:1.4'compile 'commons-discovery:commons-discovery:0.2'compile 'wsdl4j:wsdl4j:1.6.3'compile "org.springframework:spring-context:${springVersion}"compile "org.springframework:spring-web:${springVersion}"//dubbox需要的jarcompile('com.alibaba:dubbo:2.8.4')compile('com.github.sgroschupf:zkclient:0.1')compile("org.apache.zookeeper:zookeeper:3.4.9")compile('org.jboss.resteasy:resteasy-jaxrs:3.0.7.Final')compile('org.jboss.resteasy:resteasy-client:3.0.7.Final') compile('javax.validation:validation-api:1.0.0.GA')testCompile 'junit:junit:4.12'}eclipse {wtp {facet {facet name: 'jst.web', type: Facet.FacetType.fixedfacet name: 'wst.jsdt.web', type: Facet.FacetType.fixedfacet name: 'jst.java', type: Facet.FacetType.fixedfacet name: 'jst.web', version: '3.0'facet name: 'jst.java', version: '1.8'facet name: 'wst.jsdt.web', version: '1.0'}}}
在src/main/resources下新建dubbo.properties文件(必须是dubbo命名的)
dubbo.application.logger=slf4jdubbo.application.name=maven-demo dubbo.application.owner=qinwei dubbo.registry.address=zookeeper://zk的ip:2181#这个端口号一定要与tomcat的端口号相同dubbo.protocol.rest.port=8080 #dubbo.protocol.rest.threads=2 #dubbo.protocol.rest.accepts=10#这个最好和你的工程名相同,如果比你的工程名短,直接启动报错.dubbo.protocol.rest.contextpath=dubbo-demo#如果是servlet,必须在web.xml中配置dispatchServlet dubbo.protocol.rest.server=servlet dubbo.protocol.dubbo.port=20880dubbo.reference.timeout=10000
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.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><dubbo:service interface="com.dubbo.RestService" ref="restServiceImpl" protocol="rest" /></beans>
dubbo.consumer.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:reference id="restService" interface="com.dubbo.RestService" check="false"/></beans>
web.xml
<!--必须在spring的listener之前 --> <listener> <listener-class>com.alibaba.dubbo.remoting.http.servlet.BootstrapListener</listener-class> </listener> <!-- dubbo.properties中配的是servlet的话,必须要配置下面--> <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>
import javax.ws.rs.Consumes;import javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;@Path("/dubbox")@Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })@Produces({ ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8 })public interface RestService {@POST@Path("/testRest")String testRest(String json);}}
import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Service;@Service("restServiceImpl")public class RestServiceImpl implements RestService { private static final Logger LOGGER = LoggerFactory.getLogger(RestServiceImpl.class); @Override public String testRest(String json) { LOGGER.debug(json); return "{\"name\":\"qw\"}"; }}
调用dubbo服务直接引入对应的API的jar就可以了
调用rest服务需要暴露服务器的ip和端口号,调用地址是
http://IP:配置的dubbo.protocol.rest.port/dubbo.protocol.rest.contextpath/接口上的@path/方法上的@path
如果是非dubbo(比如用httpclient的方式)调用rest服务,必须要注意设置contentType,要和@Consumes指定的格式一样,否则是调不通的
阅读全文
0 0
- dubbox 在实际项目中的使用
- log4j在web项目中的实际使用
- log4j在web项目中的实际使用
- Nhibernate在实际项目中的使用
- boost在实际项目中的使用
- Nodejs, MemCacheD 在实际项目中的使用
- boost在实际项目中的使用
- boost 在实际项目中的使用
- Git 在实际项目中的使用
- SpringAop在实际项目中的使用案例
- log4j在web项目中的实际使用与jboss冲突
- JavaScriptCore在实际项目中的使用的坑
- Eigen 矩阵运算库在实际项目中的使用
- ActiveMQ的在实际项目中的简单使用
- NHibernate在实际项目中的应用
- 设计模式在实际项目中的应用
- AtomicInteger在实际项目中的应用
- AtomicInteger在实际项目中的应用
- Android 自定义实现日历控件
- Python学习笔记——高级特性
- Andorid 自适应UI
- Acm1-输入出and结构体
- python核心编程课后习题-正则式1
- dubbox 在实际项目中的使用
- 在某软的酸甜苦辣
- OutOfMemoryError异常
- 2017_NOIP初赛
- JZOJ5440. 【NOIP2017提高A组冲刺11.1】背包
- Java8函数式编程实践精华
- SVN 服务器与客户端搭建使用
- vue-cli安装步骤
- 关于在github中clone 文件产生的问题