官术网_书友最值得收藏!

Creating a product client

We have created a dynamic product registry and even registered our service. Now, let's use this lookup to access the product service.

We will use the Netflix Ribbon project, which provides a load balancer as well as having address lookup from the service registry. Spring Cloud makes it easier to configure and use this.

For now, let's run the client within the same project as the service itself. The client will make an HTTP call to the service after looking up the product definition in Eureka. All this will be done by the Ribbon library, and we will just be using it as an endpoint:

  1. Add a dependency to the Maven POM of the product project as follows:
  1. Create a ProductClient class, which simply listens on /client and then forwards the request after doing a lookup to the actual product service:
The URL construction  http://PRODUCT/ will be translated at runtime by Ribbon after doing a lookup. We are nowhere giving an IP address of the service.
  1. The restTemplate is injected here through auto-wiring. However, it needs to be initialized in the latest Spring versions. Hence, declare it as follows in the main application class, which also acts as a configuration class:
The @LoadBalanced annotation tells Spring to use the Ribbon load balancer (as Ribbon is in the classpath due to Maven).
主站蜘蛛池模板: 德格县| 潞城市| 利川市| 绥阳县| 吉水县| 云龙县| 巴林左旗| 开鲁县| 泗洪县| 英德市| 屏边| 孝义市| 满洲里市| 理塘县| 玉溪市| 荥经县| 西盟| 华阴市| 资溪县| 林西县| 施甸县| 辽宁省| 苏尼特左旗| 大埔县| 龙岩市| 稷山县| 双城市| 邵阳市| 读书| 开封市| 繁昌县| 琼中| 盐山县| 手游| 土默特右旗| 德兴市| 当涂县| 响水县| 崇义县| 百色市| 建湖县|