Springboot写的Hessian例子

SpringBoot中添加Hessian框架

Hessian一般用来做RPC接口,通过http传输二进制文件,用于程序和程序之间的通信。 在这个例子中,有两个项目,客户端(hessianClient)和主项目(asset)

1.新建一个Springboot项目

导入依赖为

<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>com.caucho</groupId>
	<artifactId>hessian</artifactId>
	<version>4.0.33</version>
</dependency>

2.写调用的接口文件

MyHessianService.java

package com.ucardemo.asset.api;

import com.ucardemo.asset.model.User;

public interface MyHessianService {
    public String justHadEnoughParties();
    public boolean checkLogin(User user);
}

MyHessianServiceImpl.java

@Service("myHessianService")
public class MyHessianServiceImpl implements MyHessianService {
    [@Override](https://my.oschina.net/u/1162528)
    public String justHadEnoughParties() {
        System.out.println("task--------------->");
        return "Please save me..";
    }

    [@Override](https://my.oschina.net/u/1162528)
    public boolean checkLogin(User user) {
        String username=user.getUsername();
        String password=user.getPassword();
        if(username.equals("tdw") && password.equals("123456")){
            System.out.println("登录成功");
            return true;
        }
        System.out.println("登录失败");
        return false;
    }
}

3.在主Application中注册一个HessianBean

其他的服务器通过访问 http://***/myHessianService 即可调用其接口Service

@Autowired
	MyHessianService myHessianService;
@Bean(name = "/myHessianService")
	public HessianServiceExporter exportHelloService() {
		HessianServiceExporter exporter = new HessianServiceExporter();
		exporter.setService(myHessianService);
		exporter.setServiceInterface(MyHessianService.class);
		return exporter;
	}

4.写一个服务端的项目,调用接口

关键点:

客户端需要调用服务端的服务,需要将服务端项目打包成jar

客户端引用jar文件,才能调用接口

客户端核心代码:

package com.useapi.hessionclient.Controller;
import com.caucho.hessian.client.HessianProxyFactory;
import com.ucardemo.asset.api.MyHessianService;
import com.ucardemo.asset.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.net.MalformedURLException;
@RestController
public class ClientController {
    @RequestMapping("/xxxx")
    @ResponseBody
    public void test() throws MalformedURLException {
        String url = "http://localhost:8081/myHessianService";
        HessianProxyFactory factory = new HessianProxyFactory();
        MyHessianService myHessianService = (MyHessianService) factory.create(MyHessianService.class, url);
        User user=new User();
        user.setPassword("123");
        user.setUsername("123");
        Boolean b= myHessianService.checkLogin(user);
        System.out.println(b);
    }
}

转载于:https://my.oschina.net/u/2344382/blog/1833859

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值