In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to use Ribbon to achieve load balancing in Spring Cloud. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Introduction to Ribbon
Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @ FeignClient then this section also applies.
Ribbon is a load balancing client, which can well control the behavior of HTTP and TCP clients. Feign has integrated Ribbon.
Spring Cloud Netflix defaults to ribbon (BeanType beanName:ClassName) to provide the following bean:
IClientConfig ribbonClientConfig: DefaultClientConfigImpl
IRule ribbonRule: ZoneAvoidanceRule
IPing ribbonPing: NoOpPing
ServerList ribbonServerList: ConfigurationBasedServerList
ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter
ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer
Preparatory work
This article starts the eureka-server project based on the previous article project, starts the eureka-client project with port 8040, changes the configuration file port of eureka-client to 8041 and starts, modifies the port number of the same project, starts multiple instances, and only needs to check Allow parallel run in IDEA:
At this point, you will find that two eureka-client instances are registered in the registration service, which is equivalent to a cluster of two nodes. Visit http://localhost:9090:
Create a new service consumer
Create a new project using Spring Initializr and call it ribbon-service. Check Eureka Discovery Client in Spring Cloud Discovery, Ribbon in Spring Cloud Routing, and Spring Web in Web:
After the creation is successful, the project pom.xml is as follows:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.9.RELEASE com.noodles.mars ribbon-service 0.0.1-SNAPSHOT ribbon-service Ribbon Service 1.8 Greenwich.SR3 org.springframework.cloud spring-cloud-starter-netflix-eureka-client Org.springframework.cloud spring-cloud-starter-netflix-ribbon org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org. Springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
Ribbon-service configuration service center address, application name, port, configuration file content:
Server: port: 8050spring: application: name: ribbon-serviceeureka: client: service-url: defaultZone: http://localhost:9090/eureka/
Annotate @ EnableDiscoveryClient on the project startup class to enable registration with the service center; define a RestTemplate Bean and annotate @ LoadBalanced to enable load balancing:
@ EnableEurekaClient@SpringBootApplicationpublic class RibbonServiceApplication {public static void main (String [] args) {SpringApplication.run (RibbonServiceApplication.class, args);} @ Bean @ LoadBalanced public RestTemplate restTemplate () {return new RestTemplate ();}}
Write a controller to consume the / hello interface of the eureka-client service through the previously defined RestTemplate. The application name is used in the url. Ribbon will select the specific service instance according to the application name, and replace the service name with the specific url when the service instance is requested:
@ RestControllerpublic class HelloController {private final RestTemplate restTemplate; @ Autowired public HelloController (RestTemplate restTemplate) {this.restTemplate = restTemplate;} @ GetMapping ("/ hello") public String hello (@ RequestParam ("name") String name) {return restTemplate.getForObject ("http://HELLO-ERUEKA-CLIENT/hello?name=" + name, String.class);}}
Visit http://localhost:8050/hello?name=Mars multiple times on the browser:
Hello, My name is Mars, I'm from port: 8040Hello, My name is Mars, I'm from port: 8041 so much about how to use Ribbon to achieve load balancing in Spring Cloud. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.