dubbo 2.6.0 之后 支持rest访问格式
支持标准的 JAX-RS 请参阅相关文档
pom.xml 参照官方的jar版本
https://github.com/apache/incubator-dubbo/blob/master/dependencies-bom/pom.xml
<properties>
<resteasy_version>3.0.19.Final</resteasy_version>
<servlet_version>3.1.0</servlet_version>
<jetty_version>6.1.26</jetty_version>
<validation_version>1.1.0.Final</validation_version>
<hibernate_validator_version>5.4.1.Final</hibernate_validator_version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.11</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>${resteasy_version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>${resteasy_version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-netty4</artifactId>
<version>${resteasy_version}</version>
<exclusions>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jdk-http</artifactId>
<version>${resteasy_version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>${resteasy_version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>${resteasy_version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet_version}</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>${jetty_version}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${validation_version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate_validator_version}</version>
</dependency>
</dependencies>
User.java
package com.yiwugou.demo.dubbo.bean;
import java.io.Serializable;
import lombok.Data;
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String username;
private String password;
}
dubbo-demo-provider.xml
<context:annotation-config />
<context:component-scan base-package="com.yiwugou.demo.dubbo" />
<dubbo:application name="dubbo-demo-provider" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:protocol name="dubbo" port="-1" host="127.0.0.1" />
<dubbo:protocol name="rest" port="8080" />
<!-- 代表两个协议都有 dubbo 和 rest,如果只要rest dubbo 协议删掉即可 -->
<dubbo:service interface="com.yiwugou.demo.dubbo.service.UserService" ref="userService" />
UserService.java (JAX-RS 注解推荐加载接口处)
package com.yiwugou.demo.dubbo.service;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
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 javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import com.yiwugou.demo.dubbo.bean.User;
@Path("users")
@Produces({ MediaType.APPLICATION_JSON })
public interface UserService {
@GET
@Path("get/{id}")
public User get(@PathParam("id") Long id);
@POST
@Path("byjson")
@Consumes({ MediaType.APPLICATION_JSON })
public User byJson(User user);
@POST
@Path("byform")
@Consumes({ MediaType.APPLICATION_FORM_URLENCODED })
public User byForm(@FormParam("id") Long id, @FormParam("username") String username);
@GET
@Path("byquery")
public User byQuery(@QueryParam("username") String username, @QueryParam("password") String password);
}
UserServiceImpl.java
package com.yiwugou.demo.dubbo.service;
import org.springframework.stereotype.Service;
import com.yiwugou.demo.dubbo.bean.User;
@Service("userService")
public class UserServiceImpl implements UserService {
@Override
public User get(Long id) {
User user = new User();
user.setUsername(id + "-get-username");
user.setPassword(id + "-get-password");
System.err.print(user);
return user;
}
@Override
public User byJson(User user) {
// TODO Auto-generated method stub
return null;
}
@Override
public User byForm(Long id, String username) {
User user = new User();
user.setUsername(id + "-byForm-" + username);
user.setPassword(id + "-byForm-password");
System.err.print(user);
return user;
}
@Override
public User byQuery(String username, String password) {
User user = new User();
user.setUsername("byQuery-" + username);
user.setPassword("byQuery-" + password);
System.err.print(user);
return user;
}
}
rest 访问的路径和方法为
get
http://127.0.0.1:8080/users/get/1234
post with body
http://127.0.0.1:8080/users/byjson
{
username:"abcd",
password:"1234"
}
post with form
http://127.0.0.1:8080/users/byform
username=abcd
password=1234
(html form)
get
http://127.0.0.1:8080/users/byquery?username=abcd&password=1234
非dubbo 如何调用 dubbo-rest 以及如何负载均衡?
官方没有提供相应的rest-client,
- 自己写client代码
访问zk node 节点 可以得到真实服务地址与端口信息列表 然后负载均衡
(进入zookeeper 运行命令 ls /dubbo/com.yiwugou.demo.dubbo.service.UserService/providers
rest://10.6.62.155:8080/com.yiwugou.demo.dubbo.service.UserService?anyhost=true&application=dubbo-demo-provider&dubbo=2.6.1&generic=false&interface=com.yiwugou.demo.dubbo.service.UserService&methods=get,byJson,byQuery,byForm&pid=1292&side=provider×tamp=1523410526582,
rest://10.6.62.155:8081/com.yiwugou.demo.dubbo.service.UserService?anyhost=true&application=dubbo-demo-provider&dubbo=2.6.1&generic=false&interface=com.yiwugou.demo.dubbo.service.UserService&methods=get,byJson,byQuery,byForm&pid=5296&side=provider×tamp=1523412396290,
)
2 建议使用第三方rest的负载均衡
如 nginx, yiwugou homer
dubbo 调用 dubbo-rest
照常即可 我们不需要关注里面的具体协议是 dubbo 还是 rest
dubbo 调用 非dubbo-rest
~1. 可以用java代码或者采用第三方rest-client
~2.需要根据提供的 rest服务以及返回参数 自己组装类和接口
例如,我们要调用如下的外部服务
http://api.foo.com/services/users/1001
http://api.foo.com/services/users/1002
获取不同ID的用户资料,返回格式是JSON
{
"id": 1001,
"name": "Larry"
}
我们可根据这些信息,编写服务接口和参数类即可:
@Path("users")
public interface UserService {
@GET
@Path("{id : \\d+}")
@Produces({MediaType.APPLICATION_JSON})
User getUser(@PathParam("id") Long id);
}
public class User implements Serializable {
private Long id;
private String name;
}
对于spring中的配置,因为这里的REST服务不是dubbo提供的,所以无法使用dubbo的注册中心,直接配置外部REST服务的url地址即可(如多个地址用逗号分隔):
<dubbo:reference id="userService" interface="xxx.UserService" url="rest://api.foo.com/services/"/>
网友评论