2016-08-12 84 views
0

Feign线程安全的实例...?我无法找到任何支持此功能的文档。有没有人认为否则呢?Feign threadsafe ...?

这里是标准的例子贴在github上回购了假死...

interface GitHub { 
    @RequestLine("GET /repos/{owner}/{repo}/contributors") 
    List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo); 
} 

static class Contributor { 
    String login; 
    int contributions; 
} 

public static void main(String... args) { 
    GitHub github = Feign.builder() 
         .decoder(new GsonDecoder()) 
         .target(GitHub.class, "https://api.github.com"); 

    // Fetch and print a list of the contributors to this library. 
    List<Contributor> contributors = github.contributors("netflix", "feign"); 
    for (Contributor contributor : contributors) { 
    System.out.println(contributor.login + " (" + contributor.contributions + ")"); 
    } 
} 

我应该将其更改为以下......是不是线程安全的...?

interface GitHub { 
    @RequestLine("GET /repos/{owner}/{repo}/contributors") 
    List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo); 
} 

static class Contributor { 
    String login; 
    int contributions; 
} 

@Component 
public class GithubService { 

    GitHub github = null; 

    @PostConstruct 
    public void postConstruct() { 
    github = Feign.builder() 
       .decoder(new GsonDecoder()) 
       .target(GitHub.class, "https://api.github.com"); 
    } 

    public void callMeForEveryRequest() { 
    github.contributors... // Is this thread-safe...? 
    } 
} 

对于上面的示例...我使用基于spring的组件来突出显示一个单例。在此先感谢...

回答

1

This讨论似乎表明,它线程安全的。 (关于创建一个效率低下的新对象) 看看源代码,似乎没有任何状态会导致它不安全。这是预期的,因为它是以球衣Target为蓝本。但是,您应该从Feign开发者处获得确认,或者在以不安全的方式使用它之前进行自己的测试并进行审核。

1

我也在寻找,但不幸的是什么都没发现。 Spring配置中提供了唯一的标志。构建器在范围原型中定义为bean,因此不应该是线程安全的。

@Configuration 
public class FooConfiguration { 
    @Bean 
    @Scope("prototype") 
    public Feign.Builder feignBuilder() { 
     return Feign.builder(); 
    } 
} 

参考:http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign-hystrix

+0

建造者成为原型是有意义的,你不这么认为吗?构建器的目标是创建一个新的不可变对象,以非原子方式设置属性。正在构建的对象可以是不可变的,最终的和线程安全的。 – Seagull