1. Load Balancing Ribbon
In actual environments, many
user
-
service
clusters are often opened
. There will be more than one service list at this time. Which one should I visit?
Generally, in this case, you need to write a load balancing algorithm and select from a list of multiple instances.
However,
Eureka
has integrated a load balancing component:
Ribbon
, which can be used by simply modifying the code.
What is
Ribbon
:
Next, we will use
Ribbon to
achieve load balancing.
1.1 Start two service instances
First, we configure and start two
user
-
service
instances, one
9091
and one
9092
.
Modify xxxConfiguration and make a copy
Eureka
monitoring panel:
2.2. Turn on load balancing
Because
Ribbon is already integrated in
Eureka
, we don't need to introduce new dependencies.
Direct personal reformation
Consumer
-
Demo \ Src \ Main \ Java \ Com \ Itheima \ Consumer \ ConsumerApplication.Java
In
RestTemplate
add the configuration method
@LoadBalanced
notes:
Modify the
calling method of
consumer
-
demo\src\main\java\com\itheima\consumer\controller\ConsumerController.java
, instead of manually obtaining the ip
and port, but directly calling it through the service name;
Visit the page to view the results; and you can
view the execution status on the
9091
and
9092
consoles: