栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实现远程调用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java实现远程调用

程序人生
  • 前言
  • 一、什么是远程调用及其意义
  • 二、如何实现远程调用
    • 1.手写工具类(核心)
    • 2.添加本地接口充当远程接口
    • 3.添加实体类
  • 测试


前言

今天呢,打算给小伙伴们带来一个远程调用的工具包以及写了一个样例,原因也是最近自己在用哈哈,方便使用理解以及帮助一下有需要的小伙伴!

一、什么是远程调用及其意义

远程调用也就是调用远程接口,我们都知道接口有自己本地的,也有远程别人写好的,而调用远程接口的就需要使用远程调用啦。个人比较喜欢随意,放荡不羁一点,像概念这东西不太不喜欢引经据典,这里我就随便说几句,有兴趣的朋友可以百度查一下。

二、如何实现远程调用 1.手写工具类(核心)

首先手写了一个工具类,大致是通过java.net.HttpURLConnection建立连接,在用过输入输出流进行数据的传输以及读取,详细的我都在代码中标识好了,小伙伴们可以参考一下,有不明白的欢迎留言讨论

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import sun.net.www.http.HttpCaptureOutputStream;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;


@Component
@EnableScheduling
public class RemoteInvoking {

    
    public static String RemoteInvoking(String url ,String data) {

        String result="";

        try {
            
            URL url1 = new URL(url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url1.openConnection();
            
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            //注意:POST必须大写
            httpURLConnection.setRequestMethod("POST");

            httpURLConnection.setUseCaches(false);
            //重定向无关紧要
            httpURLConnection.setInstanceFollowRedirects(true);

            httpURLConnection.setRequestProperty("Content-Type","application/json");

            httpURLConnection.connect();

            //通过输入流输入
            DataOutputStream out = new DataOutputStream(httpURLConnection.getOutputStream());
            out.write(data.getBytes("UTF-8"));
            out.flush();
            out.close();   //记得关闭连接

            //通过输出流打印输出
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
            String b="";
            StringBuilder sbu = new StringBuilder();
            while ((b=bufferedReader.readLine())!=null) {
                sbu.append(b);
            }
             result = sbu.toString();

        bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return result;
    }

    
    @Scheduled(cron = "* 0/1 * * * ?")
    public void test() {

        String str = "程序人生,真实的程序,扯淡的人生哈哈,小伙伴们继续加油,再接再厉哈";
        String s = RemoteInvoking("http://127.0.0.1:8089/Big/test/remoteTest", str);
        System.out.println(s);

    }



}

2.添加本地接口充当远程接口

哈哈,由于没有其他的远程接口,公司的也不方便透漏,在这里就自己本地手写了一个简单的接口测试一波,和真实远程接口大同小异,只需替换ip即可。

import com.trs.GoogNight;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RequestMapping("test")
@RestController
public class RemoteInvokingTestController {

    @PostMapping("remoteTest")
    public GoogNight test(@RequestBody String say) {

        return new GoogNight().setName("B哥").setWantToSay(say);
    }

}
3.添加实体类

最后添加实体类存储我们想要的对象信息就可以啦,哈哈,打完收工。

import lombok.Data;
import lombok.experimental.Accessors;


@Data
@Accessors(chain = true)
public class GoogNight {

    
    private String name;

    
    private String wantToSay;


}


测试

最后话不多说,上结果图:不负所望,成功读取,新的一天也要加油哦,小伙伴们GoodNight!
下面附上完整代码,有兴趣的小伙伴们可以尝试尝试,内容比较实用,有问题底部评论区留言哦!

工具类

package com.trs.remoteInvoking;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import sun.net.www.http.HttpCaptureOutputStream;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;


@Component
@EnableScheduling
public class RemoteInvoking {

    
    public static String RemoteInvoking(String url ,String data) {

        String result="";

        try {
            
            URL url1 = new URL(url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url1.openConnection();
            
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            //注意:POST必须大写
            httpURLConnection.setRequestMethod("POST");

            httpURLConnection.setUseCaches(false);
            //重定向无关紧要
            httpURLConnection.setInstanceFollowRedirects(true);

            httpURLConnection.setRequestProperty("Content-Type","application/json");

            httpURLConnection.connect();

            //通过输入流输入
            DataOutputStream out = new DataOutputStream(httpURLConnection.getOutputStream());
            out.write(data.getBytes("UTF-8"));
            out.flush();
            out.close();   //记得关闭连接

            //通过输出流打印输出
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
            String b="";
            StringBuilder sbu = new StringBuilder();
            while ((b=bufferedReader.readLine())!=null) {
                sbu.append(b);
            }
             result = sbu.toString();

        bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return result;
    }

    
    @Scheduled(cron = "* 0/1 * * * ?")
    public void test() {

        String str = "程序人生,真实的程序,扯淡的人生哈哈,小伙伴们继续加油,再接再厉哈";
        String s = RemoteInvoking("http://127.0.0.1:8089/Big/test/remoteTest", str);
        System.out.println(s);

    }



}



测试controller类

package com.trs.controller;

import com.trs.GoogNight;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RequestMapping("test")
@RestController
public class RemoteInvokingTestController {

    @PostMapping("remoteTest")
    public GoogNight test(@RequestBody String say) {

        return new GoogNight().setName("B哥").setWantToSay(say);
    }

}

实体类

package com.trs;

import lombok.Data;
import lombok.experimental.Accessors;


@Data
@Accessors(chain = true)
public class GoogNight {

    
    private String name;

    
    private String wantToSay;


}



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/342921.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号