parent
1a3f292810
commit
b61af0185b
@ -0,0 +1,28 @@
|
||||
package com.xjs.common.client.api.time;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xjs.annotation.ApiLog;
|
||||
import com.xjs.common.client.factory.TimeFeignFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import static com.xjs.consts.ApiConst.TIME;
|
||||
import static com.xjs.consts.ApiConst.TIME_URL;
|
||||
|
||||
/**
|
||||
* 网络时间feign
|
||||
* @author xiejs
|
||||
* @since 2022-02-26
|
||||
*/
|
||||
@FeignClient(name = "timeFeign", url = TIME_URL, fallbackFactory = TimeFeignFactory.class)
|
||||
public interface TimeFeignClient {
|
||||
|
||||
@GetMapping
|
||||
@ApiLog(name = TIME,
|
||||
url = TIME_URL,
|
||||
method = "Get")
|
||||
JSONObject timeApi(@RequestParam(value = "ttd_pid",defaultValue = "pubmatic")String ttd_pid,
|
||||
@RequestParam(value = "fmt",defaultValue = "json")String fmt);
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.xjs.common.client.factory;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.xjs.common.client.api.time.TimeFeignClient;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
|
||||
|
||||
/**
|
||||
* 网络时间feign降级
|
||||
* @author xiejs
|
||||
* @since 2022-02-26
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class TimeFeignFactory implements FallbackFactory<TimeFeignClient> {
|
||||
|
||||
@Override
|
||||
public TimeFeignClient create(Throwable cause) {
|
||||
log.error("时间api接口服务调用失败:{},执行降级处理", cause.getMessage());
|
||||
return ((a,b) -> {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put(DEMOTE_ERROR, R.FAIL);
|
||||
return jsonObject;
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.xjs.time.controller;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.ruoyi.common.core.web.domain.AjaxResult;
|
||||
import com.xjs.time.factory.TimeFactory;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 获取时间控制器
|
||||
* @author xiejs
|
||||
* @since 2022-02-26
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("time")
|
||||
@Api(tags = "业务模块-时间管理")
|
||||
@Log4j2
|
||||
public class TimeController {
|
||||
|
||||
@Autowired
|
||||
private TimeFactory timeFactoryImpl;
|
||||
|
||||
@GetMapping("networkTime")
|
||||
@ApiOperation("获取网络时间")
|
||||
public AjaxResult getNetworkTime() {
|
||||
String time = timeFactoryImpl.getTime();
|
||||
return AjaxResult.success(time);
|
||||
}
|
||||
|
||||
@GetMapping("localhostTime")
|
||||
@ApiOperation("获取本地时间")
|
||||
public AjaxResult getLocalhostTime() {
|
||||
return AjaxResult.success(DateUtil.now());
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.xjs.time.factory;
|
||||
|
||||
/**
|
||||
* 获取时间工厂
|
||||
* @author xiejs
|
||||
* @since 2022-02-26
|
||||
*/
|
||||
|
||||
public interface TimeFactory {
|
||||
|
||||
/**
|
||||
* 获取时间
|
||||
* @return 返回值
|
||||
*/
|
||||
String getTime();
|
||||
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package com.xjs.time.factory.impl;
|
||||
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xjs.common.client.api.time.TimeFeignClient;
|
||||
import com.xjs.exception.ApiException;
|
||||
import com.xjs.time.factory.TimeFactory;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
|
||||
|
||||
/**
|
||||
* 获取时间工厂实现
|
||||
*
|
||||
* @author xiejs
|
||||
* @since 2022-02-26
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class TimeFactoryImpl implements TimeFactory {
|
||||
|
||||
@Autowired
|
||||
private TimeFeignClient timeFeignClient;
|
||||
|
||||
@Override
|
||||
public String getTime() {
|
||||
String ttd_pid = "pubmatic";
|
||||
String fmt = "json";
|
||||
JSONObject jsonObject = timeFeignClient.timeApi(ttd_pid, fmt);
|
||||
if (jsonObject.containsKey(DEMOTE_ERROR)) {
|
||||
throw new ApiException("时间接口调用异常");
|
||||
}
|
||||
|
||||
Date time = jsonObject.getDate("TDID_CREATED_AT");
|
||||
SimpleDateFormat bjSdf = new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
|
||||
TimeZone tz = TimeZone.getTimeZone("GMT+16");
|
||||
TimeZone.setDefault(tz);
|
||||
bjSdf.setTimeZone(tz);
|
||||
return bjSdf.format(time);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue