The Ribbon is a load balancer on the client side. When a consumer invokes a service using a service alias, the Ribbon does the load balancer to access the actual service invocation address in some way.

To make a simple analogy, let’s go to Mr. Tony, and there’s usually more than one Mr. Tony in a barbershop. But there will also be a staff similar to the front desk who will arrange a haircut for Tony if he is free. The staff is like the Ribbon. Are they arranged sequentially or randomly?

Ribbon + Eureka

Create a project

Also create an empty module in the Ribbon, then create a Ribbon – Consume9101 submodule under the Ribbon empty module. Add dependencies to the POM file of the parent, which is the empty module

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </dependency>
</dependencies>

When you introduce Eureka-related dependencies, you are already introducing the dependencies of the Ribbon, so using the Ribbon + Eureka will work without using the dependencies.

The overall project structure after creation is shown in the figure

The configuration file

application.yml

server:
  port: 9101

spring:
  application:
    name: ribbon-consume
eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8001/eureka/
  instance:
    instance-id: ribbon-consume9101

Startup and business classes

Note that the provider of our service is the eureka-provide service. This service name may be more accurate to refer to provide alone and may be changed later when the project is refactored.

When it comes to service to service invocations, you typically choose to use RestTemplate and you need to inject it into the Spring container, so you choose to use the configuration class

@Configuration public class ApplicationContextConfig {@bean@LoadBalanced // Public RestTemplate is required for load balancing getRestTemplate() { return new RestTemplate(); }}

Next is the main boot class

@SpringBootApplication @EnableEurekaClient @RestController public class RibbonConsume9101 { final String PROVIDE_URL = "http://eureka-provide"; RestTemplate restTemplate; public RibbonConsume9101(RestTemplate restTemplate) { this.restTemplate = restTemplate; } @GetMapping("/ribbon/consume") public String getInfo() { return "i am consumer, but actually invoke other service: [" + restTemplate.getForObject(PROVIDE_URL + "/eureka/provide", String.class) + "]"; } public static void main(String[] args) { SpringApplication.run(RibbonConsume9101.class, args); }}

test

  1. Open EureakThe registryEurekaServer8001
  2. Open 3 different portsService providerEurekaProvide7001EurekaProvide7002 , EurekaProvide7003
  3. Open the newly created RibbonconsumersRibbonConsume9101

Looking at the Eureka registry first, you can see that there are three service providers and one consumer that is already registered with Eureka.

Then consumers access interface http://localhost:9101/ribbon/… You can see that three different ports of service providers can be called, and in turn in a certain order (polling, which is the default rule).

Custom configuration classes

If you want to call the service in any other way, then you need to customize the configuration class.

The Ribbon has six main components

  • ServerList:Define the list of fetch servers
  • ServerListFilter:The ServerList list is filtered twice
  • ServerListUpdater:Define service update policies
  • Iping:Check if the service list is alive
  • IRlue:Select a service in the list of services to invoke according to the algorithm
  • ILoadBalancer:Software load balancer entry, integration of all the above components to achieve the load function
@Configuration public class RibbonCustomConfig { @Bean public IRule ribbonRule() { return new RandomRule(); }}

The IRLUE interface is to call the service provider in what rules, can you see the implementation class of the interface

To make this configuration file aware by the Ribbon, use the @RibbonClient annotation. Create an empty annotation class and add annotation and configuration classes to customize the Ribbon configuration.

@Configuration
@RibbonClient(name = "eureka-provide", configuration = RibbonCustomConfig.class)
public class RibbonConfig {
}

Here is what we need to call the name of the server in the configuration file springcloud. Application. The value of the name. Note that there is a paragraph in the official document

In this case, RibbonCustomConfig will be scanned into Spring if it is in the same package as the main startup class, which will result in the configuration file being shared by all @RibbonClients. You can also exclude configuration files with @ComponentScan.

Restart RibbonConsume9101 services, other don’t have to move, also called consumers access interface http://localhost:9101/ribbon/…

Do you think other services failed? You can see that the speed was obviously accelerated. Fortunately, the car did not roll over at last, it was indeed changed to random rules.

Custom configuration files

In addition to custom-defining the Ribbon through configuration classes, you can also customize it through configuration files

ClientName here is also a need to call the server in the configuration file springcloud. Application. The value of the name. If it is a completely self-written class, it needs to implement the corresponding interface. Here, it also uses the randomRule class written by Netflix.

You can delete RibbonCustomConfig and RibbonConfig or comment them all out and change the configuration file

server: port: 9101 spring: application: name: ribbon-consume eureka: client: fetch-registry: true register-with-eureka: true service-url: defaultZone: http://localhost:8001/eureka/ instance: instance-id: Ribbon - consume9101 # is added the following eureka - dojo.provide: ribbon: NFLoadBalancerRuleClassName: com.net flix. Loadbalancer. RandomRule

Eureka-provide is the clientName. Then restart RibbonConsume9101 service, other don’t have to move, also called consumers access interface http://localhost:9101/ribbon/…

It is important to note that the variables configured in the configuration file have higher priority than those in the configuration class.

Ribbon

The above shows that both the consumer and the server have registered with Eureka, which is equivalent to the consumer finding the services of other service providers through Eureka. How to solve the problem of accepting new consumer side in real business and not registering with Eureka?

Let’s take a look at what happens if you don’t register. You don’t even have to think about it

Create a project

Also create a submodule under the Ribbon parent

The configuration file

Since you don’t have to register with Eureka, the configuration file has to be changed accordingly

Server: port: 9102 spring: application: name: ribbon -- consume -- without -- eureka # The ribbon is not used in the server: port: 9102 spring: application: name: ribbon -- consume -- without -- eureka # eureka: enabled: false

Startup and business classes

You also need to import the RestTemplate so you can copy the previous submodule directly

@Configuration public class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); }}

The main boot class can also be copied directly without the use of Eureka, just remove the @EnableEurekaclient annotation and change the class name

@SpringBootApplication @RestController public class RibbonConsumeWithoutEureka9102 { final String PROVIDE_URL = "http://eureka-provide"; RestTemplate restTemplate; public RibbonConsumeWithoutEureka9102(RestTemplate restTemplate) { this.restTemplate = restTemplate; } @GetMapping("/ribbon/consume") public String getInfo() { return "i am consumer, but actually invoke other service: [" + restTemplate.getForObject(PROVIDE_URL + "/eureka/provide", String.class) + "]"; } public static void main(String[] args) { SpringApplication.run(RibbonConsumeWithoutEureka9102.class, args); }}

So how does the consumer know where to find the provider’s service when it calls it, so it needs to move the configuration file

server: port: 9102 spring: application: name: ribbon-consume-without-eureka ribbon: eureka: enabled: Eureka-provide: ribbon: listOfServers: localhost:7001, localhost:7002, localhost:7003

The eureka – dojo.provide is need to call the server in the configuration file springcloud. Application. The value of the name

Open RibbonConsume9102 service, other don’t have to move, call consumers access interface http://localhost:9102/ribbon/… You can see that you can also invoke the service in the default polling way.

It is not easy to create, if you have help, welcome thumb up, collection and share!

The following is a personal public number, interested can pay attention to, perhaps is your treasure public number oh, basic 2,3 days 1 more technical articles!!