Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write service consumers using Eureka

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use Eureka to write service consumers. The content of the article is of high quality. Therefore, Xiaobian shares it with you for reference. I hope you will have a certain understanding of relevant knowledge after reading this article.

1) Call the interface directly

To create a service consumer that consumes the user/hello interface we just wrote, you also need to create a Maven project eureka-client-article-service first, and then add dependencies, the same as the service provider.

Create a startup class App, and the startup code is the same as described above. The only difference is the configuration information in the application.properties file:

spring.application.name=eureka-client-article-serviceserver.port=8082

RestTemplate is a client provided by Spring for accessing Rest services. RestTemplate provides a variety of convenient methods for accessing remote Http services, which can greatly improve the writing efficiency of clients. We invoke the interface by configuring RestTemplate, as shown below.

@Configurationpublic class BeanConfiguration {@Beanpublic RestTemplate getRestTemplate() {return new RestTemplate(); }}

Create an interface and call the user/hello interface in the interface, as shown below.

@RestControllerpublic class ArticleController {@Autowiredprivate RestTemplate restTemplate;@GetMapping("/article /callHello")public String callHello() {return restTemplate.getForObject("http://localhost:8081/user/hello", String.class);}}

Execute the App to start the consumer service, visit the/article/callHello interface to see if the Hello string is returned, and if it is returned, it proves that the call is successful. The access address is http://localhost:8082/article/callHello (http://localhost%EF%BC%9A8082/article/callHello).

2) Consuming interfaces via Eureka

The methods mentioned above are called directly from the address of the service interface, as we did before, without the convenience of Eureka. Since the registry is used, then the client does not need to care about how many service providers there are when calling. Let's modify the previous calling code.

First modify the configuration of RestTemplate, add a @LoadBalanced annotation, this annotation will automatically construct the implementation class of LoadBalancerClient interface and register it in Spring container, the code is as follows:

@Configurationpublic class BeanConfiguration {@Bean@LoadBalancedpublic RestTemplate getRestTemplate() {return new RestTemplate(); }}

The next step is to modify the calling code. We no longer write the fixed address directly, but write the name of the service. This name is the name we registered in Eureka, which is spring.application.name in the property file. The relevant code is as follows.

@GetMapping("/article/callHello2")public String callHello2() {return restTemplate.getForObject("http://eureka-client-user-service/user/hello", String.class);} About how to use Eureka to write service consumers to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report