feign.hystrix.HystrixFeign Java Examples

The following examples show how to use feign.hystrix.HystrixFeign. 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: TwitchHelixBuilder.java    From twitch4j with MIT License 8 votes vote down vote up
/**
 * Twitch API Client (Helix)
 *
 * @return TwitchHelix
 */
public TwitchHelix build() {
    log.debug("Helix: Initializing Module ...");

    // Hystrix
    ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds", timeout);
    ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.requestCache.enabled", false);
    ConfigurationManager.getConfigInstance().setProperty("hystrix.threadpool.default.maxQueueSize", getRequestQueueSize());
    ConfigurationManager.getConfigInstance().setProperty("hystrix.threadpool.default.queueSizeRejectionThreshold", getRequestQueueSize());

    // Jackson ObjectMapper
    ObjectMapper mapper = new ObjectMapper();
    // - Modules
    mapper.findAndRegisterModules();

    // Feign
    TwitchHelix client = HystrixFeign.builder()
        .client(new OkHttpClient())
        .encoder(new JacksonEncoder(mapper))
        .decoder(new JacksonDecoder(mapper))
        .logger(new Logger.ErrorLogger())
        .errorDecoder(new TwitchHelixErrorDecoder(new JacksonDecoder()))
        .requestInterceptor(new TwitchHelixClientIdInterceptor(this))
        .options(new Request.Options(timeout / 3, TimeUnit.MILLISECONDS, timeout, TimeUnit.MILLISECONDS, true))
        .retryer(new Retryer.Default(500, timeout, 2))
        .target(TwitchHelix.class, baseUrl);

    return client;
}
 
Example #2
Source File: TwitchMessagingInterfaceBuilder.java    From twitch4j with MIT License 7 votes vote down vote up
/**
 * Twitch API Client (Helix)
 *
 * @return TwitchHelix
 */
public TwitchMessagingInterface build() {
    log.debug("TMI: Initializing Module ...");

    // Hystrix
    ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds", timeout);
    ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.requestCache.enabled", false);
    ConfigurationManager.getConfigInstance().setProperty("hystrix.threadpool.default.maxQueueSize", getRequestQueueSize());
    ConfigurationManager.getConfigInstance().setProperty("hystrix.threadpool.default.queueSizeRejectionThreshold", getRequestQueueSize());

    // Build
    TwitchMessagingInterface client = HystrixFeign.builder()
        .client(new OkHttpClient())
        .encoder(new JacksonEncoder())
        .decoder(new JacksonDecoder())
        .logger(new Logger.ErrorLogger())
        .errorDecoder(new TwitchMessagingInterfaceErrorDecoder(new JacksonDecoder()))
        .logLevel(Logger.Level.BASIC)
        .requestInterceptor(new TwitchClientIdInterceptor(this.clientId, this.userAgent))
        .retryer(new Retryer.Default(1, 10000, 3))
        .options(new Request.Options(5000, TimeUnit.MILLISECONDS, 15000, TimeUnit.MILLISECONDS, true))
        .target(TwitchMessagingInterface.class, baseUrl);

    return client;
}
 
Example #3
Source File: TwitchKrakenBuilder.java    From twitch4j with MIT License 7 votes vote down vote up
/**
 * Twitch API Client (Kraken)
 *
 * @return TwitchKraken
 */
public TwitchKraken build() {
    log.debug("Kraken: Initializing Module ...");

    // Hystrix
    ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds", timeout);
    ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.requestCache.enabled", false);
    ConfigurationManager.getConfigInstance().setProperty("hystrix.threadpool.default.maxQueueSize", getRequestQueueSize());
    ConfigurationManager.getConfigInstance().setProperty("hystrix.threadpool.default.queueSizeRejectionThreshold", getRequestQueueSize());

    // Build
    TwitchKraken client = HystrixFeign.builder()
        .client(new OkHttpClient())
        .encoder(new JacksonEncoder())
        .decoder(new JacksonDecoder())
        .logger(new Logger.ErrorLogger())
        .errorDecoder(new TwitchKrakenErrorDecoder(new JacksonDecoder()))
        .requestInterceptor(new TwitchClientIdInterceptor(this.clientId, this.userAgent))
        .options(new Request.Options(timeout / 3, TimeUnit.MILLISECONDS, timeout, TimeUnit.MILLISECONDS, true))
        .retryer(new Retryer.Default(500, timeout, 2))
        .target(TwitchKraken.class, baseUrl);

    return client;
}
 
Example #4
Source File: AlertsPublisher.java    From hawkular-apm with Apache License 2.0 7 votes vote down vote up
@Asynchronous
public void publish(final Event event) {
    if (BASE_URL == null || BASE_URL.isEmpty()) {
        logger.hawkularServerNotConfigured();
        return;
    }

    if (USERNAME == null || USERNAME.isEmpty()) {
        logger.hawkularServerUsernameNotConfigured();
        return;
    }

    if (PASSWORD == null || PASSWORD.isEmpty()) {
        logger.hawkularServerPasswordNotConfigured();
        return;
    }

    HystrixFeign.builder()
            .requestInterceptor(new BasicAuthRequestInterceptor(USERNAME, PASSWORD))
            .encoder(new JacksonEncoder())
            .decoder(new JacksonDecoder())
            .retryer(new Retryer.Default())
            .target(AlertsService.class, TARGET)
            .addEvent(event);
}
 
Example #5
Source File: HystrixCommandBookConfig.java    From light-reading-cloud with MIT License 6 votes vote down vote up
@Bean
public Feign.Builder bookFeignHystrixBuilder() {
    return HystrixFeign.builder().setterFactory((target, method) -> HystrixCommand.Setter
        // 组
        .withGroupKey(HystrixCommandGroupKey.Factory.asKey(BookClient.class.getSimpleName()))
        .andCommandKey(HystrixCommandKey.Factory.asKey(BookClient.class.getSimpleName()))
        .andCommandPropertiesDefaults(
            // 超时配置
            HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(500)
        )
        .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
                .withAllowMaximumSizeToDivergeFromCoreSize(true)
                .withMaximumSize(5)
                .withCoreSize(3)
                .withMaxQueueSize(30)
        ));
}
 
Example #6
Source File: TracingConfiguration.java    From hola with Apache License 2.0 6 votes vote down vote up
/**
 * This is were the "magic" happens: it creates a Feign, which is a proxy interface for remote calling a REST endpoint with
 * Hystrix fallback support.
 *
 * @return The feign pointing to the service URL and with Hystrix fallback.
 */
@Produces
@Singleton
private AlohaService alohaService(Tracer tracer) {
    // bind current span to Hystrix thread
    TracingConcurrencyStrategy.register();

    return HystrixFeign.builder()
            // Use apache HttpClient which contains the ZipKin Interceptors
            .client(new TracingClient(new ApacheHttpClient(HttpClientBuilder.create().build()), tracer))

            // Bind Zipkin Server Span to Feign Thread
            .logger(new Logger.ErrorLogger()).logLevel(Logger.Level.BASIC)
            .decoder(new JacksonDecoder())
            .target(AlohaService.class,"http://aloha:8080/",
                    () -> Collections.singletonList("Aloha response (fallback)"));
}
 
Example #7
Source File: BladeHystrixTargeter.java    From blade-tool with GNU Lesser General Public License v3.0 5 votes vote down vote up
private <T> T targetWithFallbackFactory(String feignClientName, FeignContext context,
										Target.HardCodedTarget<T> target,
										HystrixFeign.Builder builder,
										Class<?> fallbackFactoryClass) {
	FallbackFactory<? extends T> fallbackFactory = (FallbackFactory<? extends T>)
		getFromContext("fallbackFactory", feignClientName, context, fallbackFactoryClass, FallbackFactory.class);
	return builder.target(target, fallbackFactory);
}
 
Example #8
Source File: BladeFeignAutoConfiguration.java    From blade-tool with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@ConditionalOnProperty("feign.hystrix.enabled")
public Feign.Builder feignHystrixBuilder(
	RequestInterceptor requestInterceptor, Contract feignContract) {
	return HystrixFeign.builder()
		.contract(feignContract)
		.decode404()
		.requestInterceptor(requestInterceptor);
}
 
Example #9
Source File: HystrixFeignTracingTest.java    From feign-opentracing with Apache License 2.0 5 votes vote down vote up
@Override
protected Feign getClient() {
    return feign  = HystrixFeign.builder()
            .client(new TracingClient(new Client.Default(null, null), mockTracer))
            .retryer(new Retryer.Default(100, SECONDS.toMillis(1), FeignTracingTest.NUMBER_OF_RETRIES))
            .build();
}
 
Example #10
Source File: HystrixClient.java    From goodbye with Apache License 2.0 5 votes vote down vote up
public void run() {
    GoodByeService service = HystrixFeign.builder()
        // Target REST resource
        .target(GoodByeService.class,
            // Server
            "http://localhost:8080/",
            // Fallback implemenation
            () -> "Nap response (fallback)");
    // Service invocation
    String result = service.nap();
    System.out.println(String.format("#%s - %s", this.getName(), result));
}
 
Example #11
Source File: TracingConfiguration.java    From ola with Apache License 2.0 5 votes vote down vote up
/**
 *
 * This is were the "magic" happens: it creates a Feign, which is a proxy interface for remote calling a
 * REST endpoint with Hystrix fallback support.
 */
@Bean
public HolaService holaService(Tracer tracer) {
    // bind current span to Hystrix thread
    TracingConcurrencyStrategy.register();

    return HystrixFeign.builder()
            .client(new TracingClient(new ApacheHttpClient(HttpClientBuilder.create().build()), tracer))
            .logger(new Logger.ErrorLogger()).logLevel(Logger.Level.BASIC)
            .decoder(new JacksonDecoder())
            .target(HolaService.class, "http://hola:8080/",
                    () -> Collections.singletonList("Hola response (fallback)"));
}
 
Example #12
Source File: SofaTracerHystrixFeignBuilder.java    From sofa-tracer with Apache License 2.0 4 votes vote down vote up
public static Feign.Builder builder() {
    return HystrixFeign.builder().retryer(Retryer.NEVER_RETRY).client(client());
}
 
Example #13
Source File: BladeHystrixTargeter.java    From blade-tool with GNU Lesser General Public License v3.0 4 votes vote down vote up
private <T> T targetWithFallback(String feignClientName, FeignContext context,
								 Target.HardCodedTarget<T> target,
								 HystrixFeign.Builder builder, Class<?> fallback) {
	T fallbackInstance = getFromContext("fallback", feignClientName, context, fallback, target.type());
	return builder.target(target, fallbackInstance);
}
 
Example #14
Source File: SeataHystrixFeignBuilder.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
static Feign.Builder builder(BeanFactory beanFactory) {
	return HystrixFeign.builder().retryer(Retryer.NEVER_RETRY)
			.client(new SeataFeignClient(beanFactory));
}
 
Example #15
Source File: MSF4JClient.java    From msf4j with Apache License 2.0 4 votes vote down vote up
public HystrixFeign.Builder newHystrixFeignClientBuilder() {
    return HystrixFeign.builder()
            .encoder(encoder)
            .decoder(decoder);
}