123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- package com.primeton.dsp.dataservice;
- import com.alibaba.fastjson.JSON;
- import com.primeton.dsp.dataservice.service.IDataPublisherService;
- import feign.Feign;
- import feign.Request;
- import feign.RequestTemplate;
- import feign.Response;
- import feign.Util;
- import feign.codec.Decoder;
- import feign.codec.Encoder;
- import org.apache.commons.lang.math.NumberUtils;
- import java.io.Closeable;
- import java.lang.reflect.Method;
- import java.lang.reflect.Type;
- import java.nio.charset.StandardCharsets;
- /**
- * 服务发布接口 Factory 实现
- */
- public class DataPublisherFactory {
- private DataPublisherFactory() {
- }
- /**
- * 获取一个新服务
- * @param url
- * @return
- */
- public static IDataPublisherService newInstance(String url) {
- IDataPublisherService dataPublisherService = Feign.builder()
- .encoder(feignEncoder())
- .decoder(feignDecoder())
- .target(IDataPublisherService.class, url);
- return dataPublisherService;
- }
- /**
- * close 服务
- * @param closeObj
- */
- public static void close(Object closeObj) {
- if(closeObj instanceof Closeable) {
- close((Closeable)closeObj);
- return;
- }
- try {
- // 如果有无参数的 close 方法
- Method close = closeObj.getClass().getDeclaredMethod("close");
- if(close != null) {
- close.setAccessible(true);
- close.invoke(closeObj);
- }
- } catch (Exception e) {
- }
- }
- public static void close(Closeable closeable) {
- try {
- closeable.close();
- } catch (Exception e){};
- }
- /**
- * 解码器实现
- * @return
- */
- public static Decoder feignDecoder() {
- // JSON
- return (Response response, Type type) -> {
- Response.Body body = response.body();
- if (response.status() == 404)
- return Util.emptyValueOf(type);
- if (body == null)
- return null;
- if (byte[].class.equals(type)) {
- return Util.toByteArray(response.body().asInputStream());
- }
- if (String.class == type) {
- return new String(Util.toByteArray(response.body().asInputStream()), StandardCharsets.UTF_8);
- }
- if(Number.class.isAssignableFrom(type.getClass())) {
- // 数字类型
- String bodyStr = new String(Util.toByteArray(response.body().asInputStream()), StandardCharsets.UTF_8);
- return NumberUtils.createNumber(bodyStr).intValue();
- }
- // JSON 类型或者为对象类型
- String bodyStr = new String(Util.toByteArray(response.body().asInputStream()), StandardCharsets.UTF_8);
- return JSON.parseObject(bodyStr, type);
- };
- }
- /**
- * 接口发送时编码器
- * @return
- */
- public static Encoder feignEncoder() {
- // JSON
- return (Object object, Type bodyType, RequestTemplate template) -> {
- if (bodyType == String.class) {
- template.body((String)object);
- } else if (bodyType == byte[].class) {
- template.body(Request.Body.encoded((byte[]) object, StandardCharsets.UTF_8));
- } else if (object instanceof Number) {
- // 数字类型
- template.body(String.valueOf(object));
- }
- // 复杂类型,对象类型 转为 JSON 发送
- template.body(JSON.toJSONString(object));
- };
- }
-
- // public static void main(String[] args) {
- // IDataPublisherService publisherService = DataPublisherFactory.newInstance("http://localhost:9091/datarelease");
- // Map<String, Object> model = new HashMap<>();
- // model.put("appCode", "Hello");
- // model.put("id", "adfadsfas");
- // model.put("code", "CODE");
- //
- // PubServiceVO model1 = new PubServiceVO();
- // model1.setSid("abc123");
- // model1.setPublisherModel(model);
- // Map<String, Object> res = publisherService.doPubService(model1);
- // System.out.println(res);
- //
- // DataPublisherFactory.close(publisherService);
- // }
- }
|