brief introduction
-
What is load balancing?
Load balancing is an indispensable component in the distributed architecture. Its significance is to allocate requests to various service providers through certain rules or algorithms. -
Ribbon is a client-side load balancer that provides a range of capabilities that let you control HTTP and TCP clients.
The following examples are based on the Greenwich.SR1 version of spring cloud and need to rely on the Earlier article on Eureka
Basic dependence
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <!-- Use Eureka To cooperate with Ribbon --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies>
Ribbon
The following steps are required to test the function of Ribbon.
- Run Eureka server first to start the registry.
- Run Eureka client on two different ports (6603, 6604).
You need to change the previous code to add port output to the / hello interface to differentiate services.
@Value("\${server.port}") var port: String? = null @RequestMapping("/hello") fun hello(@RequestParam("name") name: String): String { return "response from $port: hello $name." }
- Configure to start Ribbon test application
Because we need to rely on Eureka here, we need to specify the address of the registry.
application.yml
server: port: 6605 eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:6600/eureka/ spring: application: name: ribbon-client
In the startup class, add the @ EnableDiscoveryClient annotation.
@SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient class RibbonClientStarter fun main(args: Array<String>) { runApplication<RibbonClientStarter>(*args) }
After startup, open http://localhost:6600 You can see the services that are started now.
EUREKA-CLIENT UP (2) - 192.168.1.135:eureka-client:6603 , 192.168.1.135:eureka-client:6604 RIBBON-CLIENT UP (1) - 192.168.1.135:ribbon-client:6605
- Create a Controller to call Eureka client services.
First, configure to create a RestTemplate Bean to call.
@Configuration class RestTemplateConfiguration { // Use @ LoadBalance to turn on load balancing. @Bean @LoadBalanced fun restTemplate(): RestTemplate { return RestTemplate() } }
Then use RestTemplate in the Controller.
@RestController class DemoController { @Autowired lateinit var restTemplate: RestTemplate @RequestMapping("/hello") fun hello(@RequestParam("name") name: String): String? { // Where EUREKA-CLIENT is the uppercase form of the application name in application.yml and also the name displayed in the registry. // hello is the name of the interface provided by the service. return restTemplate.getForObject("http://EUREKA-CLIENT/hello?name=$name", String::class.java) } }
Multiple visits http://localhost:6605/hello?name=czb1n, it will display in turn: response from 6603: hello czb1n. And response from 6604: hello czb1n
Other
Example code address: https://github.com/czb1n/learn-spring-cloud-with-kotlin