首页> 文章> 详情

spring boot RPC 框架 Hessian

教程分享 > Java教程 (12036) 2024-04-17 12:31:25

一.背景说明

 该博客主要讲述的是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-leftso-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-leftso-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);
	}

}

浏览器访问地址:
rpc

 
https://www.leftso.com/article/294.html

相关文章
spring boot RPC 框架 Hessian,本文主要讲解spring boot整合hessian实现Spring boot RPC调用和Spring boot rpc框架hessian...
Spring boot hessian 通讯加密,Spring boot hessian RPC框架通讯之间的加密验证。实现安全的RPC访问
spring boot 1.5整合redis实现spring的缓存框架,spring boot,redis
Spring Boot 2.0,Spring框架的Spring Boot 中的Spring Boot Actuator变化讲解。并且了解如何在Spring Boot 2.0中使用Actuator...
spring boot又一个spring框架的经典项目,本文讲解spring boot入门的环境配置以及第一个项目,Spring Boot 入门教程
spring boot入门,spring boot是一个崭新的spring框架分支项目,本文讲解其属性配置相关
spring boot是一个崭新的spring框架分支项目,本文讲解基本的数据库配置
spring boot框架中常见注解说明,spring boot,JAVA
Spring Boot 2.0 Redis整合,通过spring boot 2.0整合Redis作为spring缓存框架的实现。
spring boot是一个崭新的spring框架分支项目,本文讲解spring boot中controller的常用注解使用
引言    通过之前spring boot mybatis 整合的讲解: spring boot mybaties整合  (spring boot mybaties 整合 基于Java注解方式写...
Spring Boot 入门 AOP 日志处理,这里主要讲解spring boot 中采用AOP方式进行日志的统一处理。spring 框架的处理也是一样。综合来讲,本文主要讲解了spring b...
Spring Boot validation整合hibernate validator实现数据验证,Spring Boot validation使用说明,Spring Boot validat...
spring boot框架整合MyBatis数据库暂时选用MySQL
Spring Boot 2.0 绑定properties属性资源文件 Spring Boot 2.0 读取properties配置文件值 Spring Boot 2.0获取properties配...