2016-08-12 55 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