dubbox 在實際項目中的使用

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.Facet
apply plugin:'war'
apply plugin:'eclipse-wtp'

sourceCompatibility=1.8
targetCompatibility=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需要的jar
compile('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.fixed
facet name: 'wst.jsdt.web', type: Facet.FacetType.fixed
facet name: 'jst.java', type: Facet.FacetType.fixed
facet 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=slf4j
dubbo.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=20880
dubbo.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.xsd
http://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指定的格式一樣,否則是調不通的

發佈了36 篇原創文章 · 獲贊 12 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章