The use of Spring Cloud-ribbon clients

Source: Internet
Author: User

starting with version 1.2.0, Spring Cloud Netflix now supports the use of attributes and Ribbon documentation is compatible from Defining Ribbon Clients .

This allows you to change the behavior at startup in different environments.

The supported properties are as follows and should be <clientName>.ribbon. prefixed with:

    • NFLoadBalancerClassName: Should be implementedILoadBalancer

    • NFLoadBalancerRuleClassName: Should be implementedIRule

    • NFLoadBalancerPingClassName: Should be implementedIPing

    • NIWSServerListClassName: Should be implementedServerList

    • NIWSServerListFilterClassNameshould be implementedServerListFilter

To set the service users name IRule , you can set the following:

Application.yml

users:  ribbon:    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule
using the Ribbon in Eureka

when Eureka is used in conjunction with the Ribbon (that is, both on the classpath), it is ribbonServerList extended to a DiscoveryEnabledNIWSServerList list of servers that have the extension Eureka. It also uses a NIWSDiscoveryPing replacement IPing interface, which proxies to Eureka to determine whether the server is started. One is installed by default ServerList , which is DomainExtractingServerList designed to make physical metadata available to the load balancer without using AWS Ami metadata (which Netflix relies on). By default, the server list is built using the zone information provided in the instance metadata (such as the Remote Client collection eureka.instance.metadataMap.zone ), and if it is missing, you can use the domain name in the server host name as the proxy for the zone (if a flag is set approximateZoneFromHostname ). Once the region information is available, it can be ServerListFilter used in. By default, it will be used to locate servers in the same region as the client because the default value is ZonePreferenceServerListFilter . by Default, the client's zone is the same as the remote instance, which is passed eureka.instance.metadataMap.zone .

Example: How to use the Ribbon without using the Eureka

Eureka is a convenient way to abstract the discovery of a remote server, so you do not have to hardcode its URL in the client, but if you do not want to use it, the ribbon and feign are still applicable. Suppose you have already applied for "store" @RibbonClient and Eureka is not being used (not even on the classpath). the Ribbon client defaults to the configured server list, which you can provide

Application.yml

stores:  ribbon:    listOfServers: example.com,google.com
Example: Disabling Eureka using in the Ribbon

Setting properties ribbon.eureka.enabled = false will explicitly disable the use of Eureka in the Ribbon.

Application.yml

ribbon:  eureka:   enabled: false
using the Ribbon API directly

You can also use it directly LoadBalancerClient . Example:

public class MyClass {@Autowired private loadbalancerclient loadbalancer;        public void Dostuff () {Serviceinstance instance = Loadbalancer.choose ("Stores");        URI Storesuri = uri.create (String.Format ("http://%s:%s", Instance.gethost (), Instance.getport ())); ... do something with the URI}}

Sources of information and source

The use of Spring Cloud-ribbon clients

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.