Spring Boot集成Hprose

一、项目搭建

引入SpringBoot和Hprose相关依赖:



    4.0.0

    com.example
    springboot-demo
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
         
    


    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.hprose
            hprose-java
            2.0.32
        
    

二、服务端

2.1 准备Service

package com.example.service;

/**
 * @Author: 98050
 * @Time: 2019-06-19 20:40
 * @Feature:
 */
public class Service {

    public String sayHello(String name){
        return "hello,"+ name;
    }
}

2.2 准备Servlet

对外暴露服务接口

package com.example.controller;

import com.example.service.Service;
import hprose.common.HproseMethods;
import hprose.server.HproseServlet;

import javax.servlet.annotation.WebServlet;

/**
 * @Author: 98050
 * @Time: 2019-06-19 20:28
 * @Feature:
 */
@WebServlet(urlPatterns = {"/hprose/sayHello"})
public class ServicePublish extends HproseServlet {
    @Override
    protected void setGlobalMethods(HproseMethods methods) {
        super.setGlobalMethods(methods);
        Service service = new Service();
        methods.addMethod("sayHello",service);
    }
}

2.3 扫描Servlet

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

/**
 * @Author: 98050
 * @Time: 2019-05-05 21:28
 * @Feature:
 */
@SpringBootApplication
@ServletComponentScan
public class Application {
    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
    }
}

三、客户端

3.1 准备接口

package com.example.test;

/**
 * @Author: 98050
 * @Time: 2019-06-19 20:18
 * @Feature:
 */
public interface MyService {

    String sayHello(String name);
}

3.2 远程调用

package com.example.test;

import hprose.client.HproseClient;
import hprose.client.HproseHttpClient;

/**
 * @Author: 98050
 * @Time: 2019-06-19 20:15
 * @Feature:
 */
public class Test {

    public static void main(String[] args) {
        HproseClient client = new HproseHttpClient();
        client.useService("http://localhost:8888/hprose/sayHello");

        //通过接口调用
        MyService service = client.useService(MyService.class);
        String content = service.sayHello("jack");
        System.out.println("远程RPC调用返回:" + content);
    }
}

3.3 运行

Spring Boot集成Hprose_第1张图片

你可能感兴趣的:(SpringBoot)