Browse Source

尝试使用FeignCLientFallbackFactory方式调用datastatistical

dev_shibei_match
wxz 4 years ago
parent
commit
9eecb70e18
  1. 4
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/ExceptionUtils.java
  2. 4
      epmet-module/data-statistical/data-statistical-client/src/main/java/com/epmet/feign/DataStatisticalOpenFeignClient.java
  3. 2
      epmet-module/data-statistical/data-statistical-client/src/main/java/com/epmet/feign/impl/DataStatisticalOpenFeignClientFallBack.java
  4. 20
      epmet-module/data-statistical/data-statistical-client/src/main/java/com/epmet/feign/impl/DataStatisticalOpenFeignClientFallBackFactory.java
  5. 7
      epmet-module/epmet-job/epmet-job-server/src/main/java/com/epmet/controller/DemoController.java

4
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/ExceptionUtils.java

@ -25,6 +25,10 @@ public class ExceptionUtils {
* @return 返回异常信息
*/
public static String getErrorStackTrace(Exception ex){
return getThrowableErrorStackTrace(ex);
}
public static String getThrowableErrorStackTrace(Throwable ex) {
StringWriter sw = null;
PrintWriter pw = null;
try {

4
epmet-module/data-statistical/data-statistical-client/src/main/java/com/epmet/feign/DataStatisticalOpenFeignClient.java

@ -8,6 +8,7 @@ import com.epmet.dto.group.form.GroupStatsFormDTO;
import com.epmet.dto.screen.form.InitCustomerIndexForm;
import com.epmet.dto.stats.form.CustomerIdAndDateIdFormDTO;
import com.epmet.feign.impl.DataStatisticalOpenFeignClientFallBack;
import com.epmet.feign.impl.DataStatisticalOpenFeignClientFallBackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@ -20,7 +21,8 @@ import org.springframework.web.bind.annotation.RequestBody;
* @author: jianjun liu
*/
//url="http://localhost:8108"
@FeignClient(name = ServiceConstant.DATA_STATISTICAL_SERVER, fallback = DataStatisticalOpenFeignClientFallBack.class)
//@FeignClient(name = ServiceConstant.DATA_STATISTICAL_SERVER, fallback = DataStatisticalOpenFeignClientFallBack.class, url = "http://localhost:6666")
@FeignClient(name = ServiceConstant.DATA_STATISTICAL_SERVER, fallbackFactory = DataStatisticalOpenFeignClientFallBackFactory.class)
public interface DataStatisticalOpenFeignClient {
/**

2
epmet-module/data-statistical/data-statistical-client/src/main/java/com/epmet/feign/impl/DataStatisticalOpenFeignClientFallBack.java

@ -19,7 +19,7 @@ import org.springframework.stereotype.Component;
* @author: jianjun liu
* email:liujianjun@git.elinkit.com.cn
*/
@Component
//@Component
public class DataStatisticalOpenFeignClientFallBack implements DataStatisticalOpenFeignClient {
/**

20
epmet-module/data-statistical/data-statistical-client/src/main/java/com/epmet/feign/impl/DataStatisticalOpenFeignClientFallBackFactory.java

@ -0,0 +1,20 @@
package com.epmet.feign.impl;
import com.epmet.commons.tools.exception.ExceptionUtils;
import com.epmet.feign.DataStatisticalOpenFeignClient;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class DataStatisticalOpenFeignClientFallBackFactory implements FallbackFactory<DataStatisticalOpenFeignClient> {
private DataStatisticalOpenFeignClientFallBack fallback = new DataStatisticalOpenFeignClientFallBack();
@Override
public DataStatisticalOpenFeignClient create(Throwable cause) {
log.error(ExceptionUtils.getThrowableErrorStackTrace(cause));//自己写的工具类
return fallback;
}
}

7
epmet-module/epmet-job/epmet-job-server/src/main/java/com/epmet/controller/DemoController.java

@ -8,8 +8,10 @@
package com.epmet.controller;
import com.epmet.feign.DataStatisticalOpenFeignClient;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
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;
@ -24,10 +26,15 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("demo")
@Api(tags="定时任务")
public class DemoController {
@Autowired
private DataStatisticalOpenFeignClient client;
@GetMapping("testAlarm")
public void testAlarm() {
//for (int i = 0; i < 20; i++) {
log.error("测试消息");
//}
client.agencyGridIssueStats(null);
}
}

Loading…
Cancel
Save