Created
January 16, 2018 13:18
-
-
Save ScienJus/a39545cde933648eb60c8589a206f13e to your computer and use it in GitHub Desktop.
Custom hystrix command/group key in feign clients
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.scienjus.feign; | |
import com.netflix.hystrix.HystrixCommand; | |
import com.netflix.hystrix.HystrixCommandGroupKey; | |
import feign.Feign; | |
import feign.hystrix.SetterFactory; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import static com.netflix.hystrix.HystrixCommandKey.Factory.asKey; | |
@Configuration | |
public class FeignHystrixKeyConfiguration { | |
@Bean | |
public SetterFactory setterFactory() { | |
return (target, method) -> { | |
HystrixGroupKey groupKeyAnno = method.getAnnotation(HystrixGroupKey.class); | |
if (groupKeyAnno == null) { | |
groupKeyAnno = method.getDeclaringClass().getAnnotation(HystrixGroupKey.class); | |
} | |
HystrixCommandKey commandKeyAnno = method.getAnnotation(HystrixCommandKey.class); | |
String groupKey = groupKeyAnno != null ? groupKeyAnno.value() : target.name(); | |
String commandKey = commandKeyAnno != null ? commandKeyAnno.value() : Feign.configKey(target.type(), method); | |
return HystrixCommand.Setter | |
.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)) | |
.andCommandKey(asKey(commandKey)); | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.scienjus.feign; | |
import java.lang.annotation.Documented; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target(ElementType.METHOD) | |
@Documented | |
public @interface HystrixCommandKey { | |
String value(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.scienjus.feign; | |
import java.lang.annotation.Documented; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target({ElementType.TYPE, ElementType.METHOD}) | |
@Documented | |
public @interface HystrixGroupKey { | |
String value(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment