2016-08-12 2 views
0

Feignスレッドのインスタンスは安全ですか?私はこれをサポートする文書を見つけることができませんでした。そこにいる人は他に何か考えますか?ここでFeignはスレッドセーフなのですか?

を装うための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...? 
    } 
} 

上記の例では、私はシングルトンを強調するために春に基づいたコンポーネントを使用しました。事前にありがとうございます...

答えて

1

This議論は、それスレッドセーフであることを示唆しているようです。 (新しいオブジェクトを非効率的に作成することについての話) ソースを見て、それを危険にするような状態はないようです。ジャージーターゲットをモデルにしているため、これは予想されます。しかし、安全でない方法で使用する前に、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

ビルダーがプロトタイプであることは意味があると思いますか? Builderの目標は、非永続的にプロパティを設定して新しい不変オブジェクトを作成することです。構築されているオブジェクトは、不変、最終、スレッドセーフである可能性があります。 – Seagull

関連する問題