spring boot RPC 框架 Hessian

spring boot RPC 框架 Hessian

一.背景说明

 该博客主要讲述的是spring boot整合hession实现Java编程中的RPC(远程调用)。实现RPC的方式还有很多例如Java的RMI,webservice等。这里主要讲解hessian的方式。

二.hessian服务端

2.1创建一个spring boot项目添加web依赖和hessian依赖

pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>net.xqlee.project</groupId>
	<artifactId>demo-springboot-rpc-hessian-server</artifactId>
	<version>2.0</version>
	<packaging>jar</packaging>

	<name>demo-springboot-rpc-hessian-server</name>
	<description>lee-note234-v2.0</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.8.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

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

		<!-- https://mvnrepository.com/artifact/com.caucho/hessian -->
		<dependency>
			<groupId>com.caucho</groupId>
			<artifactId>hessian</artifactId>
			<version>4.0.51</version>
		</dependency>

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

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

2.2创建一个简单的service接口并实现

HelloWorldServcie.java接口:

package net.xqlee.project.service;

public interface HelloWorldService {
	String sayHello(String name);
}

HelloWorldServiceServiceImp.java

package net.xqlee.project.service;

import org.springframework.stereotype.Service;

@Service("HelloWorldService")
public class HelloWorldServiceServiceImp implements HelloWorldService {

	@Override
	public String sayHello(String name) {
		return "Hello, " + name;
	}

}

2.3注册service接口到hessian工厂

package net.xqlee.project;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.remoting.caucho.HessianServiceExporter;

import net.xqlee.project.service.HelloWorldService;

@SpringBootApplication
public class DemoSpringbootRpcHessianApplication {
	@Autowired
	private HelloWorldService helloWorldService;

	public static void main(String[] args) {
		SpringApplication.run(DemoSpringbootRpcHessianApplication.class, args);
	}

	// 发布服务
	@Bean(name = "/HelloWorldService")
	public HessianServiceExporter accountService() {
		HessianServiceExporter exporter = new HessianServiceExporter();
		exporter.setService(helloWorldService);
		exporter.setServiceInterface(HelloWorldService.class);
		return exporter;
	}
}

至此服务端编写完毕。


提示:


这里需要一个操作.eclipse中进行右键导出,将接口导出到一个jar包文件,这样也是方便调用方使用。我这里导出并命令为rpc-hessian-demo-interface.jar

 

三.hessian客服端

3.1创建一个spring boot项目添加web依赖和hessian依赖

注意,还需要引入刚才服务端导出的
rpc-hessian-demo-interface.jar

pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>net.xqlee.project</groupId>
	<artifactId>demo-springboot-rpc-hessian-client</artifactId>
	<version>2.0</version>
	<packaging>jar</packaging>

	<name>demo-springboot-rpc-hessian-client</name>
	<description>lee-note234-v2.0</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.8.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

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

		<!-- https://mvnrepository.com/artifact/com.caucho/hessian -->
		<dependency>
			<groupId>com.caucho</groupId>
			<artifactId>hessian</artifactId>
			<version>4.0.51</version>
		</dependency>
		<!-- 本地引入hessian服务端的接口 -->
		<dependency>
			<groupId>net.xqlee.project</groupId>
			<artifactId>rpc-hessian-demo</artifactId>
			<version>1.0</version>
			<scope>system</scope>
			<systemPath>${project.basedir}/lib/rpc-hessian-demo-interface.jar</systemPath>
		</dependency>

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

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

3.2将引入的服务端接口通过hessian实例化

package net.xqlee.project;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.remoting.caucho.HessianProxyFactoryBean;

import net.xqlee.project.service.HelloWorldService;

@SpringBootApplication
public class DemoSpringbootRpcHessianClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoSpringbootRpcHessianClientApplication.class, args);
	}

	@Bean
	public HessianProxyFactoryBean helloClient() {
		HessianProxyFactoryBean factory = new HessianProxyFactoryBean();
		factory.setServiceUrl("http://localhost:8083/HelloWorldService");
		factory.setServiceInterface(HelloWorldService.class);
		return factory;
	}

}

3.3创建一个测试的controller进行调用测试

package net.xqlee.project.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import net.xqlee.project.service.HelloWorldService;

@RestController
public class DemoHessianController {

	@Autowired
	HelloWorldService helloWorldService;

	@GetMapping("/rpchello.do")
	public Object rpcSayHello(String name) {
		return helloWorldService.sayHello(name);
	}

}

浏览器访问地址:


 

spring boot RPC 框架 Hessian

相关文章:

你感兴趣的文章:

标签云: