Refer to chapter 16th and 17 of Springcloud official website
Client Side Load Balancer:ribbon
The Ribbon is a client-side load balancer that provides access control to a large number of HTTP and TCP clients. Feign is also used in the Ribbon, so in this chapter you can also use @feignclient
A core concept of the Ribbon is the named client. Each load balancer is part of the whole of this component, and they work together to connect to the server, and they all have a given name.
Spring Cloud uses Ribbonclientconfiguration to create a new whole in ApplicationContext, which contains a iloadbalancer, A restclient and a serverlistfilter
16.1 How to Include Ribbon
16.2 Customizing the Ribbon Client
Spring Cloud gives you complete control over the Ribbon client by declaring additional configurations with @ribbonclient. For example
In this example, the client is made up of ribbonclientconfiguration and fooconfiguration.
Note: In this example, fooconfiguration must be annotated with the @configuration annotation, but it should not be in the main application context component scan, otherwise it will be shared by all ribbon clients. If you use @componentscan (or @springbootapplication), then you should take steps to avoid it being included in the scanning range.
16.3 Customizing default for all Ribbon clients
With @ribbonclients annotations, you can provide a default configuration for all Ribbon clients. For example
So much has been said, how is it to be used? Here's a sample demo
There are three roles in this example: Registry, service provider, service consumer
Service provider is eureka-demo-client, consumer is Ribbon-demo, below see how the consumer is configured
First of all, Pom.xml, this is nothing to say.
Next is application.yml, and nothing special.
And finally the startup class.
There are also classes that are called
Start the Eureka-demo-server first, and then start the other two projects.
Browser Access to consumer address
HAHAHA, Access success!!!
Declarative REST Client:feign
Feign is a declarative Web services client that makes it easier to access Web services. In order to use feign, you need to create an interface and annotate it with annotations. With feign, Spring Cloud integrates the Ribbon and Eureka to provide an HTTP client with load balancing capabilities.
17.1 How to Include feign
The value of the @FeignClient annotation is the name of an arbitrary client that is used to create a ribbon load balancer. You can also use the URL property to specify a URL.
The Ribbon client wants to discover the true physical address of the "stores" service. If you are using Eureka in your app, it will be parsed by the Eureka Service registration class.
17.2 overriding feign Defaults
Next, use feign to demonstrate how to invoke the
and create a new project Feign-demo
Springcloud Study Notes (2)--ribbon