With the above introduction, we already have service registries and service providers
Let's try to build a service consumer
It needs to complete two functions, discover services and consumer services, where discovery services are done by Eureka clients, and consumer services are done by the ribbon.
Based on the discovery of Eureka Service, the Ribbon realizes a set of selection strategies for service instances, thus realizing the consumption of service.
Before we do, we need to make some preparations.
Before starting the service registry, add a simple interface for hello-service, while creating a hello-service2 project and adding the same configuration as Hello-service
Create a new class of Hellocontroller in Hello-service
Start Project under test interface
Install this configuration to create a HELLO-SERVICE2 startup Project test interface
Here in order to differentiate the return value is written as Hello2 and the port in the configuration file is changed to 8081
We can also look at the registration center again when the preparation is complete.
At this point two hello-service have been registered in.
We formally start building services for consumers
Create a new spring boot project named Ribbon-consumer
Compared to the previous hello-service, we joined the Spring-cloud-starter-ribbon dependency
Adding @enablediscoveryclient annotations to the service main class
Create a resttemplate spring bean instance in the main class and turn on client load Balancing with @loadbalanced annotations
Create the Consumercontroller class and implement a simple/robbin-consumer interface
Application.properties do the following configuration
Spring.application.name=ribbon-consumer
server.port=9000
Eureka.client.serviceurl.defaultzone=http://peer1:1111/eureka/,http://peer2:1112/eureka
Start Project
Access test
Polling access from test results showing ribbon-consumer on two instances of Hello-service and Hello-service2 respectively
Note:
The provider and consumer of the service is also a relative concept because the services are called each other.
Spring Cloud Eureka 5 (service discovery and consumption-simple robbin use)