マイクロスプリングクラウドサービス間のコール

サービスおよびサービスの呼び出しの問題を解決するためにSpringCloud、2つの方法を提供します。RestTemplateと装います。HTTPの方向通話を使用して、呼び出し、それでも同じ下部にあるとHttpClientをするために、これらの2つの方法があるが。HttpClientをのカプセル化は行なわ。私たちは詳細にこれら二つのアプローチの違いを説明しましょう、我々は最初のRestTemplateのやり方を見てください。

RestTemplate方法を呼び出します

サービスレジストリは、サービスセンターに登録されているかどうかを検出します。

直接コードに:

パッケージcom.mt.feign。
輸入org.springframework.boot.SpringApplication。
輸入org.springframework.boot.autoconfigure.SpringBootApplication。
輸入org.springframework.cloud.netflix.eureka.EnableEurekaClient。
輸入org.springframework.context.annotation.Bean。
輸入org.springframework.web.client.RestTemplate。

@SpringBootApplication
@EnableEurekaClient
パブリッククラスMtSpringcloudFeignClientApplication {
パブリック静的無効メイン(文字列[] args){
SpringApplication.run(MtSpringcloudFeignClientApplication.class、引数)。
}

@Bean
公共RestTemplate initRestTemplate(){
)(新しいRestTemplateを返します。
}
}

コールするRestTemplate方法

パッケージcom.mt.feign.controller。
輸入org.springframework.beans.factory.annotation.Autowired;
輸入org.springframework.web.bind.annotation.GetMapping。
輸入org.springframework.web.bind.annotation.RequestMapping。
輸入org.springframework.web.bind.annotation.RestController。
輸入org.springframework.web.client.RestTemplate。

@RestController
@RequestMapping( "/クライアント")
publicクラスコントローラー{
@Autowired
プライベートRestTemplateテンプレート。
@GetMapping( "/取得")
パブリックオブジェクトを取得(){
文字列結果は= template.getForObject( "http://127.0.0.1:8085/server/get"、String.class)。
結果を返します。
}
}

第二の方法を呼び出すRestTemplate

パッケージcom.mt.feign.controller。
輸入org.springframework.beans.factory.annotation.Autowired;
輸入org.springframework.cloud.client.ServiceInstance。
輸入org.springframework.cloud.client.loadbalancer.LoadBalancerClient。
輸入org.springframework.web.bind.annotation.GetMapping。
輸入org.springframework.web.bind.annotation.RequestMapping。
輸入org.springframework.web.bind.annotation.RestController。
輸入org.springframework.web.client.RestTemplate。

@RestController
@RequestMapping( "/クライアント")
publicクラスコントローラー{
@Autowired
プライベートRestTemplateテンプレート。
@Autowired
プライベートLoadBalancerClient loadBalancerClient。
@GetMapping( "/取得")
パブリックオブジェクトは、(){取得
ServiceInstance serviceInstance = loadBalancerClient.choose( "jilinwula-springcloud-装うサーバ")。
文字列のURL = String.Formatの( "のhttp://%sの:%sの/サーバー/取得"、serviceInstance.getHost()、serviceInstance.getPort());
文字列結果= template.getForObject(URL、String.class)。
結果を返します。
}
}

それはでLoadBalancerClientインターフェースSpringClourdを提供します。私たちは、ユーザインタフェースの中心を通って、アプリケーションの名前を使用してサービスのアドレスとポートを取得することができます。

RestTemplateを呼び出すには、3つの方法

起動クラスを変更します。

パッケージcom..feign。
輸入org.springframework.boot.SpringApplication。
輸入org.springframework.boot.autoconfigure.SpringBootApplication。
輸入org.springframework.cloud.client.loadbalancer.LoadBalanced;
輸入org.springframework.cloud.netflix.eureka.EnableEurekaClient。
輸入org.springframework.context.annotation.Bean。
輸入org.springframework.web.client.RestTemplate。
@SpringBootApplication
@EnableEurekaClient
パブリッククラスMtSpringcloudFeignClientApplication {
パブリック静的無効メイン(文字列[] args){
SpringApplication.run(MtSpringcloudFeignClientApplication.class、引数)。
}
@Bean
@LoadBalanced
公共RestTemplate initRestTemplate(){
新しいRestTemplateを返します();
}
}

RestTemplateで場所をインスタンス化し、自動的に私たちがRestTemplateを使用し、実際のサービスのアドレスにコール・インタフェース・アドレスに置き換えられますノートに関して、@LoadBalancedノートを追加しました。ここでは、コントローラの変化を見てみましょう。

パッケージcom.mt.feign.controller。
輸入org.springframework.beans.factory.annotation.Autowired;
輸入org.springframework.web.bind.annotation.GetMapping。
輸入org.springframework.web.bind.annotation.RequestMapping。
輸入org.springframework.web.bind.annotation.RestController。
輸入org.springframework.web.client.RestTemplate。
@RestController
@RequestMapping( "/クライアント")
publicクラスコントローラー{
@Autowired
プライベートRestTemplateテンプレート。
@GetMapping( "/取得")
のパブリックオブジェクトは、(取得){
文字列のURL = String.Formatの( "のhttp://%sの/サーバー/取得"、 "TCMP-対策サービス")。
文字列結果= template.getForObject(URL、String.class)。
結果を返します。

}

コードと最初のコード本質的に同じ、唯一の違いは、アプリケーション名を置き換えるのローカルサービスのアドレスとポートを取得することで、レジストリになり、最初の使用上の私たちのRestTemplateと違いはありませんが、URLで異なります。

それはすべてそこにある上に、実際のプロジェクトの開発に、これらの2つの方法がサービスコールとサービスの間で達成することができ、これらの2つの方法は欠点を持っている、それが特に推奨される方法ではありません。

マイクロスプリングクラウドサービス間のコール

おすすめ

転載: www.cnblogs.com/mengtaoadmin/p/11184041.html