DataPublisherFactory.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package com.primeton.dsp.dataservice;
  2. import com.alibaba.fastjson.JSON;
  3. import com.primeton.dsp.dataservice.service.IDataPublisherService;
  4. import feign.Feign;
  5. import feign.Request;
  6. import feign.RequestTemplate;
  7. import feign.Response;
  8. import feign.Util;
  9. import feign.codec.Decoder;
  10. import feign.codec.Encoder;
  11. import org.apache.commons.lang.math.NumberUtils;
  12. import java.io.Closeable;
  13. import java.lang.reflect.Method;
  14. import java.lang.reflect.Type;
  15. import java.nio.charset.StandardCharsets;
  16. /**
  17. * 服务发布接口 Factory 实现
  18. */
  19. public class DataPublisherFactory {
  20. private DataPublisherFactory() {
  21. }
  22. /**
  23. * 获取一个新服务
  24. * @param url
  25. * @return
  26. */
  27. public static IDataPublisherService newInstance(String url) {
  28. IDataPublisherService dataPublisherService = Feign.builder()
  29. .encoder(feignEncoder())
  30. .decoder(feignDecoder())
  31. .target(IDataPublisherService.class, url);
  32. return dataPublisherService;
  33. }
  34. /**
  35. * close 服务
  36. * @param closeObj
  37. */
  38. public static void close(Object closeObj) {
  39. if(closeObj instanceof Closeable) {
  40. close((Closeable)closeObj);
  41. return;
  42. }
  43. try {
  44. // 如果有无参数的 close 方法
  45. Method close = closeObj.getClass().getDeclaredMethod("close");
  46. if(close != null) {
  47. close.setAccessible(true);
  48. close.invoke(closeObj);
  49. }
  50. } catch (Exception e) {
  51. }
  52. }
  53. public static void close(Closeable closeable) {
  54. try {
  55. closeable.close();
  56. } catch (Exception e){};
  57. }
  58. /**
  59. * 解码器实现
  60. * @return
  61. */
  62. public static Decoder feignDecoder() {
  63. // JSON
  64. return (Response response, Type type) -> {
  65. Response.Body body = response.body();
  66. if (response.status() == 404)
  67. return Util.emptyValueOf(type);
  68. if (body == null)
  69. return null;
  70. if (byte[].class.equals(type)) {
  71. return Util.toByteArray(response.body().asInputStream());
  72. }
  73. if (String.class == type) {
  74. return new String(Util.toByteArray(response.body().asInputStream()), StandardCharsets.UTF_8);
  75. }
  76. if(Number.class.isAssignableFrom(type.getClass())) {
  77. // 数字类型
  78. String bodyStr = new String(Util.toByteArray(response.body().asInputStream()), StandardCharsets.UTF_8);
  79. return NumberUtils.createNumber(bodyStr).intValue();
  80. }
  81. // JSON 类型或者为对象类型
  82. String bodyStr = new String(Util.toByteArray(response.body().asInputStream()), StandardCharsets.UTF_8);
  83. return JSON.parseObject(bodyStr, type);
  84. };
  85. }
  86. /**
  87. * 接口发送时编码器
  88. * @return
  89. */
  90. public static Encoder feignEncoder() {
  91. // JSON
  92. return (Object object, Type bodyType, RequestTemplate template) -> {
  93. if (bodyType == String.class) {
  94. template.body((String)object);
  95. } else if (bodyType == byte[].class) {
  96. template.body(Request.Body.encoded((byte[]) object, StandardCharsets.UTF_8));
  97. } else if (object instanceof Number) {
  98. // 数字类型
  99. template.body(String.valueOf(object));
  100. }
  101. // 复杂类型,对象类型 转为 JSON 发送
  102. template.body(JSON.toJSONString(object));
  103. };
  104. }
  105. // public static void main(String[] args) {
  106. // IDataPublisherService publisherService = DataPublisherFactory.newInstance("http://localhost:9091/datarelease");
  107. // Map<String, Object> model = new HashMap<>();
  108. // model.put("appCode", "Hello");
  109. // model.put("id", "adfadsfas");
  110. // model.put("code", "CODE");
  111. //
  112. // PubServiceVO model1 = new PubServiceVO();
  113. // model1.setSid("abc123");
  114. // model1.setPublisherModel(model);
  115. // Map<String, Object> res = publisherService.doPubService(model1);
  116. // System.out.println(res);
  117. //
  118. // DataPublisherFactory.close(publisherService);
  119. // }
  120. }