1. Service call method
1.1 RPC和HTTP
Whether it is microservices or
SOA
, they all face remote calls between services. So what are the remote calling methods between services?
There are
two
common remote calling methods
:
RPC
:
Remote Produce Call
remote procedure call.
RPC is
based on
Socket
and works at the session layer. Custom data format
, fast and efficient
The rate is high. Early
webservices
, now popular
dubbo
, are
typical representatives of
RPC
Http
:
http
is actually
a network transmission protocol, based on
TCP
, works at the application layer, and specifies the format of data transmission
. Client browser
The communication with the server basically uses the
Http
protocol, which can also be used for remote service calls. The disadvantage is that the message package is bloated, and the advantage is to the service
The provider and the caller have no technical limitations, free and flexible, and more in line with the concept of microservices.
The popular
Rest
style can be realized through
http
protocol.
Difference
:
The mechanism of
RPC
is
defined in terms of language
API
(
language API
), not in terms of network-based applications.
If your company all adopts the
Java
technology stack, then it
is a good choice to use
Dubbo
as the microservice
architecture.
On the contrary, if the company's technology stack is diversified and you prefer the
Spring
family, then
Spring Cloud
is the best choice for building microservices. In our item
In the project, the
Spring Cloud
suite
will be selected , so the
Http
method will be used
to implement inter-service calls.
1.2Http client tool
Now that the
microservice has
chosen Http
, we need to consider ourselves to implement the processing of requests and responses. But there are already many
http
customers in the open source world
End tools can help us do these things, such as:
- HttpClient
- OKHttp
- URLConnection
However, these different clients have different
APIs
. And
Spring
also
encapsulates the
http
client and provides a tool class called
RestTemplate .
2.3. Spring 's RestTemplate (there are many details, need a lot of learning, master the methods)
Spring
provides a
RestTemplate
template tool class,
which encapsulates the
Http-
based client, and implements the
serialization and serialization of objects and
json
Deserialization is very convenient.
RestTemplate
does not limit the
Http
client type, but abstracts it. Currently,
3
commonly used
types are supported:
HttpClient
OkHttp
JDK
native
URLConnection
(default)