微服务:dubbox+zookeeper构建restful风格
dubbox+zookeeper构建restful服务
本章内容基于dubbo+zookeeper快速搭建
上一节,我们快速搭建了一个简单的dubbo服务(我姑且这样称呼)
弊端当然是,只有同样是java写的消费端才可以调用我们的服务。
所以搞个restful风格吧!!!
首先澄清dubbox的主人是当当网,而bubbo是阿里。而restful风格的支持也是在dubbox中才体现的。但是由于本人才浅,没有找到maven仓库中的dubbox。只能自己编译一下了。
dubbox-2.8.0 maven构建
1.下载源码包,这里我选择了2.8.0
https://github.com/dangdangdotcom/dubbox/releases
2.下载解压,开始install吧
跳过测试,比较快
mvn install -Dmaven.test.skip=true
这样本地仓库有了dubbox-2.8.0。我们就可以使用了。
注意SUCCESS后瞅瞅,本地仓库是否存在了哟!!!
开始我们的restful之旅
参见官方指导
使用restful,需要额外引入dubbo-rpc-rest
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-rpc-rest</artifactId>
<version>2.8.0</version>
</dependency>
1.服务接口
package com.xbz.learning.dubbox.demo.provider;
import com.xbz.learning.dubbox.demo.entity.User;
@Path("user")
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
public interface DemoService {
@GET
@Path("{uid}")
public User sayHello(@PathParam("uid")String userId);
}
2.服务实现类
package com.xbz.learning.dubbox.demo.provider.impl;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.xbz.learning.dubbox.demo.entity.User;
import com.xbz.learning.dubbox.demo.provider.DemoService;
@Path("user")
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
public class DemoServiceImpl implements DemoService{
@GET
@Path("{uid}")
public User sayHello(@PathParam("uid")String userId) {
System.out.println("input param uid :"+userId);
User user=new User();
user.setUsername("xbz");
user.setPassword("1215xubin");
return user;
}
}
注意:
1.类的上方要写一个@Path注解,否则会报如下错误
2.注解会依据接口类的定义,官方说法是接口注解在实现类上面有利于代码维护
3.对于dubbo消费者必须要在提供的接口上体现注解,所以我这里接口类和实现类保持一致的注解
java.lang.RuntimeException: Class is not a root resource. It, or one of its interfaces must be annotated with @Path:[xxx类名]
3.暴漏服务
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="hello-world-app" />
<!-- 使用zookeeper广播注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.35.100:2181" check="false" subscribe="false" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service version="1.0.0" interface="com.xbz.learning.dubbox.demo.provider.DemoService" ref="demoService" />
<!-- 指定端口8080,使用内嵌服务器jetty[默认] -->
<dubbo:protocol name="rest" port="8080" server="jetty" ></dubbo:protocol>
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.xbz.learning.dubbox.demo.provider.impl.DemoServiceImpl"></bean>
</beans>
4.启动类
package com.xbz.learning.dubbox.demo;
import java.io.IOException;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xbz.learning.dubbox.demo.provider.DemoService;
public class DemoServiceRegisterTest {
@Test
public void startDemoProvider() throws IOException{
System.out.println("准备启动DemoServiceProvider");
ClassPathXmlApplicationContext ap=new ClassPathXmlApplicationContext("classpath:applicationContext-provider.xml");
DemoService demoServiceProvider=(DemoService) ap.getBean("demoService");
System.out.println("启动DemoServiceProvider完成");
System.in.read();
}
}
启动完成后,在浏览器中访问:
http://127.0.0.1:8080/user/888
看看效果
客户端访问方式
1.dubbo消费者–>dubbox提供者
2.非dubbo消费者–>使用restful请求
非dubbo(x)客户端当然可以使用restful请求数据喽,选择自己喜欢的方式就好
dubbo消费者如果调用rest 的dubbox服务呢?
就像消费者根本不知道rest这回事一般,写写配置文件
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="consumer-of-helloworld-app" />
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry address="zookeeper://192.168.35.100:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference
id="demoService"
version="1.0.0"
interface="com.xbz.learning.dubbox.demo.provider.DemoService"
/>
</beans>
的确和上一节的调用方式,一模一样吧!!!
更多推荐
所有评论(0)