Commit 90663abd by jx-art

调用切换成rpc调用 再次测试邮箱发送

parent cb3e5bd7
package com.netease.mail.yanxuan.change.integration.email.conig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.netease.mail.yanxuan.change.integration.email.exception.RpcException;
import com.netease.mail.yanxuan.change.integration.email.exception.RpcStatusException;
import com.netease.mail.yanxuan.change.integration.email.exception.RpcTimeoutException;
import com.netease.mail.yanxuan.change.integration.email.util.EncodeUtil;
import com.netease.mail.yanxuan.change.integration.email.util.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.util.Map;
/**
* rpc 请求封装,在 HttpClientUtil 基础上加入了返回值处理和日志
*
* @author lwtang
* @date 2019-02-19
*/
@Component
public class RpcTemplate {
private Logger logger = LoggerFactory.getLogger(RpcTemplate.class);
@Value("${act.rpc.get.retry:1}")
private int rpcGetRetry;
@Value("${act.rpc.post.retry:0}")
private int rpcPostRetry;
public RpcTemplate() {
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
}
private String convertParam(Object param) {
if (param instanceof String) {
return (String) param;
}
String reqParam = "";
if (param instanceof Map) {
reqParam = EncodeUtil.encodeMap((Map) param);
} else if (param != null) {
reqParam = JSON.toJSONString(param);
}
return reqParam;
}
private <T> T execute(String url, Object params, CallHandler<T> handler,
CallTemplate callTemplate, int retryTime, Method method) {
int tryTimes = 0;
RpcException rpcException = null;
String respBody = null;
int statusCode = 200;
while (tryTimes <= retryTime) {
long startTime = System.currentTimeMillis();
try {
respBody = callTemplate.executeCall();
if (StringUtils.isEmpty(respBody)) {
throw new RpcException("response empty");
}
T t = handler.handle(respBody);
return t;
} catch (RpcTimeoutException e) {
rpcException = e;
} catch (RpcStatusException e) {
rpcException = e;
statusCode = e.getCode();
} catch (RpcException e) {
rpcException = e;
} catch (Exception e) {
rpcException = new RpcException(e);
} finally {
if (logger.isDebugEnabled()) {
long costTime = System.currentTimeMillis() - startTime;
String reqParam = convertParam(params);
logger.debug(
"process request, url={}, method={}, params={}, statusCode = {}, response={}, cost={}ms",
url, method.name(), reqParam, statusCode, respBody, costTime);
}
}
tryTimes++;
}
logger.error("request error, url={}, params={}, statusCode = {}, respBody={}", url,
convertParam(params), statusCode, respBody, rpcException);
if (rpcException == null) {
rpcException = new RpcException("服务异常");
}
throw rpcException;
}
/**
* Get 请求
*
* @param url
* @param params
* @param header
* @param timeout
* -1 取默认的超时时间
* @param handler
* @param <T>
* @return
*/
public <T> T get(String url, Map<String, Object> params, Map<String, String> header,
int timeout, CallHandler<T> handler, Integer retryTimes) {
CallTemplate template = () -> HttpClientUtil.get(url, params, header, timeout);
return this.execute(url, params, handler, template, retryTimes, Method.GET);
}
public <T> T get(String url, Map<String, Object> params, CallHandler<T> handler) {
return get(url, params, null, -1, handler, rpcGetRetry);
}
/**
* Post请求(url encoded)
*
* @param url
* @param params
* @param timeout
* @param handler
* @param <T>
* @return
*/
public <T> T post(String url, Map<String, Object> params, int timeout, CallHandler<T> handler,
Integer retryTime) {
CallTemplate template = () -> HttpClientUtil.post(url, params, timeout);
return this.execute(url, params, handler, template, retryTime, Method.POST);
}
/**
* Post请求(url encoded)
*
* @param url
* @param params
* @param timeout
* @param handler
* @param <T>
* @return
*/
public <T> T post(String url, Map<String, Object> params, int timeout, CallHandler<T> handler) {
CallTemplate template = () -> HttpClientUtil.post(url, params, timeout);
return this.execute(url, params, handler, template, rpcPostRetry, Method.POST);
}
/**
* Post Json请求
*
* @param url
* @param params
* @param timeout
* @param handler
* @param <T>
* @return
*/
public <T> T postJson(String url, String params, int timeout, CallHandler<T> handler,
Integer retryTime) {
CallTemplate template = () -> HttpClientUtil.postJson(url, params, timeout);
return this.execute(url, params, handler, template, retryTime, Method.POST);
}
/**
* Post Json请求
*
* @param url
* @param params
* @param timeout
* @param handler
* @param <T>
* @return
*/
public <T> T postJson(String url, String params, int timeout, CallHandler<T> handler) {
CallTemplate template = () -> HttpClientUtil.postJson(url, params, timeout);
return this.execute(url, params, handler, template, rpcPostRetry, Method.POST);
}
enum RpcStatus {
/**
* 成功
*/
SUCCESS(1),
/**
* 告警(请求成功,但非理想值)
*/
WARNING(2),
/**
* 异常
*/
EXCEPTION(3),
/**
* 超时
*/
TIMEOUT(4);
private int value;
RpcStatus(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
enum Method {
/**
* Get请求
*/
GET,
/**
* Post表单请求
*/
POST,
}
/**
* 模板调用
*/
@FunctionalInterface
public interface CallTemplate {
/**
* 模板调用类
*/
String executeCall();
}
/**
* 报文解析
*/
@FunctionalInterface
public interface CallHandler<T> {
/**
* 报文解析
*
* @param resp
* response body
*/
T handle(String resp) throws IOException;
/**
* 校验数据体
*
* @param t
* @return
*/
default boolean check(T t) {
return true;
}
}
}
package com.netease.mail.yanxuan.change.integration.email.email;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netease.mail.yanxuan.change.integration.email.conig.RpcTemplate;
import com.netease.mail.yanxuan.change.common.bean.ResponseCode;
import com.netease.mail.yanxuan.change.integration.email.exception.RpcException;
import java.io.IOException;
public class EmailTemplateRpcResult<T> implements RpcTemplate.CallHandler<T> {
private static final String CODE = "code";
private static final String RESULT = "data";
private static final String MESSAGE = "errorMessage";
private Class<T> tClass;
public EmailTemplateRpcResult(Class<T> tClass){
this.tClass = tClass;
}
@Override
public T handle(String resp) throws IOException {
JSONObject rpcResult = JSON.parseObject(resp);
EmailTemplateResponResult responResult = new EmailTemplateResponResult();
int code = rpcResult.getIntValue(CODE);
if(code == ResponseCode.SUCCESS.getCode()){
String data = rpcResult.getString(RESULT);
responResult.setData(data);
return (T) responResult;
}
String errorMessage = rpcResult.getString(MESSAGE);
throw new RpcException("email template response error, code : " + code + " , errorMessage : " + errorMessage);
}
}
package com.netease.mail.yanxuan.change.integration.email.service.impl; package com.netease.mail.yanxuan.change.integration.email.service.impl;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.netease.mail.yanxuan.change.integration.email.conig.RpcTemplate;
import com.netease.mail.yanxuan.change.integration.email.email.EmailTemplateResponResult; import com.netease.mail.yanxuan.change.integration.email.email.EmailTemplateResponResult;
import com.netease.mail.yanxuan.change.integration.email.email.EmailTemplateRpcResult; import com.netease.mail.yanxuan.change.integration.flow.email.EmailTemplateRpcService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.HashMap; import java.util.HashMap;
...@@ -14,22 +12,21 @@ import java.util.Map; ...@@ -14,22 +12,21 @@ import java.util.Map;
@Service @Service
public class EmailTemplateServiceImpl { public class EmailTemplateServiceImpl {
private static final String TEST_URL = "http://127.0.0.1:8550/proxy/test.yanxuan-template.service.mailsaas/templates/flowx/template/assemble.json"; /**
* 调用链接
private static final String ONELINE_URL = "http://127.0.0.1:8550/proxy/online.yanxuan-template.service.mailsaas/templates/flowx/template/assemble.json"; * private static final String TEST_URL = "http://127.0.0.1:8550/proxy/test.yanxuan-template.service.mailsaas/templates/flowx/template/assemble.json";
* private static final String ONELINE_URL = "http://127.0.0.1:8550/proxy/online.yanxuan-template.service.mailsaas/templates/flowx/template/assemble.json";
//@Value("${rpc.url.email.template}") */
private String url = "http://127.0.0.1:8550/proxy/test.yanxuan-template.service.mailsaas/templates/flowx/template/assemble.json";
@Autowired @Autowired
private RpcTemplate rpcTemplate; private EmailTemplateRpcService service;
public String template(String templateCode, Object mockData, String type) { public String template(String templateCode, Object mockData, String type) {
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();
params.put("mockData", mockData); params.put("mockData", mockData);
params.put("templateCode", templateCode); params.put("templateCode", templateCode);
params.put("type", type); params.put("type", type);
EmailTemplateResponResult emailTemplateResponResult = rpcTemplate.postJson(url, new JSONObject(params).toJSONString(), 500, new EmailTemplateRpcResult<>(EmailTemplateResponResult.class)); EmailTemplateResponResult emailTemplateResponResult = service.template(JSONObject.toJSONString(params));
return emailTemplateResponResult.getData(); return emailTemplateResponResult.getData();
} }
} }
...@@ -20,8 +20,6 @@ import com.alibaba.fastjson.JSONObject; ...@@ -20,8 +20,6 @@ import com.alibaba.fastjson.JSONObject;
@Service @Service
public class IUasClientEhcImpl implements IUasEhcClient { public class IUasClientEhcImpl implements IUasEhcClient {
//请求地址 http://127.0.0.1:8550/proxy/online.logistics-uas.service.mailsaas/push/email/yanxuan-sc-ehc
public static final String CODE = "code"; public static final String CODE = "code";
private Logger logger = LoggerFactory.getLogger(IUasClientEhcImpl.class); private Logger logger = LoggerFactory.getLogger(IUasClientEhcImpl.class);
...@@ -34,10 +32,6 @@ public class IUasClientEhcImpl implements IUasEhcClient { ...@@ -34,10 +32,6 @@ public class IUasClientEhcImpl implements IUasEhcClient {
List<File> fileList) { List<File> fileList) {
if (!CollectionUtils.isEmpty(toList)) { if (!CollectionUtils.isEmpty(toList)) {
Map<String, String> params = new HashMap(); Map<String, String> params = new HashMap();
logger.error("参数查看"+subject);
if (subject == null || subject.equals("")){
subject = "测试标题";
}
params.put("subject", subject); params.put("subject", subject);
params.put("content", content); params.put("content", content);
if (!CollectionUtils.isEmpty(toList)) { if (!CollectionUtils.isEmpty(toList)) {
......
...@@ -27,15 +27,8 @@ public class UasClientServiceImpl implements UasClientService { ...@@ -27,15 +27,8 @@ public class UasClientServiceImpl implements UasClientService {
@Autowired @Autowired
private EmailRpcService service; private EmailRpcService service;
/*
*
* emailPath=/push/email/
* rpcConfig.getUasApi()=http://127.0.0.1:8550/proxy/online.logistics-uas.service.mailsaas
* UPLOAD_FILE /push/email/attach/{0}/upload.json
*/
public void sendEmail(String subject, String content, Collection<String> toList, Collection<String> ccList, List<File> fileList) { public void sendEmail(String subject, String content, Collection<String> toList, Collection<String> ccList, List<File> fileList) {
logger.info("调用接口111~~~~~~~~~~~~~~~~~~~~~~~~");
if (!CollectionUtils.isEmpty(toList)) { if (!CollectionUtils.isEmpty(toList)) {
//String url = MessageFormat.format("http://{0}{1}/{2}", this.thirdPartyDrmConfig.getUasHostInner(), "/push/email/", this.thirdPartyDrmConfig.getUasProduct()); //String url = MessageFormat.format("http://{0}{1}/{2}", this.thirdPartyDrmConfig.getUasHostInner(), "/push/email/", this.thirdPartyDrmConfig.getUasProduct());
Map<String, String> params = new HashMap(); Map<String, String> params = new HashMap();
...@@ -48,7 +41,6 @@ public class UasClientServiceImpl implements UasClientService { ...@@ -48,7 +41,6 @@ public class UasClientServiceImpl implements UasClientService {
} }
logger.info(JSONObject.toJSONString(params)); logger.info(JSONObject.toJSONString(params));
String res = service.sendEmail(params); String res = service.sendEmail(params);
this.logger.info("返回信息日志打印\n"+res);
this.logger.info("[op:sendEmail] res={} ", new Object[]{res}); this.logger.info("[op:sendEmail] res={} ", new Object[]{res});
JSONObject jsonObject = JSON.parseObject(res); JSONObject jsonObject = JSON.parseObject(res);
if (jsonObject.getInteger("code") != 200) { if (jsonObject.getInteger("code") != 200) {
......
...@@ -13,10 +13,8 @@ import java.util.Map; ...@@ -13,10 +13,8 @@ import java.util.Map;
@Service @Service
@MissaClient(serviceCode = "logistics-uas") @MissaClient(serviceCode = "logistics-uas")
public interface EmailRpcService { public interface EmailRpcService {
//templates/flowx/template/assemble.json
//yanxuan-qc-change-system @PostMapping("/push/email/attach/yanxuan-qc-change-system/upload.json")
//@PostMapping("/push/email/attach/yanxuan-qc-change-system/upload.json")
@PostMapping("/push/email/attach/yanxuan-qc-ecr-system/upload.json")
String sendEmail(Map<String, String> params); String sendEmail(Map<String, String> params);
//yanxuan-sc-ehc //yanxuan-sc-ehc
......
package com.netease.mail.yanxuan.change.integration.flow.email;
import com.netease.mail.yanxuan.change.integration.email.email.EmailTemplateResponResult;
import com.netease.yanxuan.missa.client.annotation.MissaClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
/**
* @author WangJiaXiang
* @date 2022/11/29/029$
*/
@Service
@MissaClient(serviceCode = "yanxuan-template")
public interface EmailTemplateRpcService {
@PostMapping("/templates/flowx/template/assemble.json")
EmailTemplateResponResult template(String json);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment