parent
8fddea4427
commit
0ee72c1206
@ -0,0 +1,42 @@
|
|||||||
|
package com.xjs.apitools.factory.impl;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.xjs.apitools.domain.ApiNowWeather;
|
||||||
|
import com.xjs.apitools.domain.RequestBody;
|
||||||
|
import com.xjs.apitools.factory.ApiToolsFactory;
|
||||||
|
import com.xjs.common.client.api.roll.RollWeatherFeignClient;
|
||||||
|
import com.xjs.config.RollProperties;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
|
||||||
|
import static com.xjs.consts.ApiConst.ROLL_CODE_SUCCESS;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* roll天气预报api工厂实现
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-01-18
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Log4j2
|
||||||
|
public class RollNowWeatherFactory implements ApiToolsFactory<ApiNowWeather, RequestBody> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RollProperties rollProperties;
|
||||||
|
@Autowired
|
||||||
|
private RollWeatherFeignClient rollWeatherFeignClient;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApiNowWeather apiData(RequestBody requestBody) {
|
||||||
|
requestBody.setApp_id(rollProperties.getApp_id());
|
||||||
|
requestBody.setApp_secret(rollProperties.getApp_secret());
|
||||||
|
JSONObject jsonObject = rollWeatherFeignClient.nowWeatherApi(requestBody, requestBody.getCity());
|
||||||
|
if (!jsonObject.containsKey(DEMOTE_ERROR) && jsonObject.getInteger("code") == ROLL_CODE_SUCCESS.intValue()) {
|
||||||
|
JSONObject jsonData = jsonObject.getJSONObject("data");
|
||||||
|
return jsonData.toJavaObject(ApiNowWeather.class);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.xjs.common.client.api.roll;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.xjs.annotation.ApiLog;
|
||||||
|
import com.xjs.apitools.domain.RequestBody;
|
||||||
|
import com.xjs.common.client.factory.RollWeatherFeignFactory;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.cloud.openfeign.SpringQueryMap;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
|
||||||
|
import static com.xjs.consts.ApiConst.ROLL_WEATHER;
|
||||||
|
import static com.xjs.consts.ApiConst.ROLL_WEATHER_URL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* roll 天气预报api调用feign
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-01-18
|
||||||
|
*/
|
||||||
|
@FeignClient(name = "rollWeather", url = ROLL_WEATHER_URL, fallbackFactory = RollWeatherFeignFactory.class)
|
||||||
|
public interface RollWeatherFeignClient {
|
||||||
|
|
||||||
|
@GetMapping("/current/{city}")
|
||||||
|
@ApiLog(name = ROLL_WEATHER,
|
||||||
|
url = ROLL_WEATHER_URL+"/current",
|
||||||
|
method = "Get")
|
||||||
|
JSONObject nowWeatherApi(@SpringQueryMap RequestBody requestBody, @PathVariable("city")String city);
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/forecast/{city}")
|
||||||
|
@ApiLog(name = ROLL_WEATHER,
|
||||||
|
url = ROLL_WEATHER_URL+"/forecast",
|
||||||
|
method = "Get")
|
||||||
|
JSONObject forecastWeatherApi(@SpringQueryMap RequestBody requestBody,@PathVariable("city")String city);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.xjs.common.client.factory;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.ruoyi.common.core.domain.R;
|
||||||
|
import com.xjs.apitools.domain.RequestBody;
|
||||||
|
import com.xjs.common.client.api.roll.RollWeatherFeignClient;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* roll 天气服务api降级处理
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-01-18
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Log4j2
|
||||||
|
public class RollWeatherFeignFactory implements FallbackFactory<RollWeatherFeignClient> {
|
||||||
|
@Override
|
||||||
|
public RollWeatherFeignClient create(Throwable cause) {
|
||||||
|
return new RollWeatherFeignClient() {
|
||||||
|
@Override
|
||||||
|
public JSONObject nowWeatherApi(RequestBody requestBody,String city) {
|
||||||
|
log.error("api模块roll 实时天气服务调用失败:{},执行降级处理", cause.getMessage());
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
jsonObject.put(DEMOTE_ERROR, R.FAIL);
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JSONObject forecastWeatherApi(RequestBody requestBody, String city) {
|
||||||
|
log.error("api模块roll 预报天气服务调用失败:{},执行降级处理", cause.getMessage());
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
jsonObject.put(DEMOTE_ERROR, R.FAIL);
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue