Springboot寫的Hessian例子

SpringBoot中添加Hessian框架

Hessian通常用來作RPC接口,經過http傳輸二進制文件,用於程序和程序之間的通訊。 在這個例子中,有兩個項目,客戶端(hessianClient)和主項目(asset)java

1.新建一個Springboot項目

導入依賴爲web

<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.javaspring

package com.ucardemo.asset.api;

import com.ucardemo.asset.model.User;

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

MyHessianServiceImpl.javaapi

@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.寫一個服務端的項目,調用接口

關鍵點:app

客戶端須要調用服務端的服務,須要將服務端項目打包成jar框架

客戶端引用jar文件,才能調用接口ide

客戶端核心代碼:spring-boot

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);
    }
}
相關文章
相關標籤/搜索