In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how Ribbon consumes microservice-provider-user in a load-balanced manner. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
To consume microservice-provider-user services is easy, we just need to use RestTemplate, or http tools such as HttpClient can be used. But in a cluster environment, we must deploy multiple instances per service, so what about load balancing when service consumers consume service providers?
1. Preparatory work
Start the registry: microservice-discovery-eureka
Startup provider: microservice-provider-user
Modify the port of microservice-provider-user to 8001, and start another instance
At this point, visit http://discovery:8761
You can see that there are two instances of microservice-provider-user running in Eureka.
Let's create a new microservice (microservice-consumer-movie-*) that consumes microservice-provider-user 's services in a load-balanced manner.
2.Ribbon introduction
Ribbon is an open source project released by Netflix, and its main function is to provide software load balancing algorithms on the client side to connect the middle-tier services of Netflix. The Ribbon client component provides a complete set of configuration items such as connection timeout, retry, etc. To put it simply, all the machines behind Load Balancer are listed in the configuration file, and Ribbon will automatically help you connect these machines based on certain rules (such as simple polling, immediate connection, etc.). We can also easily implement custom load balancing algorithms using Ribbon. Simply put, Ribbon is a client-side load balancer. Ribbon is divided into two steps: the first step is to select Eureka Server, which gives priority to the Server; with the same Zone and less load, and then, according to the policy specified by the user, select an address from the service registration list obtained from Server. Ribbon provides three strategies: polling, circuit breaker, and weighting based on response time.
3. Development
Create a Maven project and add the following to pom.xml:
Xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://m
Aven.apache.org/xsd/maven-4.0.0.xsd ">
4.0.0
Microservice-consumer-movie-ribbon
Jar
Com.itmuch.cloud
Spring-cloud-microservice-study
0.0.1-SNAPSHOT
Org.springframework.cloud
Spring-cloud-starter-eureka
Org.springframework.cloud
Spring-cloud-starter-ribbon
Org.springframework.boot
Spring-boot-starter-actuator
Startup class: MovieRibbonApplication.java. Use the @ LoadBalanced annotation to enable load balancing for RestTemplate.
@ SpringBootApplication
@ EnableDiscoveryClient
Public class MovieRibbonApplication {
/ * *
* instantiate RestTemplate and enable load balancing capacity through @ LoadBalanced annotation.
* @ return restTemplate
* /
@ Bean
@ LoadBalanced
Public RestTemplate restTemplate () {
Return new RestTemplate ()
}
Public static void main (String [] args) {
SpringApplication.run (MovieRibbonApplication.class, args)
}
}
Entity class: User.java
Public class User {
Private Long id
Private String username
Private Integer age
...
/ / getters and setters
}
Test class for Ribbon: RibbonService.java
@ Service
Public class RibbonService {
@ Autowired
Private RestTemplate restTemplate
Public User findById (Long id) {
/ / serviceId/url of the http:// service provider
Return this.restTemplate.getForObject ("http://microservice-p"
Rovider-user/ "+ id, User.class)
} Wuxi × × Hospital https://yyk.familydoctor.com.cn/20612/
}
Controller:RibbonController.java
@ RestController
Public class RibbonController {
@ Autowired
Private RibbonService ribbonService
@ GetMapping ("/ ribbon/ {id}")
Public User findById (@ PathVariable Long id) {
Return this.ribbonService.findById (id)
}
}
Application.yml
Server:
Port: 8010
Spring:
Application:
Name: microservice-consumer-movie-ribbon
Eureka:
Client:
ServiceUrl:
Instance:
PreferIpAddress: true
After startup, visit http://localhost:8010/ribbon/1 multiple times and return the result:
{
"id": 1
"username": "Tom"
"age": 12
}
Then open the console of the two microservice-provider-user instances and find that both instances output log contents similar to the following:
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_
User0_.username as username3_0_0_ from user user0_ where user0_.id=?
2016-09-13 21 o.h.type.descriptor.sql.BasicBinder 38 o.h.type.descriptor.sql.BasicBinder 56.719 TRACE 17404-[nio-8000-exec-1]
Binding parameter [1] as [BIGINT]-[1]
2016-09-13 21 o.h.type.descriptor.sql.BasicExtractor 38 o.h.type.descriptor.sql.BasicExtractor 56.720 TRACE 17404-[nio-8000-exec-1]
Extracted value ([age2_0_0_]: [INTEGER])-[12]
2016-09-13 21 o.h.type.descriptor.sql.BasicExtractor 38 o.h.type.descriptor.sql.BasicExtractor 56.720 TRACE 17404-[nio-8000-exec-1]
Extracted value ([username3_0_0_]: [VARCHAR])-[Tom]
2016-09-13 21 c.n.d.s.r.aws.ConfigClusterResolver 39 c.n.d.s.r.aws.ConfigClusterResolver 10.588 INFO 17404-[trap-executor-0]
Resolving eureka endpoints
Via configuration
So far, we have achieved load balancing on the client side through Ribbon.
So much for sharing about how Ribbon consumes microservice-provider-user in a load-balanced manner. I hope the above content can be of some help and learn more knowledge. 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.