關(guān)于dubbox的介紹可以自行百度党远。
官網(wǎng)地址:https://github.com/dangdangdotcom/dubbox
Dubbox需要什么版本的JDK削解?
目前最好在JDK 1.7以上運(yùn)行
Dubbo REST的服務(wù)能和Dubbo注冊(cè)中心富弦、監(jiān)控中心集成嗎?
可以的氛驮,而且是自動(dòng)集成的腕柜,也就是你在dubbo中開發(fā)的所有REST服務(wù)都會(huì)自動(dòng)注冊(cè)到服務(wù)冊(cè)中心和監(jiān)控中心,可以通過它們做管理矫废。
但是盏缤,只有當(dāng)REST的消費(fèi)端也是基于dubbo的時(shí)候,注冊(cè)中心中的許多服務(wù)治理操作才能完全起作用蓖扑。而如果消費(fèi)端是非dubbo的唉铜,自然不受注冊(cè)中心管理,所以其中很多操作是不會(huì)對(duì)消費(fèi)端起作用的律杠。
不廢話潭流,代碼可以說明一切
可以參考官方的文檔?http://dangdangdotcom.github.io/dubbox/rest.html
一、先從git上面下載項(xiàng)目柜去,然后在項(xiàng)目根目錄執(zhí)行mvn install灰嫉,執(zhí)行完成之后將下圖中紅色框里面的war包放tomcat里面運(yùn)行。
二嗓奢、創(chuàng)建如下項(xiàng)目
項(xiàng)目程序結(jié)構(gòu)如圖:
1讼撒、pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
? <modelVersion>4.0.0</modelVersion>
? <groupId>hvgroup</groupId>
? <artifactId>com</artifactId>
? <version>0.0.1-SNAPSHOT</version>
? <packaging>war</packaging>
? <name>dbxtest</name>
? <dependencies>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.alibaba</groupId>
? ? ? ? ? ? <artifactId>dubbo-demo-api</artifactId>
? ? ? ? ? ? <version>2.8.4</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.alibaba</groupId>
? ? ? ? ? ? <artifactId>dubbo</artifactId>
? ? ? ? ? ? <version>2.8.4</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.javassist</groupId>
? ? ? ? ? ? <artifactId>javassist</artifactId>
? ? ? ? ? ? <version>3.20.0-GA</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>io.netty</groupId>
? ? ? ? ? ? <artifactId>netty</artifactId>
? ? ? ? ? ? <version>3.7.0.Final</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.mina</groupId>
? ? ? ? ? ? <artifactId>mina-core</artifactId>
? ? ? ? ? ? <version>1.1.7</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.glassfish.grizzly</groupId>
? ? ? ? ? ? <artifactId>grizzly-core</artifactId>
? ? ? ? ? ? <version>2.1.4</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.httpcomponents</groupId>
? ? ? ? ? ? <artifactId>httpclient</artifactId>
? ? ? ? ? ? <version>4.2.1</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.alibaba</groupId>
? ? ? ? ? ? <artifactId>fastjson</artifactId>
? ? ? ? ? ? <version>1.1.39</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.thoughtworks.xstream</groupId>
? ? ? ? ? ? <artifactId>xstream</artifactId>
? ? ? ? ? ? <version>1.4.1</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.bsf</groupId>
? ? ? ? ? ? <artifactId>bsf-api</artifactId>
? ? ? ? ? ? <version>3.1</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.zookeeper</groupId>
? ? ? ? ? ? <artifactId>zookeeper</artifactId>
? ? ? ? ? ? <version>3.4.6</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.github.sgroschupf</groupId>
? ? ? ? ? ? <artifactId>zkclient</artifactId>
? ? ? ? ? ? <version>0.1</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.curator</groupId>
? ? ? ? ? ? <artifactId>curator-framework</artifactId>
? ? ? ? ? ? <version>2.5.0</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.googlecode.xmemcached</groupId>
? ? ? ? ? ? <artifactId>xmemcached</artifactId>
? ? ? ? ? ? <version>1.3.6</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.cxf</groupId>
? ? ? ? ? ? <artifactId>cxf-rt-frontend-simple</artifactId>
? ? ? ? ? ? <version>2.6.1</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.cxf</groupId>
? ? ? ? ? ? <artifactId>cxf-rt-transports-http</artifactId>
? ? ? ? ? ? <version>2.6.1</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.thrift</groupId>
? ? ? ? ? ? <artifactId>libthrift</artifactId>
? ? ? ? ? ? <version>0.8.0</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.caucho</groupId>
? ? ? ? ? ? <artifactId>hessian</artifactId>
? ? ? ? ? ? <version>4.0.7</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>javax.servlet</groupId>
? ? ? ? ? ? <artifactId>javax.servlet-api</artifactId>
? ? ? ? ? ? <version>3.1.0</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.mortbay.jetty</groupId>
? ? ? ? ? ? <artifactId>jetty</artifactId>
? ? ? ? ? ? <exclusions>
? ? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? ? <groupId>org.mortbay.jetty</groupId>
? ? ? ? ? ? ? ? ? ? <artifactId>servlet-api</artifactId>
? ? ? ? ? ? ? ? </exclusion>
? ? ? ? ? ? </exclusions>
? ? ? ? ? ? <version>6.1.26</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>log4j</groupId>
? ? ? ? ? ? <artifactId>log4j</artifactId>
? ? ? ? ? ? <version>1.2.16</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.slf4j</groupId>
? ? ? ? ? ? <artifactId>slf4j-api</artifactId>
? ? ? ? ? ? <version>1.6.2</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>redis.clients</groupId>
? ? ? ? ? ? <artifactId>jedis</artifactId>
? ? ? ? ? ? <version>2.1.0</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>javax.validation</groupId>
? ? ? ? ? ? <artifactId>validation-api</artifactId>
? ? ? ? ? ? <version>1.0.0.GA</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.hibernate</groupId>
? ? ? ? ? ? <artifactId>hibernate-validator</artifactId>
? ? ? ? ? ? <version>4.2.0.Final</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>javax.cache</groupId>
? ? ? ? ? ? <artifactId>cache-api</artifactId>
? ? ? ? ? ? <version>0.4</version>
? ? ? ? </dependency>
? ? ? ? <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.apache.tomcat.embed</groupId>
? ? ? ? ? ? <artifactId>tomcat-embed-core</artifactId>
? ? ? ? ? ? <version>8.0.11</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.tomcat.embed</groupId>
? ? ? ? ? ? <artifactId>tomcat-embed-logging-juli</artifactId>
? ? ? ? ? ? <version>8.0.11</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.esotericsoftware.kryo</groupId>
? ? ? ? ? ? <artifactId>kryo</artifactId>
? ? ? ? ? ? <version>2.24.0</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>de.javakaffee</groupId>
? ? ? ? ? ? <artifactId>kryo-serializers</artifactId>
? ? ? ? ? ? <version>0.26</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>de.ruedigermoeller</groupId>
? ? ? ? ? ? <artifactId>fst</artifactId>
? ? ? ? ? ? <version>1.55</version>
? ? ? ? </dependency>
? ? </dependencies>
? <build>
? ? <plugins>
? ? ? <plugin>
? ? ? ? <artifactId>maven-compiler-plugin</artifactId>
? ? ? ? <configuration>
? ? ? ? ? <source>1.6</source>
? ? ? ? ? <target>1.6</target>
? ? ? ? </configuration>
? ? ? </plugin>
? ? </plugins>
? </build>
</project>
2、User.java
package com.hvgroup.user;
import java.io.Serializable;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.codehaus.jackson.annotate.JsonProperty;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class User implements Serializable {
? ? @NotNull
? ? @Min(1L)
? ? private Long id;
? ? @JsonProperty("username")
? ? @XmlElement(name = "username")
? ? @NotNull
? ? @Size(min = 6, max = 50)
? ? private String name;
? ? public User() {
? ? }
? ? public User(Long id, String name) {
? ? ? ? this.id = id;
? ? ? ? this.name = name;
? ? }
? ? public Long getId() {
? ? ? ? return id;
? ? }
? ? public void setId(Long id) {
? ? ? ? this.id = id;
? ? }
? ? public String getName() {
? ? ? ? return name;
? ? }
? ? public void setName(String name) {
? ? ? ? this.name = name;
? ? }
? ? @Override
? ? public String toString() {
? ? ? ? return "User (" +
? ? ? ? ? ? ? ? "id=" + id +
? ? ? ? ? ? ? ? ", name='" + name + '\'' +
? ? ? ? ? ? ? ? ')';
? ? }
}
3股耽、UserService.java
package com.hvgroup.user;
public interface? UserService {? ?
? void registerUser(User user);
? User getUser(Long id);
}
4根盒、UserServiceImpl.java
package com.hvgroup.user;
public class UserServiceImpl implements UserService {
? ? public void registerUser(User user) {
? ? ? ? // save the user...
? ? }
? ? public User getUser(Long id) {
? ? ? ? return new User(id, "username" + id);
? ? }
}
5、UserServiceWebImpl.java
package com.hvgroup.user;
import com.alibaba.dubbo.rpc.RpcContext;
import com.hvgroup.user.User;
import com.hvgroup.user.UserService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Path("users")
@Consumes({"application/json", "text/xml"})
@Produces({"application/json; charset=UTF-8", "text/xml; charset=UTF-8"})
public class UserServiceWebImpl implements UserService {
? ? private static final Logger logger = LoggerFactory.getLogger(UserServiceWebImpl.class);
? ? private UserService userService;
? ? public void setUserService(UserService userService) {
? ? ? ? this.userService = userService;
? ? }
? ? @GET
? ? @Path("{id : \\d+}")
? ? public User getUser(@PathParam("id") Long id)
? ? {
? ? ? if (RpcContext.getContext().getRequest(HttpServletRequest.class) != null) {
? ? ? ? System.out.println("Client IP address from RpcContext: " + ((HttpServletRequest)RpcContext.getContext().getRequest(HttpServletRequest.class)).getRemoteAddr());
? ? ? }
? ? ? if (RpcContext.getContext().getResponse(HttpServletResponse.class) != null) {
? ? ? ? System.out.println("Response object from RpcContext: " + RpcContext.getContext().getResponse(HttpServletResponse.class));
? ? ? }
? ? ? return this.userService.getUser(id);
? ? }
? ? @POST
? ? @Path("register")
? ? public void registerUser(User user) {
? ? }
}
6物蝙、dubbo-demo-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-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
? ? <dubbo:application name="dubboxtest" owner="programmer" organization="dubbox"/>
? ? <dubbo:registry address="zookeeper://192.168.89.135:2181"/>
? ? <!-- 用rest協(xié)議在8080端口暴露服務(wù) -->
<dubbo:protocol name="rest" port="8888" threads="500"? server="tomcat" accepts="500"
? ? ? ? ? ? ? ? ? ? extension="com.alibaba.dubbo.demo.extension.TraceInterceptor,
? ? ? ? ? ? ? ? ? ? com.alibaba.dubbo.demo.extension.TraceFilter,
? ? ? ? ? ? ? ? ? ? com.alibaba.dubbo.demo.extension.ClientTraceFilter,
? ? ? ? ? ? ? ? ? ? com.alibaba.dubbo.demo.extension.DynamicTraceBinding,
? ? ? ? ? ? ? ? ? ? com.alibaba.dubbo.demo.extension.CustomExceptionMapper,
? ? ? ? ? ? ? ? ? ? com.alibaba.dubbo.rpc.protocol.rest.support.LoggingFilter"/>
<!-- <dubbo:protocol name="rest" port="8888" contextpath="dbxtest" server="servlet" />? -->
<!-- 聲明需要暴露的服務(wù)接口 -->
<dubbo:service interface="com.hvgroup.user.UserWebService" ref="userWebService" protocol="rest" validation="true"/>
<!-- <dubbo:service interface="com.hvgroup.user.UserWebService" ref="userWebService"/> -->
<!-- 和本地bean一樣實(shí)現(xiàn)服務(wù) -->
<bean id="userService" class="com.hvgroup.user.UserServiceImpl" />
<bean id="userWebService" class="com.hvgroup.user.UserServiceWebImpl">
? ? ? ? <property name="userService" ref="userService"/>
? ? </bean>
</beans>
7郑象、log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="CONSOLE" />
</root>
</log4j:configuration>
8、web.xml
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
? ? <context-param>
? ? ? ? <param-name>contextConfigLocation</param-name>
? ? ? ? <param-value>/WEB-INF/classes/dubbo-demo-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>
三茬末、將新創(chuàng)建的項(xiàng)目部署到tomcat里面運(yùn)行起來厂榛。
四、在瀏覽器上面?http://localhost:8888/users/1001
更多內(nèi)容請(qǐng)關(guān)注我丽惭,會(huì)第一時(shí)間持續(xù)進(jìn)行更新击奶。