Spring Cloudマイクロサービスアーキテクチャを構築するサービス消費(一)
9095 ワード
前の記事Spring Cloudでマイクロサービスを構築するサービス登録と発見(一)NetflixのEurekaサービスを使用してサービス登録センターとサービスプロバイダを構築する方法を紹介し,ここではLoadBalanceClientを使用して登録センターに登録されたサービスプロバイダが提供するインタフェースを消費する方法を紹介する.
LoadBalanceClientの使用
Spring cloudでは、DiscoveryClient、LoadBalanceClientなど、サービスガバナンスを提供する高度な抽象インタフェースが提供されています.LoadBalanceClient,Spring Cloudが提供する負荷等化クライアントインタフェースを直接使用してサービスの消費を実現することができます.使用方法については、次の例を参照してください.1サービスエンジニアリングの下に新しいModuleを作成します.名前はloadbalance-client-customer、pomです.xmlは以下の通り:
ここでは特にjackson-databindの依存バージョンを指定します.なぜなら、バージョンを指定しないと次のNoClassDefFoundError異常が報告されるからです.2イニシエータクラス作成LoadBalanceClientコンシューマクラス 3消費者マイクロサービスを作成するControllerはModuleの下でcontrollerパッケージを作成し、Controllerインタフェースを作成して消費者マイクロサービスが提供するマイクロサービス を作成する.
ControllerではLoadBalanceClientオブジェクトを注入し、インタフェースが呼び出されたときにLoadBalanceClientがchooseメソッドで負荷を動的に計算して指定した名前のマイクロサービスプロバイダインスタンスServiceInstanceを選択します.選択したサービスInstanceのホストとポートをlogで出力します.その後、対応するサービスプロバイダインタフェースがRestTemplateを介して呼び出される. 4構成項目properties 5ブラウザでのプロジェクトの開始:http://localhost:9001/customer/loadbalancerclient/getUserInfo?userName=snow コンソールには、 というログが表示されます.
ログ分析から、LoadBalanceClientは登録された2つのノード8801と8802を等化して選択し、消費者向けのマイクロサービス提供を要求するインタフェースを処理し、restTemplateを通じてマイクロサービスにアクセスする.次のブラウザからコンテンツが表示され、要求がマイクロサービスに到着し、正しく戻ったことを確認できます.
これでLoadBalanceClientコンシューママイクロサービスが提供するインタフェースのプレゼンテーションが完了し、記事のすべてのインスタンスコードは以下の接続で参考にすることができます.ありがとうございます.コード市:spring-cloud-demos
LoadBalanceClientの使用
Spring cloudでは、DiscoveryClient、LoadBalanceClientなど、サービスガバナンスを提供する高度な抽象インタフェースが提供されています.LoadBalanceClient,Spring Cloudが提供する負荷等化クライアントインタフェースを直接使用してサービスの消費を実現することができます.使用方法については、次の例を参照してください.
services
spring-cloud-demos
1.0-SNAPSHOT
4.0.0
loadBalance Client
loadbalance-client-customer
org.springframework.cloud
spring-cloud-starter-eureka
com.fasterxml.jackson.core
jackson-databind
2.8.9
org.springframework.boot
spring-boot-starter-web
1.5.7.RELEASE
ここでは特にjackson-databindの依存バージョンを指定します.なぜなら、バージョンを指定しないと次のNoClassDefFoundError異常が報告されるからです.
Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.fasterxml.jackson.databind.SerializationConfig
at com.fasterxml.jackson.databind.ObjectMapper.(ObjectMapper.java:560) ~[jackson-databind-2.8.10.jar:2.8.10]
at com.fasterxml.jackson.databind.ObjectMapper.(ObjectMapper.java:476) ~[jackson-databind-2.8.10.jar:2.8.10]
at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.build(Jackson2ObjectMapperBuilder.java:588) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.(MappingJackson2HttpMessageConverter.java:57) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter.(AllEncompassingFormHttpMessageConverter.java:61) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.web.filter.HttpPutFormContentFilter.(HttpPutFormContentFilter.java:63) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.boot.web.filter.OrderedHttpPutFormContentFilter.(OrderedHttpPutFormContentFilter.java:29) ~[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration.httpPutFormContentFilter(WebMvcAutoConfiguration.java:149) ~[spring-boot-autoconfigure-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$$EnhancerBySpringCGLIB$$b70dbbdb.CGLIB$httpPutFormContentFilter$1() ~[spring-boot-autoconfigure-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$$EnhancerBySpringCGLIB$$b70dbbdb$$FastClassBySpringCGLIB$$d99e4f65.invoke() ~[spring-boot-autoconfigure-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$$EnhancerBySpringCGLIB$$b70dbbdb.httpPutFormContentFilter() ~[spring-boot-autoconfigure-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
... 27 common frames omitted
package com.snow.spring.cloud.customer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class CustomerByLoadBalanceClientApp {
public static void main(String[] args){
SpringApplication.run(CustomerByLoadBalanceClientApp.class,args);
}
@Bean
RestTemplate restTemplate(){
return new RestTemplate();
}
}
package com.snow.spring.cloud.customer.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
*
*/
@RestController
public class LoadBalanceController {
Logger logger = LoggerFactory.getLogger(LoadBalanceController.class);
@Autowired
LoadBalancerClient loadBalancerClient;
@Autowired
RestTemplate restTemplate;
/**
* 1 getUserInfo
* @param userName
* @return
*/
@GetMapping("/customer/loadbalancerclient/getUserInfo")
public String getInfoFrom(@RequestParam String userName){
logger.info(" ---Load Balance Client ");
ServiceInstance instance = loadBalancerClient.choose("biz-provider-service1");
logger.info(" :" + instance.getHost() + "-- :" + instance.getPort());
return restTemplate.postForObject("http://"+instance.getHost()+":"+instance.getPort()+"/getUserInfo",userName,String.class);
}
}
ControllerではLoadBalanceClientオブジェクトを注入し、インタフェースが呼び出されたときにLoadBalanceClientがchooseメソッドで負荷を動的に計算して指定した名前のマイクロサービスプロバイダインスタンスServiceInstanceを選択します.選択したサービスInstanceのホストとポートをlogで出力します.その後、対応するサービスプロバイダインタフェースがRestTemplateを介して呼び出される.
spring:
application:
name: loadbalance-customer-serice
server:
port: 9001
eureka:
instance:
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://localhost:8762/eureka/
services:
urls:
userInfoUrl: http://biz-provider-service1/
2018-07-01 15:45:12.535 INFO 13256 --- [nio-9001-exec-1] c.s.s.c.c.c.LoadBalanceController : :192.168.43.27-- :8802
2018-07-01 15:45:13.455 INFO 13256 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty : Flipping property: biz-provider-service1.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2018-07-01 15:50:00.097 INFO 13256 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
2018-07-01 15:50:00.552 INFO 13256 --- [nio-9001-exec-6] c.s.s.c.c.c.LoadBalanceController : ---Load Balance Client
2018-07-01 15:50:00.553 INFO 13256 --- [nio-9001-exec-6] c.s.s.c.c.c.LoadBalanceController : :192.168.43.27-- :8801
ログ分析から、LoadBalanceClientは登録された2つのノード8801と8802を等化して選択し、消費者向けのマイクロサービス提供を要求するインタフェースを処理し、restTemplateを通じてマイクロサービスにアクセスする.次のブラウザからコンテンツが表示され、要求がマイクロサービスに到着し、正しく戻ったことを確認できます.
hello snowfrom provider service1
これでLoadBalanceClientコンシューママイクロサービスが提供するインタフェースのプレゼンテーションが完了し、記事のすべてのインスタンスコードは以下の接続で参考にすることができます.ありがとうございます.コード市:spring-cloud-demos