RestTemplate implements communication and interface calls between microservices

Problem phenomenon:

Today, in the process of learning microservice architecture, I am thinking about:

How to realize the communication between microservices?


problem analysis:

During my study, I came across a very simple communication method, and I would like to share it with you here:

When we first learned Spring, we often used  HttpClient or Httpurlconnection  to implement external service interface calls , but the code was too redundant.

Here is a method for everyone: RestTemplate , which is convenient and fast!

First, register the  RestTemplate object with @Bean in the startup class

Then you can inject the RestTemplate object in the Controller layer

The interface of the corresponding service can be called by calling the method of the object:

Here is a brief mention:

The methods of get request are: getForEntity and getForObject (url address, data type returned by the interface)

The post request methods are: postForLocation and postForObject 

note:

Here is a mention: Feign, an extremely commonly used component , is used for calls between microservices, and is more optimized and more convenient than RestTemplate;

But only the registered microservice interface can be called, and the external service interface cannot be called at will like RestTemplate.

 

 

Guess you like

Origin blog.csdn.net/weixin_42585386/article/details/109205203