I. Introduction
After several series of cloud, I think we have a solid foundation for subsequent learning will be a lot easier, just to see if there are eureka reader needs basic knowledge, or knowledge seekers access to the cloud series of columns; this The main article explains how to use the ribbon implement web service client calls, ribbon default load balancing algorithm!
About two ribbon
ribbon is a client load balancer that can integrate different protocols tools for web service API calls;
The main features are as follows:
- Provide pluggable load balancing
- Integration Service discovery
- Fault elastic recovery
- cloud support
- Integrated load balancing client load
The core project as follows:
ribbon-core: integration with other modules to achieve load balancing
ribbon-eureka: eureka client-based load balancing
ribbon-httpclient: JSR-311 includes a loading implement load balancing REST
Three eureka-client
Created in the previous Project eureka-client packet controller, and providing a representation of an interface layer, as follows, and then start the eureka-client Example 2, respectively, port 8090, port 8091;
/**
* @Author lsc
* <p> 知识追寻者,ribbon provider the restful api</p>
*/
@RestController
public class RibbonProvidderController {
@GetMapping("zszxz/ribbon")
public String testRibbon(){
return "hell i am zszxz";
}
}
Four ribbon-client
4.1 pom.xml
In the lower parent project new module is added ribbon-client; original start-web dependence has been introduced in the parent project, so that the ribbon does not introduce the module dependency again;
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
</dependencies>
4.2 config
RestTemplate used for client calls, it needs to be configured, and the method for adding annotation @LoadBalanced opening ribbon represents the load balancing;
/**
* @Author lsc
* <p> ribbon-restTemplate配置 </p>
*/
@Configuration
public class RibbonConfig {
@Bean
@LoadBalanced//表示用于负载均衡
RestTemplate restTemplate(){
return new RestTemplate();
}
}
4.3 service
Create a service class, and use restTemplate conduct HTTP API calls; note the point here is not to use the ip port, but directly using the application name of the service eureka-client;
/**
* @Author lsc
* <p> </p>
*/
@Service
public class RibbonService {
@Autowired
RestTemplate restTemplate;
public String testRibbon(){
return restTemplate.getForObject("http://eureka-client/zszxz/ribbon",String.class);
}
}
4.4 controller
Create a presentation layer classes for browser access;
/**
* @Author lsc
* <p> </p>
*/
@RestController
public class RibbonController {
@Autowired
RibbonService ribbonService;
@GetMapping("ribbon/test")
public String testRibbon(){
return ribbonService.testRibbon();
}
}
4.5 ribbon startup class
The ribbon is started as a client-based terminal eureka @EnableEurekaClient it necessary to add annotations;
/**
* @Author lsc
* <p>知识追寻者 ribbon启动类 </p>
*/
@SpringBootApplication
@EnableEurekaClient
public class RibbonApp {
public static void main(String[] args) {
SpringApplication.run(RibbonApp.class,args);
}
}
4.6 application.yml
application.yml configurations using port 8092, registered address service are peer1, peer2, peer3;
server:
port: 8092
spring:
application:
name: eureka-ribbon # 应用名称
eureka:
client:
service-url:
# 服务注册地址
defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/
4.7 Start Project
Each start client, server; FIG follows
4.8 browser to access the results
Browser results are as follows, that we use restTemplate combine ribbon achieved through HTTP API calls from eureka-clinet module in the eureka-ribbon;
Five ribbon default load balancing
LoadBalancerClient is a core class ribbon, which can be obtained ribbon provides an example of information load balancing; adding the controller layer method ribbon-client module, the method body is obtained instance port and host; facilitate our analysis ribbon load balancing algorithm ;
@Autowired
LoadBalancerClient loadBalanced;
@GetMapping("ribbon")
public String ribbon(){
ServiceInstance choose = loadBalanced.choose("eureka-client");
String s = "host is :" + choose.getHost() + " port is :" + choose.getPort();
System.out.println(s);
return s;
}
Restart ribbon-client project, accessed using a browser, the console output is as follows, respectively called 8090,8091 two examples eureka-client API, explained ribbon default load-balancing mechanism is a polling algorithm;
host is :peer1 port is :8091
host is :peer1 port is :8090
host is :peer1 port is :8091
host is :peer1 port is :8090
host is :peer1 port is :8091
host is :peer1 port is :8090
host is :peer1 port is :8091
host is :peer1 port is :8090