the load-balancing architecture will be built as follows
Engineering description
Spring-cloud-02-ribbon-retry Analog customer service, request services, resttemplate+loadbalanced to achieve load balancing
Spring-cloud-02-ribbon-eureka as service discovery, manage all services
SPRING-CLOUD-02-RIBBON-CLIENT-01/SPRING-CLOUD-RIBBON-CLIENT-02 as a Cluster service, coordinating processing requests
several commonly used load-balancing strategies
Simple polling load balancing (roundrobin)
Stochastic load Balancing (Random)
Weighted response Time Load balancing (weightedresponsetime)
Zone aware polling load balancing (zoneaware)
Select a minimal concurrent request Bestavailablerule
Project Spring-cloud-02-ribbon-eureka, no explanation
The spring-cloud-02-ribbon-client-01 configuration is as follows
Spring:
application:
client-service server: 7002
/
Eureka:
client:
service-url:
http://localhost:8001/eureka/
@RestController
public class Indexcontroller {
@RequestMapping'/retry ') public
String retry () throws interruptedexception {
System.err.println ("-------Client---");
;
}
}
/spring-cloud-ribbon-client-02 configuration as follows
Spring:
application:
client-service server: 7001
/
Eureka:
client:
service-url:
http://localhost:8001/eureka/
@RestController
public class Indexcontroller {
@RequestMapping'/retry ') public
String retry () throws interruptedexception {
System.err.println ("-------Client---");
Thread.Sleep (5000);
;
}
}
Note: The Client2 service sleeps for 5 seconds before the corresponding
and start the service all
@SpringBootApplication
@EnableDiscoveryClient
public class Clientapplication {public
static void Main (string[] args) {
Springapplication.run ( Clientapplication.class, args);
}
Engineering spring-cloud-02-ribbon-retry POM Import Ribbon jar pack
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency >
<groupId>org.springframework.cloud</groupId>
<artifactId> spring-cloud-starter-ribbon</artifactid>
</dependency>
<dependency>
<groupid >org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
< /dependency>
</dependencies>
Configuration class
Spring:Application:Name:Retry-serviceCloud:LoadBalancer:Retry:Enabled:True #启动重试机制Server:Port:7004Context-path:/Eureka:Client:Service-url:Defaultzone:http://localhost:8001/eureka/#启动断路器Hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds:10000Custom:Rest:connect-timeout:1000connection-request-timeout:1000read-timeout:3000Client-service:Ribbon:
oktoretryonalloperations:True # if all requests are retriedMaxautoretriesnextserver:2 #重试切换实例次数maxautoretries:1 #重试次数
Configure load Balancing Resttemplate
@SpringBootApplication
@EnableRetry
@EnableDiscoveryClient
public class Retryapplication {
@Bean
@ConfigurationProperties"Custom.rest" ) Public
httpcomponentsclienthttprequestfactory httpcomponentsclienthttprequestfactory () {
Httpcomponentsclienthttprequestfactory httpcomponentsclienthttprequestfactory=new Httpcomponentsclienthttprequestfactory ();
return httpcomponentsclienthttprequestfactory;
}
@Bean
@LoadBalanced
public resttemplate resttemplate () {return
new Resttemplate (Httpcomponentsclienthttprequestfactory ());
}
public static void Main (string[] args) {
springapplication.run (Retryapplication.class, args);
}
Retrycontroller
@RestController
public class Retrycontroller {
@Autowired
private resttemplate resttemplate;
@RequestMapping "Retry")
Public String retry () throws Interruptedexception {
responseentity<string> result= Resttemplate.getforentity ("Http://client-service/retry", String.class);
System.err.println ("------------""--------------------");
"-------result"+result+
}
}
Code Explanation:
1: Access to Retrycontroller/retry, find the corresponding service through the service Discovery Client1/client Retry Service
2:retry configuration read-timeout:3000, if the call service exceeds 3 seconds, the service retries, retries 1 times, if it fails, the service switches the instance and retries again
3: Call by Ressttemplate Load Balancing poll
4: Call CLIENT1, no sleep, return immediately, service does not timeout
5: Call Client2, service sleep 5 seconds, check the timeout time 3 seconds, will be retried, retry, or failure, will be the instance switch, call service 1, normal return
screenshot One: Service Discovery Center
screenshot Two Access service return result is client01
Screenshot Three: Retry service Client2 two times
Screenshot Four: Toggle the instance to CLIENT1 and return the final result