Java实现不同系统之间数据同步传输操作

目录

场景:

实现思想:

关于HttpClient

引入jar包

HttpUtils工具类

核心实现

一:使用微服务架构

二.使用本地

Fegin

一、借助网关断言

二、自定义配置

注意注意!!!


场景:

A系统---->B系统。系统A表数据有审核通过(修改)时,会把这些发生记录的数据同步至B系统。

实现思想:

1.确定系统A数据审核通过

2.建立A系统与B系统之间的联系

3.进行B系统数据新增

本篇文章主要是A系统已经完成数据审核通过操作,在第二步进行不同系统之间建立联系展开操作。

注:此处采用httpClinet!

关于HttpClient

HttpClient是Apache中的一个开源的项目。它实现了HTTP标准中Client端的所有功能,使用它能够很容易地进行HTTP信息的传输。

引入jar包

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>
 
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>

HttpUtils工具类

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * HttpUtils工具类
 * @author 小沈
 * @date 2023/10/26
 */
public class HttpUtils {

	/**
	 * get
	 *用于发送HTTP GET请求
	 * @param host 目标服务器的主机名或IP地址
	 * @param path 请求路径
	 * @param method 请求方式
	 * @param headers 请求头信息,以键值对的形式存储在Map中
	 * @param querys 查询参数,以键值对的形式存储在Map中
	 * @return
	 * @throws Exception
	 */
	public static HttpResponse doGet(String host, String path, String method,
			Map<String, String> headers,
			Map<String, String> querys)
            throws Exception {
    	HttpClient httpClient = wrapClient(host);

    	HttpGet request = new HttpGet(buildUrl(host, path, querys));
        for (Map.Entry<String, String> e : headers.entrySet()) {
        	request.addHeader(e.getKey(), e.getValue());
        }

        return httpClient.execute(request);
    }

	/**
	 * post form
	 *用于发送HTTP POST请求
	 * @param host 目标服务器的主机名或IP地址
	 * @param path 请求路径
	 * @param method 请求方式
	 * @param headers 请求头信息,以键值对的形式存储在Map中
	 * @param querys 查询参数,以键值对的形式存储在Map中
	 * @param bodys  请求体内容,以键值对的形式存储在Map中
	 * @return
	 * @throws Exception
	 */
	public static HttpResponse doPost(String host, String path, String method,
			Map<String, String> headers,
			Map<String, String> querys,
			Map<String, String> bodys)
            throws Exception {
    	HttpClient httpClient = wrapClient(host);

    	HttpPost request = new HttpPost(buildUrl(host, path, querys));
        for (Map.Entry<String, String> e : headers.entrySet()) {
        	request.addHeader(e.getKey(), e.getValue());
        }

        if (bodys != null) {
            List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();

            for (String key : bodys.keySet()) {
                nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
            }
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
            formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
            request.setEntity(formEntity);
        }

        return httpClient.execute(request);
    }

	/**
	 * Post String
	 *用于发送HTTP POST请求
	 * @param host 目标服务器的主机名或IP地址
	 * @param path 请求路径
	 * @param method 请求方式
	 * @param headers 请求头信息,以键值对的形式存储在Map中
	 * @param querys 查询参数,以键值对的形式存储在Map中
	 * @param bodys  请求体内容,以键值对的形式存储在Map中
	 * @return
	 * @throws Exception
	 */
	public static HttpResponse doPost(String host, String path, String method,
			Map<String, String> headers,
			Map<String, String> querys,
			String body)
            throws Exception {
    	HttpClient httpClient = wrapClient(host);

    	HttpPost request = new HttpPost(buildUrl(host, path, querys));
        for (Map.Entry<String, String> e : headers.entrySet()) {
        	request.addHeader(e.getKey(), e.getValue());
        }

        if (StringUtils.isNotBlank(body)) {
        	request.setEntity(new StringEntity(body, "utf-8"));
        }

        return httpClient.execute(request);
    }

	/**
	 * Post stream
	 *用于发送HTTP POST请求
	 * @param host 目标服务器的主机名或IP地址
	 * @param path 请求路径
	 * @param method 请求方式
	 * @param headers 请求头信息,以键值对的形式存储在Map中
	 * @param querys 查询参数,以键值对的形式存储在Map中
	 * @param bodys  请求体内容,以键值对的形式存储在Map中
	 * @return
	 * @throws Exception
	 */
	public static HttpResponse doPost(String host, String path, String method,
			Map<String, String> headers,
			Map<String, String> querys,
			byte[] body)
            throws Exception {
    	HttpClient httpClient = wrapClient(host);

    	HttpPost request = new HttpPost(buildUrl(host, path, querys));
        for (Map.Entry<String, String> e : headers.entrySet()) {
        	request.addHeader(e.getKey(), e.getValue());
        }

        if (body != null) {
        	request.setEntity(new ByteArrayEntity(body));
        }

        return httpClient.execute(request);
    }

	/**
	 * Put String
	 *用于发送HTTP PUT请求
	 * @param host 目标服务器的主机名或IP地址
	 * @param path 请求路径
	 * @param method 请求方式
	 * @param headers 请求头信息,以键值对的形式存储在Map中
	 * @param querys 查询参数,以键值对的形式存储在Map中
	 * @param bodys  请求体内容,以键值对的形式存储在Map中
	 * @return
	 * @throws Exception
	 */
	public static HttpResponse doPut(String host, String path, String method,
			Map<String, String> headers,
			Map<String, String> querys,
			String body)
            throws Exception {
    	HttpClient httpClient = wrapClient(host);

    	HttpPut request = new HttpPut(buildUrl(host, path, querys));
        for (Map.Entry<String, String> e : headers.entrySet()) {
        	request.addHeader(e.getKey(), e.getValue());
        }

        if (StringUtils.isNotBlank(body)) {
        	request.setEntity(new StringEntity(body, "utf-8"));
        }

        return httpClient.execute(request);
    }

	/**
	 * Put stream
	 *用于发送HTTP PUT请求
	 * @param host 目标服务器的主机名或IP地址
	 * @param path 请求路径
	 * @param method 请求方式
	 * @param headers 请求头信息,以键值对的形式存储在Map中
	 * @param querys 查询参数,以键值对的形式存储在Map中
	 * @param bodys  请求体内容,以键值对的形式存储在Map中
	 * @return
	 * @throws Exception
	 */
	public static HttpResponse doPut(String host, String path, String method,
			Map<String, String> headers,
			Map<String, String> querys,
			byte[] body)
            throws Exception {
    	HttpClient httpClient = wrapClient(host);

    	HttpPut request = new HttpPut(buildUrl(host, path, querys));
        for (Map.Entry<String, String> e : headers.entrySet()) {
        	request.addHeader(e.getKey(), e.getValue());
        }

        if (body != null) {
        	request.setEntity(new ByteArrayEntity(body));
        }

        return httpClient.execute(request);
    }

	/**
	 * Delete
	 *用于发送HTTP DELETE请求
	 * @param host 目标服务器的主机名或IP地址
	 * @param path 请求路径
	 * @param method 请求方式
	 * @param headers 请求头信息,以键值对的形式存储在Map中
	 * @param querys 查询参数,以键值对的形式存储在Map中
	 * @return
	 * @throws Exception
	 */
	public static HttpResponse doDelete(String host, String path, String method,
			Map<String, String> headers,
			Map<String, String> querys)
            throws Exception {
    	HttpClient httpClient = wrapClient(host);

    	HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
        for (Map.Entry<String, String> e : headers.entrySet()) {
        	request.addHeader(e.getKey(), e.getValue());
        }

        return httpClient.execute(request);
    }
    	/**
     * 构建一个URL字符串   
	 * @param host 目标服务器的主机名或IP地址
	 * @param path 请求路径
	 * @param querys 查询参数,以键值对的形式存储在Map中
	 * @return {@link String}
	 * @throws UnsupportedEncodingException
	 */
	private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
    	StringBuilder sbUrl = new StringBuilder();
    	sbUrl.append(host);
    	if (!StringUtils.isBlank(path)) {
    		sbUrl.append(path);
        }
    	if (null != querys) {
    		StringBuilder sbQuery = new StringBuilder();
        	for (Map.Entry<String, String> query : querys.entrySet()) {
        		if (0 < sbQuery.length()) {
        			sbQuery.append("&");
        		}
        		if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
        			sbQuery.append(query.getValue());
                }
        		if (!StringUtils.isBlank(query.getKey())) {
        			sbQuery.append(query.getKey());
        			if (!StringUtils.isBlank(query.getValue())) {
        				sbQuery.append("=");
        				sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
        			}
                }
        	}
        	if (0 < sbQuery.length()) {
        		sbUrl.append("?").append(sbQuery);
        	}
        }

    	return sbUrl.toString();
    }
	/**
     * 创建新的DefaultHttpClient对象
	 * @param host 主机名
	 * @return {@link HttpClient}
	 */
	private static HttpClient wrapClient(String host) {
		HttpClient httpClient = new DefaultHttpClient();
		if (host.startsWith("https://")) {
			sslClient(httpClient);
		}

		return httpClient;
	}

	/**
	 * 将传入的HttpClient对象配置为使用SSL/TLS协议进行安全通信
	 * @param httpClient
	 */
	private static void sslClient(HttpClient httpClient) {
        try {
			//创建一个SSLContext实例,使用"TLS"协议
            SSLContext ctx = SSLContext.getInstance("TLS");
            X509TrustManager tm = new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                public void checkClientTrusted(X509Certificate[] xcs, String str) {

                }
                public void checkServerTrusted(X509Certificate[] xcs, String str) {

                }
            };
            ctx.init(null, new TrustManager[] { tm }, null);
            SSLSocketFactory ssf = new SSLSocketFactory(ctx);
            ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            ClientConnectionManager ccm = httpClient.getConnectionManager();
            SchemeRegistry registry = ccm.getSchemeRegistry();
            registry.register(new Scheme("https", 443, ssf));
        } catch (KeyManagementException ex) {
            throw new RuntimeException(ex);
        } catch (NoSuchAlgorithmException ex) {
        	throw new RuntimeException(ex);
        }
    }
}

核心实现

注:此处是A系统完成审核通过操作,返回数据之前与B系统建立联系

一:使用微服务架构

1.首先,在对应微服务的nacos自定义host

2.其次,在代码中进行配置获取

    @Value("${zcwl.url}")
    private  String HOST1;

3.核心代码

        //进行A系统审核通过数据同步B系统
        String host = HOST1;
        System.out.println("同步企业信息==>"+HOST1);
        //B系统接口的路径
        String path = "/busi/api/zcBusiEnterprise/updateOrAdd";
        String method = "POST";
        //用于获取传输数据的地址 校验拼接的host+path是否正确
        /**
        *     StringBuilder sbUrl = new StringBuilder();
        *     sbUrl.append(host);
        *     if (!org.apache.commons.lang.StringUtils.isBlank(path)) {
        *     sbUrl.append(path);
        *     }
        *  log.info("请求路径:{}",sbUrl);
        */
        Map<String, String> headers = new HashMap<>();
        //根据API的要求,定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, String> querys = new HashMap<>();
        String bodys = JSONObject.toJSONString(zcBusiEnterprise);
        System.out.println("入参==>"+bodys);
        HttpResponse response = HttpUtils2.doPost(host, path, method, headers, querys, bodys);
        System.out.println("返回值==>"+response.toString());
        String msg = new String(EntityUtils.toString(response.getEntity()).getBytes(),"UTF-8");
        //转json对象到定义的实体类
        R object = JSONObject.toJavaObject(JSONObject.parseObject(msg), R.class);
        if(object.getCode()!=0){
        System.out.println("返回值2==>"+object.getCode()+"|"+object.getMsg());
               return R.ok(false, "同步B系统异常!");
         }

二.使用本地

        //进行A系统审核通过数据同步B系统
        String host = "https://xxx.com";
        System.out.println("同步企业信息==>"+host);
        //B系统接口的路径
        String path = "/busi/api/zcBusiEnterprise/updateOrAdd";
        String method = "POST";
        //用于获取传输数据的地址 校验拼接的host+path是否正确
        /**
        *     StringBuilder sbUrl = new StringBuilder();
        *     sbUrl.append(host);
        *     if (!org.apache.commons.lang.StringUtils.isBlank(path)) {
        *     sbUrl.append(path);
        *     }
        *  log.info("请求路径:{}",sbUrl);
        */
        Map<String, String> headers = new HashMap<>();
        //根据API的要求,定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, String> querys = new HashMap<>();
        String bodys = JSONObject.toJSONString(zcBusiEnterprise);
        System.out.println("入参==>"+bodys);
        HttpResponse response = HttpUtils2.doPost(host, path, method, headers, querys, bodys);
        System.out.println("返回值==>"+response.toString());
        String msg = new String(EntityUtils.toString(response.getEntity()).getBytes(),"UTF-8");
        //转json对象到定义的实体类
        R object = JSONObject.toJavaObject(JSONObject.parseObject(msg), R.class);
        if(object.getCode()!=0){
        System.out.println("返回值2==>"+object.getCode()+"|"+object.getMsg());
               return R.ok(false, "同步B系统异常!");
         }

Fegin

借助springcloud自带的fegin调用

一、借助网关断言

  1. application.ymlapplication.properties文件中添加以下配置:
    spring:
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848
        gateway:
          routes:
          - id: your_service_id
            uri: lb://your_service_name
            predicates:
            - Path=/your_api_path/**
            filters:
            - name: Cors
              args:
                allowedOrigins: "*"
                allowedMethods: "GET,POST,PUT,DELETE,OPTIONS"
                allowedHeaders: "*"
                allowCredentials: true
                maxAge: 3600
  2. 在需要使用Feign的项目中,创建一个接口并使用@FeignClient注解指定服务名称:
    @FeignClient(name = "your_service_name")
    public interface YourFeignClient {
    
        @GetMapping("/your_api_path/{param}")
        String yourApiMethod(@PathVariable("param") String param);
    }

    二、自定义配置

此处基于nacos自定义host文件,可参考上方截图

@FeignClient(name = "ZsClient",url = "${zcwl.url}",fallbackFactory = RemoteOtherServiceImpl.class)
//@Headers({ "Content-Type: application/json" })
public interface RemoteOtherService {
    /**
     *
     * @param orderId
     * @param from
     * @return
     */
    @GetMapping("/busi/api/order/getOrderById/{orderId}")
    R getOrderById(@PathVariable(value = "orderId") Long orderId, @RequestHeader(SecurityConstants.FROM) String from);

}

注意注意!!!

如何是流的形式参数 需要更换注解

    R uploadFile(@RequestPart("file") MultipartFile file,@RequestHeader(SecurityConstants.FROM) String from);

(1)feign接口多个参数必须加上consumes = MediaType.MULTIPART_FORM_DATA_VALUE,而且对应的file要用@RequestPart
(2)@RequestPart(“file”),这里的“file”定义接口调用方和提供方必须一致
(3)feign实现文件上传还需要引入依赖

<dependency>
 	<groupId>io.github.openfeign.form</groupId>
 	<artifactId>feign-form</artifactId>
 	<version>xxx</version>
</dependency>

<dependency>
 	<groupId>io.github.openfeign.form</groupId>
 	<artifactId>feign-form-spring</artifactId>
 	<version>xxx</version>
</dependency>

到这里结束啦!B系统的接口小伙伴自行测试哟!!!


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值