SpringBoot 集成Hessian RPC及其应用
1、依赖
<!--rpc调用用例-->
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.63</version>
</dependency>
2、声明服务
(1)简单模式
接口:
public interface MyHessianService {
String hello(String var1);
}
实现类:
@Service
public class MyHessianServiceImpl implements MyHessianService{
@Override
public String hello(String var1) {
return "我是rpc的服务端";
}
}
(2)注解模式
前缀默认是注解中写的值
@HessianRpcService
public class MyHessianServiceImpl implements MyHessianService{
@Override
public String hello(String var1) {
return "我是rpc的服务端";
}
}
或者
自定义前缀,注解内容详见注解发布服务
@HessianRpcService("rpc")
public class MyHessianServiceImpl implements MyHessianService{
@Override
public String hello(String var1) {
return "我是rpc的服务端";
}
}
3、服务发布
(1)方式一(手动发布)
可以在启动类中直接写、或者可以单独写一个类,加上@Configuration也可以,但是这种写法对于有大量的rpc服务很不友好,及其麻烦
@SpringBootApplication
public class DemoRpcApplication {
public static void main(String[] args) {
SpringApplication.run(DemoRpcApplication.class, args);
}
//写法1
@Autowired
MyHessianService myHessianService;
@Bean(name = "/hessian")
public HessianServiceExporter exportHelloService() {
HessianServiceExporter exporter = new HessianServiceExporter();
exporter.setService(myHessianService);
exporter.setServiceInterface(MyHessianService.class);
return exporter;
}
(2)方式二(xml发布)
SpringBoot号称基于注解式开发,所以xml也不太建议
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:context="http://www.springframework.org/schema/context"
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">
<bean id="rpc" class="com.example.rpc.test.impl.MyHessianServiceImpl" />
<bean name="/rpc" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="rpc" />
<property name="prc" value="com.example.rpc.test.MyHessianService" />
</bean>
</beans>
加载xml
@SpringBootApplication
@ImportResource(locations = {"classpath:hessian-service.xml"})
public class DemoRpcApplication {
public static void main(String[] args) {
SpringApplication.run(DemoRpcApplication.class, args);
}
}
(3)方式二(注解发布)
注解定义
package com.example.rpc.test;
import org.springframework.stereotype.Service;
import java.lang.annotation.*;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Service
public @interface HessianRpcService {
String value() default "hession";
}
注解扫描
适合大量rpc服务,很方便
package com.example.rpc.test;
/**
* Created with IntelliJ IDEA.
*
* @Author: qzc
* @Date: 2023/06/13/16:18
* @Description:
*/
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.remoting.caucho.HessianServiceExporter;
import org.springframework.stereotype.Component;
import java.util.LinkedHashSet;
import java.util.Set;
@Component
public class HessianRpcServiceScanner implements BeanFactoryPostProcessor {
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
Set<String> beanDefinitions = new LinkedHashSet<>();
String[] beanNames = beanFactory.getBeanNamesForAnnotation(HessianRpcService.class);
for (String beanName : beanNames) {
String className = beanFactory.getBeanDefinition(beanName).getBeanClassName();
Class<?> clazz;
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new BeanInitializationException(e.getMessage(), e);
}
String hessianRpcServiceBeanName = "/" + beanName.replace("Impl", "");
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(HessianServiceExporter.class);
builder.addPropertyReference("service", beanName);
builder.addPropertyValue("serviceInterface", clazz.getInterfaces()[0].getName());
((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(hessianRpcServiceBeanName, builder.getBeanDefinition());
beanDefinitions.add(hessianRpcServiceBeanName);
}
if (beanDefinitions.isEmpty()) {
System.out.println(this.getClass().getName() + ": not service be scanned");
} else {
for (String beanName : beanDefinitions) {
System.out.println(this.getClass().getName() + ": " + beanName);
}
}
}
}
注解应用
见声明服务注解模式
4、客户端应用
(1)创建接口
客户端需要创建和服务端一样的接口,建议抽离为jar包,客户端和服务端进行引用、这样比较方便
(2)客户端调用
方式一
String rpc ="http://localhost:8082/rpc/hession";
HessianProxyFactory factory = new HessianProxyFactory();
factory.setOverloadEnabled(true);//开启重载
MyHessianService basic = (MyHessianService) factory.create(MyHessianService.class, rpc);
//调用
String str= basic.hello(key,multipartFile.getInputStream());
方式二
配置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:context="http://www.springframework.org/schema/context"
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">
<bean id="rpc" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceInterface" value="com.example.rpc.test.MyHessianService" />
<property name="serviceUrl" value="http://localhost:8080/rpc" />
<property name="overloadEnabled" value="true" />
</bean>
</beans>
读取配置
@SpringBootApplication
@ImportResource(locations = {"classpath:hessian-client.xml"})
public class DemoRpcApplication {
public static void main(String[] args) {
SpringApplication.run(DemoRpcApplication.class, args);
}
}
使用的时候在需要应用的类中直接注入即可