spring-cloud+dubbo+hessian集成
spring-cloud+dubbo+hessian+nacos集成
本文基于spring-cloud-alibaba版本集成dubbo协议、hessian协议,dubbo版本为2.7.3,hessian版本4.0.7,注册中心使用的是nacos,
添加依赖
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.7</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>javax.servlet</groupId>-->
<!-- <artifactId>javax.servlet-api</artifactId>-->
<!-- <version>4.0.1</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
</dependency>
javax.servlet-api 我本地其他包依赖已经添加 去除;
jetty配置hessian服务 默认使用jetty;
服务端以及客户端都需要引用相同的依赖进行开发,防止调用过程中产生不定的错误;
服务端
在service实现类上添加 @Service
@Slf4j
@Service(protocol = {"dubbo"})
public class ServiceImpl implements PubDeptService {
注意:此处的@Service 为dubbo的 ,org.apache.dubbo.config.annotation.Service;
服务端配置类需要添加hessian 配置其服务信息,如下:
@Configuration
public class DubboHessianConfig {
@Bean("hessian")
public ProtocolConfig restProtocolConfig() throws NacosException {
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName("hessian");
protocolConfig.setId("hessian");
protocolConfig.setServer("jetty");
protocolConfig.setPort(9091);
protocolConfig.setAccepts(500);
protocolConfig.setThreads(100);
// protocolConfig.setContextpath("/file");
// 暴露hessian接口之后注册服务
// Properties properties = new Properties();
// properties.setProperty("serverAddr","127.0.0.1:8848");
// properties.setProperty("namespace","ec071e39-164b-4fa5-bc19-e0504a114342");
// properties.setProperty("groupName","SYSTEM");
// NamingService namingService = NamingFactory.createNamingService(properties);
// Instance instance = new Instance();
// instance.setIp(NetUtils.localIP());
// instance.setPort(9090);
// Map map = new HashMap<>();
// map.put("hessianUrl", "/file");
// instance.setMetadata(map);
// namingService.registerInstance("com.iop.dubbo.hessian.server", instance);
// 可继续增加其它配置
return protocolConfig;
}
@Bean
public ServletRegistrationBean<DispatcherServlet> servletRegistrationBean() {
return new ServletRegistrationBean<DispatcherServlet>(new DispatcherServlet(), "/*");
}
}
服务端配置就完成了;服务启动中会启动jetty server;
客户端配置
引用上述依赖,服务端需要提供暴露的api service共享,然后在需要引用的接口上使用 @Reference 即可,
@Reference
LoginService LoginService;
@Reference
SSoTokenService SSoTokenService;
@org.apache.dubbo.config.annotation.Reference(interfaceClass = PubFileService.class,proxy = "javassist",url = "hessian://192.168.101.1:9091/file")
FileService FileService;
FileService 是用来对 InputStream in 进行传输的,若不用hessian 建议转成字节 dubbo协议进行传输;
public String savePubFileOnceWithFileArr(FilePara fp, byte[] fileInputStream) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(fileInputStream);
return savePubFileOnce(fp,inputStream);
}
需要调整dubbo传输的大小限制;