1、后端获取实时天气定时任务实现

pull/254/head
xjs 4 years ago
parent a5cd1d7fe1
commit 9bbeb47e53

@ -8,9 +8,9 @@ import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
/** /**
* RPC
* @author xiejs * @author xiejs
* @desc RPC * @since 2021-12-27
* @create 2021-12-27
*/ */
@FeignClient(contextId = "remoteCopyWritingFeign", @FeignClient(contextId = "remoteCopyWritingFeign",
value = ServiceNameConstants.BUSINESS_OPENAPI_SERVICE, value = ServiceNameConstants.BUSINESS_OPENAPI_SERVICE,

@ -9,9 +9,9 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
/** /**
* rpc
* @author xiejs * @author xiejs
* @desc rpc * @since 2021-12-30
* @create 2021-12-30
*/ */
@FeignClient(contextId = "remoteTranDictFeign", @FeignClient(contextId = "remoteTranDictFeign",
value = ServiceNameConstants.BUSINESS_OPENAPI_SERVICE, value = ServiceNameConstants.BUSINESS_OPENAPI_SERVICE,

@ -9,9 +9,9 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
/** /**
* rpc
* @author xiejs * @author xiejs
* @desc rpc * @since 2021-12-29
* @create 2021-12-29
*/ */
@FeignClient(contextId = "remoteTranslationFeign", @FeignClient(contextId = "remoteTranslationFeign",
value = ServiceNameConstants.BUSINESS_OPENAPI_SERVICE, 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; import java.time.temporal.ChronoUnit;
/** /**
*
* @author xiejs * @author xiejs
* @desc * @since 2021-12-27
* @create 2021-12-27
*/ */
@Component("CopyWritingTask") @Component("CopyWritingTask")
public class 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; package com.xjs.weather.controller;
import com.ruoyi.common.core.domain.R; 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.log.annotation.Log;
import com.ruoyi.common.security.annotation.RequiresLogin; import com.ruoyi.common.security.annotation.RequiresLogin;
import com.xjs.weather.domain.NowWeather; 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.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.Objects;
/** /**
* *
*
* @author xiejs * @author xiejs
* @since 2022-01-16 * @since 2022-01-16
*/ */
@ -27,11 +31,20 @@ public class WeatherController {
@Autowired @Autowired
private WeatherService weatherService; private WeatherService weatherService;
@GetMapping @GetMapping
@ApiOperation("获取天气信息") @ApiOperation("获取天气信息")
@Log(title = "获取天气") @Log(title = "获取天气")
@RequiresLogin @RequiresLogin
public R<NowWeather> getWeatherApiData() { public AjaxResult getWeatherApiData() {
return R.ok(weatherService.saveNowWeather()); 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(); NowWeather saveNowWeather();
/**
*
* @return NowWeather
*/
NowWeather save();
} }

@ -12,6 +12,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static com.xjs.consts.RedisConst.NOW_WEATHER; 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…
Cancel
Save