DUBBO REST -- HelloWorld(附完整实践源码)

dubbo rest 总结
一、概述:
  1.REST的优点(摘自维基百科)
    可更高效利用缓存来提高响应速度
    通讯本身的无状态性可以让不同的服务器的处理一系列请求中的不同请求,提高服务器的扩展性
    浏览器即可作为客户端,简化软件需求
    相对于其他叠加在HTTP协议之上的机制,REST的软件依赖性更小
    不需要额外的资源发现机制
    在软件技术演进中的长期的兼容性更好
    基于简单的文本格式消息和通用的HTTP协议,使它具备极广的适用性,几乎所有语言和平台都对它提供支持,同时其学习和使用的门槛也较低。
  2.应用场景
    由于REST在适用性方面的优点,所以在dubbo中支持REST,可以为当今多数主流的远程调用场景都带来(显著)好处:
      显著简化企业内部的异构系统之间的(跨语言)调用。此处主要针对这种场景:dubbo的系统做服务提供端,其他语言的系统(也包括某些不基于dubbo的java系统)做服务消费端,两者通过HTTP和文本消息进行通信。
      显著简化浏览器AJAX应用的开发。类似于2,既可以用dubbo来开发专门的AJAX服务器端,也可以将原内部使用的dubbo service直接”透明“的暴露给浏览器中JavaScript。当然,很多AJAX应用更适合与web框架协同工作,所以直接访问dubbo service在很多web项目中未必是一种非常优雅的架构。
      为企业内部的dubbo系统之间(即服务提供端和消费端都是基于dubbo的系统)提供一种基于文本的、易读的远程调用方式。
      以上部分是dubbo的REST调用最有价值的应用场景,并且我们为dubbo添加REST调用,其最主要到目的也是面向服务的提供端,即开发REST服务来提供给非dubbo的(异构)消费端。
  3.实践中遇到的问题:
    - 在注册服务时出现一个未知的注册IP 报错:cause: Failed to register dubbo://192.168.122.1
      解决:在provider.xml中加入host节点 <dubbo:protocol name="rest" port="7001" server="tomcat" host="服务所在主机IP"/>
    - non-parseable settings使用maven install命令,报错:
    Non-parseable settings C:\Users\xxxx.m2\settings.xml: expected START_TA
    G or END_TAG not TEXT (position: TEXT seen ...\n\t \ua0\

      解决:pom.xml里的内容是复制粘贴的?把报错处附近行尾的空白字符删掉就可以了,我就是这样解决的

    - dubbo rest处理post请求,取不到请求参数 报错415 Unsupported Media Type
    解决:缺少JAR包-->
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson-provider</artifactId>
            <version>2.3.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxb-provider</artifactId>
            <version>3.0.16.Final</version>
        </dependency>
二、实现过程(源码)
  1.项目结构图
  
  2.Provider.java文件(main方法)代码

package com.juwenzhe.dubbo.rest;

import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * @author Jupiter
 * @devDate 2019年1月2日
 * @description dubbo rest的启动入口
 */
public class Provider {
	@SuppressWarnings("resource")
	public static void main(String[] args) throws Exception{
		FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("classpath:provider.xml"); 
		context.start(); 
		// 阻塞 
		//System.in.read(); 
		synchronized (Provider.class) {
			while(true){
				Provider.class.wait();
			}
		} 
	}
}

  3.Service.java接口文件代码

package com.juwenzhe.dubbo.rest.service;

/**
 * @author Jupiter
 * @devDate 2019年1月2日
 * @description Hello World服务接口
 */
public interface Service {
	 String hello(); 
}

  4.ServiceImpl.java实现类文件代码

package com.juwenzhe.dubbo.rest.service.impl;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import com.juwenzhe.dubbo.rest.service.Service;

/**
 * @author Jupiter
 * @devDate 2019年1月2日
 * @description HelloWorld接口实现类
 */
@Path("demo") 
public class ServiceImpl implements Service {

	@Path("hello") 
	@GET 
	public String hello() {
		return "Hello World! "; 
	} 
}

  5.provider.xml文件代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
	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"> <!-- provider's application name, used for tracing dependency relationship -->
	<dubbo:application name="demo-provider" /> <!-- use multicast registry center to export service -->
	<dubbo:registry address="zookeeper://localhost:2181" /> 
	<!-- use dubbo protocol to export service on port 20880 -->
	<dubbo:protocol name="dubbo" port="2080" />
	<dubbo:protocol name="rest" port="7001" server="tomcat"/>
	<bean id="demoService" class="com.juwenzhe.dubbo.rest.service.impl.ServiceImpl" /> 
	<!-- declare the service interface to be exported -->
	<dubbo:service interface="com.juwenzhe.dubbo.rest.service.Service"
		ref="demoService" protocol="dubbo,rest" />
</beans>

  6.jsw.xml文件(项目发布包打包使用,简单实践可以不添加此文件)代码

<assembly
	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
	<id>bin</id><!-- 打成的包附带的名称 如: wx_gws-1.1.5.5-bin.tar.gz -->
	<includeBaseDirectory>false</includeBaseDirectory>
	<formats>
		<format>tar.gz</format>
	</formats>
	<fileSets>
		<fileSet>
			<!-- 需要打包的路径 -->
			<directory>${project.build.directory}/generated-resources/appassembler/jsw/${project.artifactId}</directory>
			<!-- 打包后输出的路径 -->
			<outputDirectory>${project.artifactId}</outputDirectory>
			<!-- 设置文件、文件夹的访问权限755 -->
			<fileMode>0755</fileMode>
			<directoryMode>0755</directoryMode>
		</fileSet>
		<fileSet>
			<directory>src/main/resources</directory>
			<outputDirectory>${project.artifactId}/logs</outputDirectory>
			<!-- 排除某些文件的样例 -->
			<excludes>
				<exclude>**/*</exclude>
			</excludes>
		</fileSet>
	</fileSets>
</assembly>

  7.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>com.juwenzhe.dubbo.test</groupId>
	<artifactId>dubbo-rest-01</artifactId>
	<version>0.0.1-SNAPSHOT</version>


	<dependencies>
		<dependency>
			<groupId>org.jboss.resteasy</groupId>
			<artifactId>resteasy-client</artifactId>
			<version>3.0.12.Final</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
		<!-- <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> 
			<version>4.1.30.Final</version> </dependency> <dependency> <groupId>io.netty</groupId> 
			<artifactId>netty</artifactId> <version>3.6.10.Final</version> </dependency> -->
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.9</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.6.0</version>
			<exclusions>
				<!-- <exclusion> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> 
					</exclusion> -->
			</exclusions>
		</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>javax.validation</groupId>
			<artifactId>validation-api</artifactId>
			<version>1.1.0.Final</version>
		</dependency>
	</dependencies>
</project>

  8.启动项目
  
  
  +.想让dubbo rest在linux上启动,相关打包配置可参考 :> 启动项目及dubbo容器的初始化工作流程分析
  https://blog.csdn.net/Juwenzhe_HEBUT/article/details/84336243
三、参考文献
  1.如何使用Dubbo-2.6.0开发REST接口 https://yq.aliyun.com/ziliao/490483
  2.dubbo rest官方文档 http://dubbo.apache.org/zh-cn/docs/user/references/protocol/rest.html
  3.架构师之路-在Dubbo中开发REST风格的远程调用 https://segmentfault.com/a/1190000012132553

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值