Eureka と Feign Client を組み合わせてリモート通話を実装

        Spring Cloud では、サービス レジストリおよび検出メカニズムとして Eureka を使用する場合、Feign Client を次の手順と組み合わせて使用​​できます。

1. pom.xml 以下の依存関係を導入します。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2. Eurekaクライアントを構成する

        Eureka Client を設定するか application.yml 、  application.properties Eureka に登録します 

spring:
    application:
        name: feign-client-demo

eureka:
    client:
        serviceUrl:
            defaultZone: http://localhost:8761/eureka/

3. 偽クライアントの構成

@SpringBootApplication 次のように、型のメイン クラス         に 注釈を追加して Feign 関数を有効にし、注釈を使用して@EnableFeignClients Feign Client インターフェイスを定義するクラスで @FeignClient 呼び出されるサービスを宣言します。

@SpringBootApplication
@EnableFeignClients
public class FeignClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(FeignClientApplication.class, args);
    }

    @FeignClient(name = "eureka-client-demo")
    public interface DemoClient {
        @RequestMapping(value = "/", method = RequestMethod.GET)
        String home();
    }
}

4. 偽装クライアントに電話をかける

      サービスを呼び出す必要がある場合は、以下に示すように、宣言された Feign Client インターフェイスを挿入してメソッドを呼び出します。

@RestController
public class HomeController {

    @Autowired
    private DemoClient demoClient;

    @GetMapping("/")
    public String home() {
        return demoClient.home();
    }
}

        以上がEurekaでFeign Clientを利用するための基本的な手順ですが、実際の利用プロセスでは自社の業務と組み合わせて具体的な設定や呼び出しを行う必要があります。

おすすめ

転載: blog.csdn.net/friggly/article/details/130602655