springboot结合hessian的使用

1. 架构
在这里插入图片描述
细化结构:
在这里插入图片描述
1. 1.hessian-study的pom文件

<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.fracong</groupId>
    <artifactId>hessian-study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <properties>
        <java.version>1.8</java.version>
        <ch.qos.logback.version>1.2.3</ch.qos.logback.version>
        <fastjson-version>1.2.29</fastjson-version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>${ch.qos.logback.version}</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>${ch.qos.logback.version}</version>
            </dependency>
            <!-- json -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson-version}</version>
            </dependency>
            <!-- commons-lang -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.0</version>
            </dependency>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <modules>
        <module>hessian-server</module>
        <module>hessian-client</module>
        <module>hessian-interface</module>
    </modules>
</project>

1.2.hessian-interface的pom文件

<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>
  <parent>
    <groupId>com.fracong</groupId>
    <artifactId>hessian-study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>hessian-interface</artifactId>
</project>
1.3.hessian-service的pom文件
<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>
    <parent>
        <groupId>com.fracong</groupId>
        <artifactId>hessian-study</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>hessian-server</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <dependency>    
            <groupId>com.caucho</groupId>    
            <artifactId>hessian</artifactId> 
            <version>4.0.38</version>   
        </dependency>
        <dependency>
            <groupId>${project.parent.groupId}</groupId>
            <artifactId>hessian-interface</artifactId>
            <version>${project.parent.version}</version>
            <type>jar</type>
        </dependency>
    </dependencies>
</project>

1.4.hessian-client的pom文件

<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>
  <parent>
    <groupId>com.fracong</groupId>
    <artifactId>hessian-study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>hessian-client</artifactId>
  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <dependency>    
            <groupId>com.caucho</groupId>    
            <artifactId>hessian</artifactId>
            <version>4.0.38</version>
        </dependency>
        <dependency>
            <groupId>${project.parent.groupId}</groupId>
            <artifactId>hessian-interface</artifactId>
            <version>${project.parent.version}</version>
            <type>jar</type>
        </dependency>
    </dependencies>
</project>

2.1.hessian-service的配置文件

server.servlet.context-path=/hessian-server
server.port=8280

2.2.hessian-client的配置文件

server.servlet.context-path=/hessian-client
server.port=8180

3.代码实现
3.1.hessian-interface的统一接口

package com.fracong.hessian.test.myinterface;
public interface HessianTestServiceInterface {
    public String sayTest(String str);
}

3.2.hessian-service的服务注册
3.2.1.注入service,实现hessian-interface中的接口

package com.fracong.hessian.test.service;
​
import org.springframework.stereotype.Service;
​
import com.fracong.hessian.test.myinterface.HessianTestServiceInterface;
​
@Service
public class HessianTestService implements HessianTestServiceInterface{
    @Override
    public String sayTest(String str) {
        return "aaa-"+str;
    }
}

3.2.2.注入service的服务发现

书写TestServiceComponent
package com.fracong.hessian.component;
​
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.remoting.caucho.HessianServiceExporter;
import org.springframework.stereotype.Component;
​
import com.fracong.hessian.test.myinterface.HessianTestServiceInterface;
import com.fracong.hessian.test.service.HessianTestService;
​
@Component
public class TestServiceComponent {
    
    @Autowired
    private HessianTestService hessianTestService;
    
    @Bean(name = "/HessianTestService")
    public HessianServiceExporter accountService() {
        HessianServiceExporter exporter = new HessianServiceExporter();
        exporter.setService(hessianTestService);
        exporter.setServiceInterface(HessianTestServiceInterface.class);
        return exporter;
    }
}


3.3.hessian-client的客户端发现服务
3.3.1.发现服务

package com.fracong.hessian.test.component;
​
import org.springframework.context.annotation.Bean;
import org.springframework.remoting.caucho.HessianProxyFactoryBean;
import org.springframework.stereotype.Component;
​
import com.fracong.hessian.test.myinterface.HessianTestServiceInterface;
​
@Component
public class HessianClientComponent {
    @Bean
    public HessianProxyFactoryBean helloClient() {
        HessianProxyFactoryBean factory = new HessianProxyFactoryBean();
        //该接口的路径,需要和3.2.2中注册的bean名称一致
        factory.setServiceUrl("http://localhost:8280/hessian-server/HessianTestService");
        factory.setServiceInterface(HessianTestServiceInterface.class);
        return factory;
    }
}


3.3.2.服务调用测试

package com.fracong.hessian.test.controller;
​
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
​
import com.fracong.hessian.test.myinterface.HessianTestServiceInterface;
​
@RestController
public class TestController {
    @Autowired
    private HessianTestServiceInterface hessianService;
    @RequestMapping(value="/test/{id}")
    public String test(@PathVariable("id")String id) {
        String sayTest = hessianService.sayTest("aa"+id);
        return sayTest;
    }
}


4.服务启动
4.1.服务端启动

package com.fracong.hessian;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
​
@SpringBootApplication
@ComponentScan("com.fracong") 
public class HessianServiceApplication {
    //启动类入口
    public static void main(String[] args) {
        SpringApplication.run(HessianServiceApplication.class,args);    
    }
}


4.2.客户端启动

package com.fracong.hessian;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
​
@SpringBootApplication
@ComponentScan("com.fracong")
public class HessianClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(HessianClientApplication.class, args);
    }
}


5.测试

http://localhost:8180/hessian-client/test/222
结果:aaa-aa222
  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值