前言
Spring Cloud的服务调用使用RESTFUL接口,Spring Cloud就是基于HTTP的restTemplate调用的,ribbon就是一个负载均衡,ribbon本意是轮训,意味着Spring Cloud的默认负载均衡是轮训策略。
1. ribbon service provider
要负载均衡需要服务提供方至少2个或以上的provider,所以我们要建2个提供方,建立ribbon-service的Spring Boot服务,并注册给eureka server。负载均衡是通过注册中心实现的,体现了中心化的思想。pom如下:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
</dependencies>
main方法,@EnableDiscoveryClient
package com.spring.cloud.ribbon.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonServiceMain {
public static void main(String[] args) {
SpringApplication.run(RibbonServiceMain.class, args);
}
}
application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8082/eureka/
server:
port: 8202
spring:
profiles: r1
application:
name: ribbon-service
my:
mark: ---------ribbon 1111111111111111----------
---
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8082/eureka/
server:
port: 8203
spring:
profiles: r2
application:
name: ribbon-service
my:
mark: ---------ribbon 2222222222222222----------
新建controller
package com.spring.cloud.ribbon.service.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class RibbonServiceController {
@Value("${my.mark}")
private String mark;
@RequestMapping(value = "/mark", method = RequestMethod.GET)
public Map<String, String> getRobbinService(){
Map<String, String> map = new HashMap<>();
map.put("myMark", mark);
return map;
}
}
使用JVM参数-Dspring.profiles.active=r2,-Dspring.profiles.active=r1分别启动
访问我们上一章的注册中心:

服务已经注册成功,注册application栏,这就是负载均衡的精髓。
2. 建一个ribbon的消费端
建一个模拟消费,创建方法跟上面类似,pom依赖相同。
application.yml文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8102/eureka/
server:
port: 8205
spring:
application:
name: ribbon-consumer
新建restTemplate,使用@LoadBalanced注解,此处可注明负载均衡模式,默认ribbon模式。
package com.spring.cloud.ribbon.consumer.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
新建service bean
package com.spring.cloud.ribbon.consumer.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
@Service
public class ConsumerService {
@Autowired
private RestTemplate restTemplate;
public Map consumerMark() {
return restTemplate.getForObject("http://RIBBON-SERVICE/mark",Map.class);
}
}
注意:服务调用在Spring Cloud中,使用application name表示,比如示例:RIBBON-SERVICE
新建controller,测试
package com.spring.cloud.ribbon.consumer.controller;
import com.spring.cloud.ribbon.consumer.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class ConsumerController {
@Autowired
private ConsumerService consumerService;
@RequestMapping(value = "/consumer", method = RequestMethod.GET)
public Map consumerService(){
return consumerService.consumerMark();
}
}
启动main方法,去eureka server查看,服务均已注册。

访问localhost:8205/consumer/,可以看到服务调用的轮训机制


总结
ribbon的负载均衡是实现的策略模式,可以根据用户配置选择不同的负载均衡算法。服务在发起调用的时候,会去eureka server拿application name对应的ip 端口 应用名等信息的列表,然后缓存下来,根据负载均衡算法,实现负载均衡。

2388

被折叠的 条评论
为什么被折叠?



