Spring Cloud Eureka 5 (service discovery and consumption-simple robbin use)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.