装うサービスコンシューマのSpringCloud

プロフィールを装います

装うは、それが簡単にHTTPクライアントを書くことになり、宣言型の擬似HTTPクライアントです。装うを使用して、あなただけがインターフェイスや注釈を作成する必要があります。それは装うJAX-RS注釈やメモをプラグイン可能な注釈機能を使用することができました。プラグ可能なエンコーダおよびデコーダをふり。デフォルトの統合されたリボン、ユーレカと組み合わせて、負荷分散のデフォルトの実装を装います。

  • 装うは、アノテーションベースのインタフェースを使用しています
  • ロード・バランシング機能と、統合されたリボンを装います
  • 統合Hystrix、融合する能力を持っています

書き込みサービスを装います

消費者へのサービスとして、モデル事業、すなわちユーレカ-装う-クライアントを作成します。
輸入依存度

<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> 

プログラムに加えて、クラス@EnableFeignClientsのローンチの時点でオープン装う機能コメント:

1つの インポートorg.springframework.boot.SpringApplication。
2  インポートorg.springframework.boot.autoconfigure.SpringBootApplication。
3  インポートorg.springframework.cloud.client.loadbalancer.LoadBalanced。
4  インポートorg.springframework.cloud.netflix.eureka.EnableEurekaClient。
5  輸入org.springframework.cloud.openfeign.EnableFeignClients。
6  インポートorg.springframework.context.annotation.Bean。
7  インポートorg.springframework.web.client.RestTemplate。
8  
9  @SpringBootApplication
 10  @EnableEurekaClient
 11  //开启ふり的功能
12  @EnableFeignClients
 13  パブリック クラスEurekaFeignClientApplication {
 14  
15      公共 静的 ボイドメイン(文字列[]引数){
 16          SpringApplication.run(EurekaFeignClientApplication。クラス、引数)。
17      }
 18 }
コードの表示

次のようにapplication.ymlプロフィール:

server:
  port: 8083 spring: application: name: eureka-feign-client eureka: client: serviceUrl: defaultZone: http://localhost:9100/eureka/ 

定義されたインタフェースを装う@FeignClient(“服务名”)、サービスコールを指定します。

import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; // 申明这是一个Feign客户端,并且指明服务id @FeignClient(value = "eureka-client") public interface FeignClientInter { // 这里定义了类似于SpringMVC用法的方法,就可以进行RESTful方式的调用了 @GetMapping(value = "/hello") String sayHelloFromClient(); } 

消費者サービスへの上記に定義されたエンドを通じて顧客を装います。

1つの インポートcom.yq.feign.service.FeignClientInter。
2  インポートorg.springframework.beans.factory.annotation.Autowired。
3  輸入org.springframework.web.bind.annotation.GetMapping。
4  インポートorg.springframework.web.bind.annotation.RestController。
5  
6  @RestController
 7つの パブリック クラスHelloController {
 8  
9      @Autowired(必須= 10      FeignClientInter feignClientInter。
11  
12      @GetMapping( "/ふり" 13      公共の文字列デモ(){
14          リターンfeignClientInter.sayHelloFromClient()。
15      }
 16 }

 

テスト:
ここに画像を挿入説明

おすすめ

転載: www.cnblogs.com/const-/p/11454565.html
おすすめ