SpringBoot 整合 Hessian

Hessian简介

Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。
相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,
所以它很适合于发送二进制数据。但是它的参数和返回值都需要实现Serializable接口

Hessian使用

  1. Hessian分为服务端和客户端。
    服务端则负责提供服务,客户端则消费服务。但是服务器和客户端不是绝对的。

  2. 使用springboot整合Hessian

共有模块

接口

1
2
3
4
public interface HelloWorldService {

List<User> insertUserAndGetAll(User user);
}

User实体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.ooyhao.hessianserver.javahessianserver.bean;
import java.io.Serializable;
public class User implements Serializable {

private String username;

private Integer age;

private String email;

public User() {
}

public User(String username, Integer age, String email) {
this.username = username;
this.age = age;
this.email = email;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}

服务端

依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- hessian -->
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
<!-- springboot-mvc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.ooyhao.hessian</groupId>
<artifactId>java-hessian-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
  • 注意:如果不实现Serializable接口,会出现以下错误,所以,接口的返回值和参数都必须实现Serializable接口。
    must serializable

ServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Service
public class HelloWorldServiceImpl implements HelloWorldService {

@Override
public List<User> insertUserAndGetAll(User user) {

List<User> users = new ArrayList<>();
for (int i = 0; i < 5; i++) {
User u = new User();
u.setUsername("Tom"+i);
u.setAge(10+i);
u.setEmail(i+"123456@163.com");
users.add(u);
}
users.add(user);
return users;
}
}

config文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Configuration
public class HessianConfig {

@Autowired
private HelloWorldService helloWorldService;

@Bean(name = "/hessianService")
public HessianServiceExporter hessianService(){
HessianServiceExporter exporter = new HessianServiceExporter();
exporter.setService(helloWorldService);
exporter.setServiceInterface(HelloWorldService.class);
return exporter;
}
}

客户端

依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- hessian -->
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.ooyhao.hessian-server</groupId>
<artifactId>java-hessian-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>com.ooyhao.hessian</groupId>
<artifactId>java-hessian-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
  • 上述依赖可以看出:客户端需要依赖服务端,所以,需要将服务端打包成jar。

config文件

1
2
3
4
5
6
7
8
9
10
@Configuration
public class HessianConfig {
@Bean
public HessianProxyFactoryBean helloClient(){
HessianProxyFactoryBean factoryBean = new HessianProxyFactoryBean();
factoryBean.setServiceUrl("http://localhost:8080/hessianService");
factoryBean.setServiceInterface(HelloWorldService.class);
return factoryBean;
}
}

调用服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RestController
public class HelloWorldController {

@Autowired
private HelloWorldService helloWorldService;

@RequestMapping("/test")
public List<User> test(){
User u = new User();
u.setEmail("625194999@qq.com");
u.setAge(23);
u.setUsername("ooyhao");
return helloWorldService.insertUserAndGetAll(u);
}
}

效果

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×