Home > Java > javaTutorial > Ribbon usage in Spring Cloud applications

Ribbon usage in Spring Cloud applications

无忌哥哥
Release: 2018-07-23 10:08:13
Original
1845 people have browsed it

A load balancer provides at least the following functions:

  • To maintain the IP and other information of each server

  • According to specific Logical selection of servers

In order to achieve basic load balancing functions, Ribbon's load balancer has three major sub-modules:

  • Rule

  • Ping

  • ServerList

When Ribbon is used in SpringCloud, the configuration can be done in two ways, one One is Java configuration and the other is configuration file configuration.

Configuration through java:

package com.hurricane.learn.springcloud.ribbon;

import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;

@RibbonClient(name="user-service-provider",configuration=MyConfig.class)
public class MyConfig {
	
	@Bean
	public MyRule createMyRule() {
		return new MyRule();
	}

}
Copy after login

Configuration file configuration:

#自定义规则的使用
user-service-provider.ribbon.NFLoadBalancerRuleClassName=com.hurricane.learn.springcloud.ribbon.MyRule
Copy after login

A pitfall of service calls:

Test calls between services, keep calling Unsuccessful, prompt:

Request URI does not contain a valid hostname: http://user_service_provider/getUser
Copy after login

Finally found out that it was because the service instance name cannot have underscores. You can access it by changing the instance name to aaa.

The above is the detailed content of Ribbon usage in Spring Cloud applications. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template