springboot 整合hessian(server端)

2018-08-28  本文已影响0人  不知不怪

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>com.example</groupId>
    <artifactId>hessianserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>HessianServer</name>
    <description>Demo project for Spring Boot</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.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>
        <dependency>
            <groupId>com.caucho</groupId>
            <artifactId>hessian</artifactId>
            <version>4.0.38</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.properties

server.port = 8080

user.java

package com.example;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer id;
    private String name;
    private Date birthday;

    public User(Integer id, String name, Date birthday) {
        super();
        this.id = id;
        this.name = name;
        this.birthday = birthday;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

}

userCondition.java

package com.example;

import java.io.Serializable;
import java.util.Date;

public class UserCondition implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer id;
    private String name;
    private Date birthday;

    public UserCondition(Integer id, String name, Date birthday) {
        super();
        this.id = id;
        this.name = name;
        this.birthday = birthday;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "UserCondition [id=" + id + ", name=" + name + ", birthday=" + birthday + "]";
    }

}

IHelloService

package com.example;

import java.util.List;

/**
 * @功能描述 Hessian实例之服务器
 * @author www.gaozz.club
 * @date 2018-08-26
 */
public interface IHelloService {
    String sayHello(String name);

    List<User> queryList(UserCondition cond);
}

HelloService

package com.example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

/**
 * @功能描述 Hessian实例之服务器
 * @author www.gaozz.club
 * @date 2018-08-26
 */
@Service("HelloService")
public class HelloService implements IHelloService {
    @Override
    public String sayHello(String name) {
        return "Hello World! " + name;
    }

    @Override
    public List<User> queryList(UserCondition cond) {
        System.out.println(cond);
        List<User> list = new ArrayList<>();
        list.add(new User(1, "张三", new Date()));
        list.add(new User(2, "李四", new Date()));
        return list;
    }
}

主程序(重点.....)

package com;

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 com.example.IHelloService;

/**
 * @功能描述 Hessian实例之服务器
 * @author www.gaozz.club
 * @date 2018-08-26
 */
@SpringBootApplication
public class HessianServer {

    @Autowired
    private IHelloService service;

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

    @Bean(name = "/HelloService")
    public HessianServiceExporter accountService() {
        HessianServiceExporter exporter = new HessianServiceExporter();
        exporter.setService(service);
        exporter.setServiceInterface(IHelloService.class);
        return exporter;
    }
}

代码生成器源码

代码生成器演示

spring boot 2.x 实例源码

上一篇 下一篇

猜你喜欢

热点阅读