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

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).
主站蜘蛛池模板: 宜黄县| 乳源| 浦北县| 梓潼县| 绥德县| 曲靖市| 饶平县| 辽源市| 榆树市| 锡林郭勒盟| 化州市| 汶上县| 湘西| 武冈市| 分宜县| 滁州市| 海原县| 屏东市| 扎赉特旗| 阜宁县| 侯马市| 长寿区| 德惠市| 深圳市| 时尚| 交口县| 会理县| 西华县| 台中县| 崇州市| 藁城市| 莱芜市| 金溪县| 海林市| 玉山县| 新丰县| 东平县| 宁化县| 罗山县| 灌南县| 朔州市|