1、后端获取实时天气定时任务实现
This commit is contained in:
parent
a5cd1d7fe1
commit
9bbeb47e53
|
|
@ -8,9 +8,9 @@ import org.springframework.cloud.openfeign.FeignClient;
|
|||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
/**
|
||||
* RPC远程调用文案接口服务
|
||||
* @author xiejs
|
||||
* @desc RPC远程调用文案接口服务
|
||||
* @create 2021-12-27
|
||||
* @since 2021-12-27
|
||||
*/
|
||||
@FeignClient(contextId = "remoteCopyWritingFeign",
|
||||
value = ServiceNameConstants.BUSINESS_OPENAPI_SERVICE,
|
||||
|
|
|
|||
|
|
@ -9,9 +9,9 @@ import org.springframework.web.bind.annotation.GetMapping;
|
|||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
/**
|
||||
* rpc调用翻译字典接口
|
||||
* @author xiejs
|
||||
* @desc rpc调用翻译字典接口
|
||||
* @create 2021-12-30
|
||||
* @since 2021-12-30
|
||||
*/
|
||||
@FeignClient(contextId = "remoteTranDictFeign",
|
||||
value = ServiceNameConstants.BUSINESS_OPENAPI_SERVICE,
|
||||
|
|
|
|||
|
|
@ -9,9 +9,9 @@ import org.springframework.web.bind.annotation.GetMapping;
|
|||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
/**
|
||||
* rpc远程调用其他服务翻译接口
|
||||
* @author xiejs
|
||||
* @desc rpc远程调用其他服务翻译接口
|
||||
* @create 2021-12-29
|
||||
* @since 2021-12-29
|
||||
*/
|
||||
@FeignClient(contextId = "remoteTranslationFeign",
|
||||
value = ServiceNameConstants.BUSINESS_OPENAPI_SERVICE,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
package com.xjs.business.api;
|
||||
|
||||
import com.ruoyi.common.core.constant.ServiceNameConstants;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.xjs.business.api.factory.RemoteWeatherFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
/**
|
||||
* rpc远程调用其他服务天气接口
|
||||
* @author xiejs
|
||||
* @since 2022-01-16
|
||||
*/
|
||||
@FeignClient(contextId = "remoteTWeatherFeign",
|
||||
value = ServiceNameConstants.BUSINESS_OPENAPI_SERVICE,
|
||||
fallbackFactory = RemoteWeatherFactory.class)
|
||||
public interface RemoteWeatherFeign {
|
||||
|
||||
@GetMapping("/weather/getWeatherForRPC")
|
||||
R getWeatherForRPC() ;
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package com.xjs.business.api.factory;
|
||||
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.xjs.business.api.RemoteWeatherFeign;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 内部调用天气服务降级
|
||||
* @author xiejs
|
||||
* @since 2022-01-16
|
||||
*/
|
||||
@Component
|
||||
public class RemoteWeatherFactory implements FallbackFactory<RemoteWeatherFeign> {
|
||||
private static final Logger log = LoggerFactory.getLogger(RemoteWeatherFactory.class);
|
||||
|
||||
@Override
|
||||
public RemoteWeatherFeign create(Throwable cause) {
|
||||
log.error("api模块天气服务调用失败:{}", cause.getMessage());
|
||||
return () -> R.fail("天气服务调用失败" + cause.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
@ -13,9 +13,9 @@ import java.time.LocalDateTime;
|
|||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
/**
|
||||
* 调用文案定时任务
|
||||
* @author xiejs
|
||||
* @desc 调用文案定时任务
|
||||
* @create 2021-12-27
|
||||
* @since 2021-12-27
|
||||
*/
|
||||
@Component("CopyWritingTask")
|
||||
public class CopyWritingTask {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,41 @@
|
|||
package com.xjs.job.task;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.xjs.business.api.RemoteWeatherFeign;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
/**
|
||||
* 调用天气定时任务
|
||||
* @author xiejs
|
||||
* @since 2022-01-16
|
||||
*/
|
||||
@Component("WeatherTask")
|
||||
public class WeatherTask {
|
||||
|
||||
@Resource
|
||||
private RemoteWeatherFeign remoteWeatherFeign;
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(WeatherTask.class);
|
||||
|
||||
/**
|
||||
* 任务执行
|
||||
*/
|
||||
public void execute() {
|
||||
LocalDateTime localDateTime1 = DateUtil.date().toLocalDateTime();
|
||||
log.info("---------------天气定时任务Start-------------------");
|
||||
R r = remoteWeatherFeign.getWeatherForRPC();
|
||||
log.info("天气定时任务结果:code={},msg={},data={}",r.getCode(),r.getMsg(),r.getData());
|
||||
LocalDateTime localDateTime2 = DateUtil.date().toLocalDateTime();
|
||||
long between = ChronoUnit.MILLIS.between(localDateTime1, localDateTime2);
|
||||
log.info("Job耗费时间:{}ms", between);
|
||||
log.info("---------------天气定时任务end---------------------");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package com.xjs.weather.controller;
|
||||
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.ruoyi.common.core.web.domain.AjaxResult;
|
||||
import com.ruoyi.common.log.annotation.Log;
|
||||
import com.ruoyi.common.security.annotation.RequiresLogin;
|
||||
import com.xjs.weather.domain.NowWeather;
|
||||
|
|
@ -13,8 +14,11 @@ import org.springframework.web.bind.annotation.GetMapping;
|
|||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 天气控制器
|
||||
*
|
||||
* @author xiejs
|
||||
* @since 2022-01-16
|
||||
*/
|
||||
|
|
@ -27,11 +31,20 @@ public class WeatherController {
|
|||
@Autowired
|
||||
private WeatherService weatherService;
|
||||
|
||||
|
||||
@GetMapping
|
||||
@ApiOperation("获取天气信息")
|
||||
@Log(title = "获取天气")
|
||||
@RequiresLogin
|
||||
public R<NowWeather> getWeatherApiData() {
|
||||
return R.ok(weatherService.saveNowWeather());
|
||||
public AjaxResult getWeatherApiData() {
|
||||
return AjaxResult.success(weatherService.saveNowWeather());
|
||||
}
|
||||
|
||||
@GetMapping("getWeatherForRPC")
|
||||
@ApiOperation("远程调用获取天气信息ForRPC")
|
||||
public R getWeatherForRPC() {
|
||||
NowWeather nowWeather = weatherService.save();
|
||||
return Objects.nonNull(nowWeather.getCity()) ? R.ok() : R.fail();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,5 +15,11 @@ public interface WeatherService {
|
|||
*/
|
||||
NowWeather saveNowWeather();
|
||||
|
||||
/**
|
||||
* 只保存
|
||||
* @return NowWeather
|
||||
*/
|
||||
NowWeather save();
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.xjs.consts.RedisConst.NOW_WEATHER;
|
||||
|
|
@ -51,5 +52,12 @@ public class WeatherServiceImpl implements WeatherService {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public NowWeather save() {
|
||||
NowWeather nowWeather = Optional.ofNullable(gaodeNowWeatherFactory.weatherApi()).orElseGet(NowWeather::new);
|
||||
nowWeatherMapper.insert(nowWeather);
|
||||
return nowWeather;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue