当前位置: 首页 > 知识库问答 >
问题:

javanica中不在spring-boot java应用程序中运行的Hystrix异步方法

沈英勋
2023-03-14

我正在使用spring-cloud-starter(即..带有所有微服务特性的spring boot)。当我在一个使用javanica@hystrixCommand注释的组件中创建hystrix方法时,请按照javanica github站点(https://github.com/netflix/hystrix/tree/master/hystrix-contrib/hystrix-javanica)上的说明使该方法异步运行,不管我是使用它们的“future<>”还是使用反应执行“observable<>”,都不会运行/执行任何东西,并且只要我尝试拉出结果(在future<>的情况下)或获得回调(在反应执行的情况下),就会得到
java.lang.ClassCastException:

public class Application { ...
}
@RestController
@RequestMapping(value = "/makebunchofcalls/{num}")
class EricController { ..

    @RequestMapping(method={RequestMethod.POST})
    ArrayList<Eric> doCalls(@PathVariable Integer num) throws IOException {
        ArrayList<Eric> ale = new ArrayList<Eric>(num);
        for (int i =0; i<num; i++) {
            rx.Observable<Eric> oe = this.ericComponent.doRestTemplateCallAsync(i);
            oe.subscribe(new Action1<Eric>() {
                @Override
                public void call(Eric e) {  // AT RUNTIME, ClassCastException
                    ale.add(e);
                }
            });
        }

        return ale;
    }

@Component
class EricComponent { ...

    // async version =========== using reactive execution via rx library from netflix ==============

    @HystrixCommand(fallbackMethod = "defaultRestTemplateCallAsync", commandKey = "dogeAsync")
    public rx.Observable<Eric> doRestTemplateCallAsync(int callNum) {
        return new ObservableResult<Eric>() {
            @Override
            public Eric invoke() {  // NEVER CALLED
                try {
                    ResponseEntity<String> result = restTemplate.getForEntity("http://doges/doges/24232/photos", String.class);  // actually make a call
                    System.out.println("*************** call successfull: " + new Integer(callNum).toString() + " *************");
                } catch (Exception ex) {
                    System.out.println("=============== call " + new Integer(callNum).toString() + " not successfull: " + ex.getMessage() + " =============");
                }
                return new Eric(new Integer(callNum).toString(), "ok");
            }
        };
    }

    public rx.Observable<Eric> defaultRestTemplateCallAsync(int callNum) {
        return new ObservableResult<Eric>() {
            @Override
            public Eric invoke() {
                System.out.println("!!!!!!!!!!!!! call bombed " + new Integer(callNum).toString() + "!!!!!!!!!!!!!");
                return new Eric(new Integer(callNum).toString(), "bomb");
            }
        };
    }
}

为什么我会拿回ericcomponent$1而不是eric?顺便说一句,eric只是一个包含2个字符串的简单类...它被省略了。

我想我必须显式执行,但这暗示了我,因为:1)使用future<>队列()方法执行时,不像文档所说的那样可用;2)使用observable<>执行时,我确实没有方法执行它。


共有1个答案

强化
2023-03-14

应用程序类上是否有@enablehystrix注释?

subscribe方法是异步的,您正在尝试在同步控制器方法中填充列表,因此可能存在问题。您能否将subscribe更改为ToBlockingObServable().foreach(),看看这是否有帮助?

更新#1我可以复制。默认方法不应返回可观察的 ,而应返回eric

public Eric defaultRestTemplateCallAsync(final int callNum) {
    System.out.println("!!!!!!!!!!!!! call bombed " + new Integer(callNum) + "!!!!!!!!!!!!!");
    return new Eric(new Integer(callNum).toString(), "bomb");
}

更新#2请参阅我的代码https://github.com/spencergibb/communityanswers/tree/so26372319

更新#3当我注释掉FallbackMethod属性时,它抱怨找不到AOP的ericComponent的公共版本。我使ericcomponentpublic static,它工作了。一个顶级类在它自己的文件中可以工作。上面链接的我的代码可以工作(假设restTemplate调用可以工作),并返回n OK

 类似资料:
  • 我基于@SpencerGibb feign-eureka spring cloud starter示例构建了一个超级简单的Hystrix短路示例。一开始,我以为我不能让hystrix javanica默认的fallbackMethod触发由于虚假…现在,除去feign,hystrix默认的fallbackMethod仍然不能捕获异常。 pom.xml 主文件: java(创建HelloClient

  • javanica 是 Hystrix 开源社区贡献的一个类库。 Java 语言相比其他语言有一些比较 great 的优点,那就是反射(refleaction)和注解(annotation)。在传统的使用 Hystrix 时,你需要编写大量的代码,这显然对开发者并不友好,也会制约 Hystrix 未来的发展。这种模式下,你需要花很长时间编写一些 Hystrix commands。Javanica 项

  • 我正试图将Hystrix javanica集成到我现有的java EJB web应用程序中,在运行它时面临两个问题。 > 当我尝试调用以下服务时,它总是从回退方法返回响应,我看到回退方法中的Throwable对象有com.netflix.hystrix.exception.HystrixTimeoutExc0019异常。 每次触发此服务时,都会多次调用HystrixComad和回退方法约50次。

  • 嘿,我是新来的Spring,并试图在我的Applications.java.中运行多个运行方法 当我试图运行这个我得到一个异常。 有没有办法在main中同时调用这两个run方法? -StackTrace

  • 问题内容: 我有一个现有的Flask应用程序,并且想找到通往另一个应用程序的路线。更具体地说,第二个应用程序是Plotly Dash应用程序。如何在现有的Flask应用程序中运行Dash应用程序? 我还尝试将路由添加到Dash实例,因为它是Flask应用程序,但出现错误: 问题答案: 从文档: 基本的Flask应用程序可从访问app.server。 你还可以将自己的Flask应用实例传递到Dash

  • 我试图在SpringMVC中运行SpringBoot应用程序,在SpringMVCPOM中添加SpringBoot应用程序依赖项,并扫描SpringBoot包,但我面临以下问题