Java Code Examples for com.netflix.hystrix.HystrixCommand#Setter

The following examples show how to use com.netflix.hystrix.HystrixCommand#Setter . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultSetterFactory.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public HystrixCommand.Setter createSetter(FilterInvoker invoker, SofaRequest request) {
    Method clientMethod = request.getMethod();
    if (!SETTER_CACHE.containsKey(clientMethod)) {
        synchronized (DefaultSetterFactory.class) {
            if (!SETTER_CACHE.containsKey(clientMethod)) {
                String interfaceId = invoker.getConfig().getInterfaceId();
                String commandKey = generateCommandKey(interfaceId, request.getMethod());
                HystrixCommand.Setter setter = HystrixCommand.Setter
                    .withGroupKey(HystrixCommandGroupKey.Factory.asKey(interfaceId))
                    .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey));
                SETTER_CACHE.put(clientMethod, setter);
            }
        }
    }
    return SETTER_CACHE.get(clientMethod);
}
 
Example 2
Source File: HystrixCommandInterceptor.java    From micro-service with MIT License 5 votes vote down vote up
private HystrixCommand.Setter configHystrixCommand(String className, String methodName) {
	return HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(className + "Group"))
			.andCommandKey(HystrixCommandKey.Factory.asKey(className + "." + methodName))
			.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(className + "ThreadPool"))
			.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
			.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD))
			.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(10));
}
 
Example 3
Source File: HystrixCommandInterceptor.java    From micro-service with MIT License 5 votes vote down vote up
private HystrixCommand.Setter configHystrixCommand(String className, String methodName) {
	return HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(className + "Group"))
			.andCommandKey(HystrixCommandKey.Factory.asKey(className + "." + methodName))
			.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(className + "ThreadPool"))
			.andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD))
			.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(10));
}
 
Example 4
Source File: HystrixTimeoutManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenSvcTimeoutOf100AndDefaultSettings_whenRemoteSvcExecuted_thenReturnSuccess()
  throws InterruptedException {
    HystrixCommand.Setter config = HystrixCommand
      .Setter
      .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup2"));

    assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(100)).execute(),
      equalTo("Success"));
}
 
Example 5
Source File: HystrixTimeoutManualTest.java    From tutorials with MIT License 5 votes vote down vote up
public String invokeRemoteService(HystrixCommand.Setter config, int timeout)
  throws InterruptedException {
    String response = null;
    try {
        response = new RemoteServiceTestCommand(config,
          new RemoteServiceTestSimulator(timeout)).execute();
    } catch (HystrixRuntimeException ex) {
        System.out.println("ex = " + ex);
    }
    return response;
}
 
Example 6
Source File: SetterFactory.java    From feign with Apache License 2.0 5 votes vote down vote up
@Override
public HystrixCommand.Setter create(Target<?> target, Method method) {
  String groupKey = target.name();
  String commandKey = Feign.configKey(target.type(), method);
  return HystrixCommand.Setter
      .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
      .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey));
}
 
Example 7
Source File: HystrixFilterAsyncTest.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Test
public void testHystrixCircuitBreakerFallback() throws InterruptedException {
    // 强制开启熔断
    SetterFactory setterFactory = new SetterFactory() {

        @Override
        public HystrixCommand.Setter createSetter(FilterInvoker invoker, SofaRequest request) {
            String groupKey = invoker.getConfig().getInterfaceId();
            String commandKey = request.getMethodName() + "_circuit_breaker_test";
            return HystrixCommand.Setter
                .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
                .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey))
                .andCommandPropertiesDefaults(
                    HystrixCommandProperties.defaultSetter().withCircuitBreakerForceOpen(true));
        }
    };

    providerConfig = defaultServer(0);
    providerConfig.export();

    consumerConfig = defaultClient()
        .setTimeout(10000);

    SofaHystrixConfig.registerFallbackFactory(consumerConfig, new HystrixServiceFallbackFactory());
    SofaHystrixConfig.registerSetterFactory(consumerConfig, setterFactory);

    HystrixService hystrixService = consumerConfig.refer();
    //wait server ok
    Thread.sleep(2000);
    for (int i = 0; i < 20; i++) {
        long start = System.currentTimeMillis();
        hystrixService.sayHello("abc", 24);
        String result = (String) SofaResponseFuture.getResponse(10000, true);
        Assert.assertTrue((System.currentTimeMillis() - start) < HYSTRIX_DEFAULT_TIMEOUT);
        Assert.assertEquals(
            "fallback abc from server! age: 24, error: java.lang.RuntimeException",
            result);
        Assert.assertTrue((System.currentTimeMillis() - start) < HYSTRIX_DEFAULT_TIMEOUT);
    }
    // 熔断时服务端不应该接收到任何请求
    Assert.assertEquals(0, ((InvokeCounterHystrixService) providerConfig.getRef()).getExecuteCount());

}
 
Example 8
Source File: HystrixFilterAsyncTest.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Test
public void testHystrixThreadPoolRejectedFallback() throws InterruptedException {
    // 强制开启熔断
    SetterFactory setterFactory = new SetterFactory() {

        @Override
        public HystrixCommand.Setter createSetter(FilterInvoker invoker, SofaRequest request) {
            String groupKey = invoker.getConfig().getInterfaceId() + "thread_pool_rejected";
            String commandKey = request.getMethodName() + "thread_pool_rejected";
            return HystrixCommand.Setter
                .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
                .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionTimeoutEnabled(false))
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(1));
        }
    };

    providerConfig = defaultServer(2000);
    providerConfig.export();

    consumerConfig = defaultClient()
        .setTimeout(10000);

    SofaHystrixConfig.registerFallbackFactory(consumerConfig, new HystrixServiceFallbackFactory());
    SofaHystrixConfig.registerSetterFactory(consumerConfig, setterFactory);

    HystrixService HystrixService = consumerConfig.refer();
    //wait server ok
    Thread.sleep(3000);
    for (int i = 0; i < 20; i++) {
        long start = System.currentTimeMillis();
        HystrixService.sayHello("abc", 24);
        Future future = SofaResponseFuture.getFuture();
        // 第一个请求用于阻塞线程池,其他请求会直接线程池拒绝
        if (i > 0) {
            Assert.assertTrue(future.isDone());
            String result = (String) SofaResponseFuture.getResponse(10000, true);
            Assert.assertEquals(
                "fallback abc from server! age: 24, error: java.util.concurrent.RejectedExecutionException",
                result);
            Assert.assertTrue((System.currentTimeMillis() - start) < HYSTRIX_DEFAULT_TIMEOUT);
        }
    }
    Thread.sleep(3000);
    // 只有第一个线程执行了,所以服务端只会收到一个请求
    Assert.assertEquals(1, ((InvokeCounterHystrixService) providerConfig.getRef()).getExecuteCount());

}
 
Example 9
Source File: SetterFactory.java    From sofa-rpc with Apache License 2.0 2 votes vote down vote up
/**
 * Create a {@link HystrixObservableCommand.Setter} with the given invoker and request
 * @param invoker
 * @param request
 * @return
 */
HystrixCommand.Setter createSetter(FilterInvoker invoker, SofaRequest request);
 
Example 10
Source File: SetterFactory.java    From feign with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a hystrix setter appropriate for the given target and method
 */
HystrixCommand.Setter create(Target<?> target, Method method);