不同springCloud项目互相调用接口 - Go语言中文社区

不同springCloud项目互相调用接口


有A,B两个springCloud项目,B项目的提供restful接口供A项目调用,B项目不在A的注册中心,其实跟普通的http接口调用方法一样

代码

A项目

1.调用接口类

public static String defaultConnection(String method, String path, int timeout, int readTimeout, String data)
            throws Exception {
        URL url=null;
        URLConnection con=null;
        HttpURLConnection urlCon=null;
        String strResponse=null;
        StringBuilder sb=new StringBuilder();
        int intResponseCode = HttpURLConnection.HTTP_OK;
        OutputStream out=null;
        InputStream input=null;

        //第1步,建立连接
        try {
            url=new URL(path);
            //向某个特定协议对象返回表现http资源连接的引用
            con=url.openConnection();
        }catch(Exception e) {
            e.printStackTrace();
            System.out.println("http连接失败:"+e.getMessage());
        }

        //第2步,验证连接的类型,必须是HttpURLConnection
        if(!(con instanceof HttpURLConnection)){
            System.out.println("http连接失败,连接类型错误");
        }

        //第3步,发送报文
        try {
            //表明程序必须把名称/值对输出到服务器程序资源
            con.setConnectTimeout(timeout == 0 ? 1000 : timeout);
            con.setReadTimeout(readTimeout == 0 ? 1000 : readTimeout);
            con.setRequestProperty("Content-Type","application/json");


            con.setDoOutput(true);
            con.setDoInput(true);
            //表明只能返回有用的信息
            con.setUseCaches(false);
            urlCon=(HttpURLConnection)con;
            //设置HTTP请求方法
            urlCon.setRequestMethod(method);
            //获得输出流对象
            out=urlCon.getOutputStream();
            DataOutputStream dos=new DataOutputStream(out);
            dos.write(data.getBytes("utf-8"));
            dos.flush();
        }catch(Exception e) {
            e.printStackTrace();
            System.out.println("http发送失败:"+e.getMessage());
        }finally{
            out.close();
        }

        //第4步,校验返回状态
        try {
            intResponseCode=urlCon.getResponseCode();
        }catch(Exception e) {
            e.printStackTrace();
            System.out.println("http返回状态失败:"+e.getMessage());
        }
        if(intResponseCode!=HttpURLConnection.HTTP_OK){  //如果不为(HTTP_OK)200,说明服务器返回错误
            System.out.println("request "+ urlCon.getURL() +" fail. response: code="+intResponseCode +
                    ", message="+urlCon.getResponseMessage());
        }


        //第5步,接收报文
        try {
            input=urlCon.getInputStream();
            //将字节流转换为字符流
            BufferedReader br=new BufferedReader(new InputStreamReader(input));
            while((strResponse=br.readLine())!=null){
                sb.append(strResponse);
            }
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("http接收错误:"+e.getMessage());
        }finally{
            input.close();
            //关闭和服务器的连接
            urlCon.disconnect();
        }
        return sb.toString();
    }

2. 调用方法

public void testtoken(){

        String uuid = "{"accessToken":"123456"}";
        System.out.println("uuid=="+uuid);
        String responseEntity = "";
        try {
            responseEntity = defaultConnection("POST", "http://localhost:50666/receiveToken", 10000, 10000, uuid);
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("responseEntity=="+responseEntity);
    }

B项目

@RequestMapping(value = "/receiveToken", method = RequestMethod.POST)
    public void receiveToken(@RequestBody UserParamDto user,HttpServletResponse response) throws Exception {
        System.out.println(user.getAccessToken());
        PrintWriter out = null;
        try {
            out = response.getWriter();
            System.out.println(out);
            out.write("{"loginName":"11223344"}");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

结果

A

B

 

 

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/m0_38072862/article/details/82419103
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-08 16:37:46
  • 阅读 ( 925 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢