说明:1、百度翻译接口降级处理
This commit is contained in:
parent
074448d44b
commit
0f36d3e0fe
|
|
@ -2,6 +2,7 @@ package com.xjs.common.client;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.xjs.common.aop.ApiLog;
|
import com.xjs.common.aop.ApiLog;
|
||||||
|
import com.xjs.common.client.factory.BaiduFeignFactory;
|
||||||
import com.xjs.translation.domain.qo.translation.BaiDuTranslationQo;
|
import com.xjs.translation.domain.qo.translation.BaiDuTranslationQo;
|
||||||
import org.springframework.cloud.openfeign.FeignClient;
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
|
@ -11,7 +12,7 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||||
* @desc 百度翻译接口api调用
|
* @desc 百度翻译接口api调用
|
||||||
* @create 2021-12-25
|
* @create 2021-12-25
|
||||||
*/
|
*/
|
||||||
@FeignClient(name = "baidu",url = "http://api.fanyi.baidu.com/api/trans/vip/translate?")
|
@FeignClient(name = "baidu",url = "http://api.fanyi.baidu.com/api/trans/vip/translate?",fallbackFactory = BaiduFeignFactory.class)
|
||||||
public interface BaiduFeignClient {
|
public interface BaiduFeignClient {
|
||||||
|
|
||||||
@PostMapping(headers = {"Content-Type=application/x-www-form-urlencoded"})
|
@PostMapping(headers = {"Content-Type=application/x-www-form-urlencoded"})
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||||
* @desc 天行数据朋友圈文案接口api调用
|
* @desc 天行数据朋友圈文案接口api调用
|
||||||
* @create 2021-12-27
|
* @create 2021-12-27
|
||||||
*/
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
@FeignClient(name = "tianXing",url = "http://api.tianapi.com/pyqwenan/index",fallbackFactory = TianXingFeignFactory.class)
|
@FeignClient(name = "tianXing",url = "http://api.tianapi.com/pyqwenan/index",fallbackFactory = TianXingFeignFactory.class)
|
||||||
public interface TianXingFeignClient {
|
public interface TianXingFeignClient {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.xjs.common.client.factory;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.xjs.common.client.BaiduFeignClient;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @desc 百度翻译平台服务降级处理类
|
||||||
|
* @create 2021-12-28
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
@Component
|
||||||
|
public class BaiduFeignFactory implements FallbackFactory<BaiduFeignClient> {
|
||||||
|
@Override
|
||||||
|
public BaiduFeignClient create(Throwable cause) {
|
||||||
|
log.error("英语模块百度翻译服务调用失败:{},执行降级处理", cause.getMessage());
|
||||||
|
return qo -> {
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
//构建一个异常json给下层接口处理
|
||||||
|
jsonObject.put("error", 500);
|
||||||
|
return jsonObject;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,10 +2,7 @@ package com.xjs.common.client.factory;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.xjs.common.client.TianXingFeignClient;
|
import com.xjs.common.client.TianXingFeignClient;
|
||||||
import com.xjs.copywriting.domain.RequestBody;
|
|
||||||
import com.xjs.copywriting.service.CopyWritingService;
|
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
|
@ -17,22 +14,16 @@ import org.springframework.stereotype.Component;
|
||||||
@Log4j2
|
@Log4j2
|
||||||
@Component
|
@Component
|
||||||
public class TianXingFeignFactory implements FallbackFactory<TianXingFeignClient> {
|
public class TianXingFeignFactory implements FallbackFactory<TianXingFeignClient> {
|
||||||
@Autowired
|
|
||||||
private CopyWritingService copyWritingService;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TianXingFeignClient create(Throwable cause) {
|
public TianXingFeignClient create(Throwable cause) {
|
||||||
log.error("英语模块文案服务调用失败:{},执行降级处理", cause.getMessage());
|
log.error("英语模块文案服务调用失败:{},执行降级处理", cause.getMessage());
|
||||||
//没用拉姆达考虑后面该feign接口还会调用其他api接口
|
return requestBody -> {
|
||||||
return new TianXingFeignClient() {
|
JSONObject jsonObject = new JSONObject();
|
||||||
@Override
|
//构建一个异常json给下层接口处理
|
||||||
public JSONObject copyWritingApi(RequestBody requestBody) {
|
jsonObject.put("error", 500);
|
||||||
JSONObject jsonObject = new JSONObject();
|
return jsonObject;
|
||||||
//构建一个异常json给下层接口处理
|
};
|
||||||
jsonObject.put("error", 500);
|
|
||||||
return jsonObject;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,9 @@ public class BaiDuTranslationFactory implements TranslationFactory {
|
||||||
baiDuTranslationQo.setSign(sign);
|
baiDuTranslationQo.setSign(sign);
|
||||||
baiDuTranslationQo.setQ(translationQo.getQ());
|
baiDuTranslationQo.setQ(translationQo.getQ());
|
||||||
JSONObject jsonObject = baiduFeignClient.translationApi(baiDuTranslationQo);
|
JSONObject jsonObject = baiduFeignClient.translationApi(baiDuTranslationQo);
|
||||||
if(Objects.nonNull(jsonObject.getString("error_code"))){
|
System.out.println(jsonObject);
|
||||||
|
//接口内部错误以及网络错误都抛异常
|
||||||
|
if(jsonObject.containsKey("error_code") || jsonObject.containsKey("error")){
|
||||||
throw new ApiException("百度翻译接口调用异常");
|
throw new ApiException("百度翻译接口调用异常");
|
||||||
}
|
}
|
||||||
TranslationVo translationVo = new TranslationVo();
|
TranslationVo translationVo = new TranslationVo();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue