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

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).
主站蜘蛛池模板: 沂水县| 娄烦县| 邵阳县| 兴义市| 南丰县| 龙门县| 静乐县| 汝阳县| 八宿县| 西吉县| 资兴市| 肇东市| 明溪县| 嘉黎县| 博客| 本溪市| 延寿县| 宿迁市| 双柏县| 桐梓县| 安龙县| 广水市| 南江县| 上犹县| 舒兰市| 呼图壁县| 九寨沟县| 元阳县| 蕉岭县| 门源| 汶上县| 海原县| 万源市| 台湾省| 大足县| 辽阳县| 固始县| 个旧市| 大同市| 淄博市| 曲松县|