Spring cloud Eureka ribbon service registration, discovery and invocation
summary
Use a simple example to demonstrate the basic usage of Eureka and ribbon in spring cloud.
Version and environment
Building Eureka server
In spring cloud, you can use Eureka to manage microservices, and microservices can be registered in Eureka.
First, you can use spring initialzr of idea to create the Eureka server registry.
Modify application Properties file, add the following
Add the @ enableeurekaserver annotation to the startup class serverapplication generated for us by spring boot
Enter in the browser http://localhost:8881/
You can see the following interface:
You can see that the service has not been registered yet. At this point, a simple microservice registry is built.
Write microservice userservice
Next, build a microservice interface with rest and register it in the registry. Still use spring initialzr to build a new project. Use the same way as above.
Note that the Eureka discovery component should be checked this time. Not Eureka server.
Modify application Properties file, add the following:
Use the @ enablediscoveryclient annotation in the userapplication class generated for us by spring boot.
Create a rest full microservice interface.
After running userapplication, access again http://localhost:8881/
You will find that the user service has been registered.
Write microservice order
Next, we build an order micro service and access the interface in the user micro service.
Still use spring initialzr to build a new project. User is a micro service that can be deployed on multiple machines. When the client accesses this service, the request can be routed to any machine with the user service deployed. Therefore, the client needs to use a routing algorithm to schedule the user service. In spring cloud, the ribbon component can be used for client-side routing. The ribbon will go to the service registry to get the list of services so as to call the corresponding services.
This time, in addition to checking the Eureka discovery component. You also need to check the ribbon.
Modify application Properties file, add the following:
Add the following configuration to the orderapplication class generated for us by spring boot.
Because the ribbon is used, the @ loadbalanced annotation needs to be used here.
Write ordercontroller.
After running orderapplication, access http://localhost:8881/
You will find that the order service has also been registered in the registry.
Next, we access the getorderuser method in the ordercontroller to trigger the call to the getuser method of the usercontroller. Enter in the browser: http://localhost:8883/getOrderUser
You can see the return: I am user list
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.