Ribbon是一個基於HTTP和TCP的客戶端負載均衡工具,它有助於控制HTTP和TCP的客戶端的行爲。爲Ribbon配置服務提供者地址後,Ribbon就可基於某種負載均衡算法,自動地幫助服務消費者去請求。Ribbon默認爲咱們提供了不少負載均衡算法,例如輪詢、隨機等,咱們也可爲Ribbon實現自定義的負載均衡算法。java
1.啓動SpringCloud 高可用服務註冊中心(Eureka)搭建的註冊中心和服務提供者。算法
2.在say-hello項目中添加一個controller,對外提供服務spring
@RestController public class SayHelloController { @Value("${server.port}") private String serverPort; @GetMapping("/sayHello") public String sayHelloCtr(@RequestParam("helloName")String helloName){ return "Hello "+helloName+",個人端口是: "+serverPort; } }
3.而後把註冊中心和服務提供者say-hello分別啓動兩個實例。apache
4.查看Eureka註冊中心(http://localhost:11111/,http://localhost:11112/)segmentfault
1.建立一個module項目(service-ribbon)
2.添加maven依賴瀏覽器
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.definesys</groupId> <artifactId>my_cloud</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.definesys</groupId> <artifactId>service-ribbon</artifactId> <version>0.0.1-SNAPSHOT</version> <name>service-ribbon</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <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-netflix-ribbon</artifactId> </dependency> <!--ribbon中使用斷路器--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.修改配置文件app
server.port=3333 eureka.client.service-url.defaultZone=http://server1:11111/eureka/,http://server2:11112/eureka/ spring.application.name=service-ribbon
4.建立一個開啓負載均衡的restRemplate負載均衡
@Configuration public class RestTemplateConfig { @Bean @LoadBalanced //代表這個restRemplate開啓負載均衡的功能 public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder){ return restTemplateBuilder .setConnectTimeout(20000) .setReadTimeout(30000) .build(); } }
5.添加service消費say-hello提供的服務dom
@Service public class HelloServiceRibbonSer { @Autowired RestTemplate restTemplate; public String helloServiceRibbon(String helloName) { return restTemplate.getForObject("http://say-hello/sayHello?helloName="+helloName,String.class); } }
注:getForObject中的url say-hello爲say-hello項目(服務提供者)的應用名稱,也就是在建立say-hello項目時在配置文件中配置的spring.application.name=say-hello。sayHello爲say-hello項目中接口的地址(controller), helloName是請求參數。maven
6.建立controller,調用service中的方法
@RestController public class HelloServiceRibbonControler { @Autowired private HelloServiceRibbonSer helloServiceRibbonSer; @GetMapping("/ribbonHello") public String ribbonHelloCtr(@RequestParam("helloName")String helloName){ return helloServiceRibbonSer.helloServiceRibbon(helloName); } }
7.啓動service-ribbon,在瀏覽器地址欄訪問ribbon項目中的controller
當一直訪問http://localhost:3333/ribbonHello?helloName=aaaa時能夠發現瀏覽器交替顯示端口2222和2223,說明已經實現客戶端的負載均衡,而且ribbon默認採用輪詢的負載均衡算法。
1.修改service-ribbon工程配置文件添加以下配置(使用隨機方式)
client.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
2.修改RestTemplateConfig配置類添加
@Bean public IRule ribbonRule() { return new RandomRule();//實例化與配置文件對應的策略類 }
3.修改HelloServiceRibbonControler
@RestController public class HelloServiceRibbonControler { @Autowired private HelloServiceRibbonSer helloServiceRibbonSer; @Autowired private LoadBalancerClient loadBalancerClient; @GetMapping("/ribbonHello") public String ribbonHelloCtr(@RequestParam("helloName")String helloName){ return helloServiceRibbonSer.helloServiceRibbon(helloName); } /** * 隨機方式 * @param helloName * @return */ @GetMapping("/ribbonRandomHello") public String ribbonRandomHelloCtr(@RequestParam("helloName")String helloName){ this.loadBalancerClient.choose("CLIENT");//隨機訪問策略 return helloServiceRibbonSer.helloServiceRibbon(helloName); }
4.啓動項目
依次啓動eureka註冊中心,say-hello項目,service-ribbon項目,訪問http://localhost:3333/ribbonRandomHello?helloName=aaaa。能夠發現瀏覽器隨機顯示端口。