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

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).
主站蜘蛛池模板: 重庆市| 安多县| 南溪县| 抚远县| 吉林市| 东方市| 常熟市| 马关县| 土默特左旗| 沽源县| 新丰县| 苏州市| 商都县| 厦门市| 荣昌县| 北辰区| 类乌齐县| 北票市| 平泉县| 秀山| 阳泉市| 胶州市| 宣威市| 韩城市| 谷城县| 民县| 饶平县| 巴彦淖尔市| 桐庐县| 洛南县| 云梦县| 亳州市| 康保县| 思茅市| 镇江市| 日土县| 都昌县| 绥棱县| 巫山县| 基隆市| 婺源县|