一,介绍
Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随即连接等)去连接这些机器。我们也很容易使用Ribbon实现自定义的负载均衡算法。
二、开发
1、新建项目hjduan.spring.cloud.ribbon pom如下
<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.hjduan.spring.cloud</groupId>
<artifactId>hjduan.spring.cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>hjduan.spring.cloud.ribbon</artifactId>
<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.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、application.yml配置
server:
port: 8764
eureka:
client:
serviceUrl:
defaultZone: http://admin:admin@localhost:8761/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
spring:
application:
name: hjduan-spring-cloud-ribbon
3、编写一个服务消费类 RibbonHelloWordService
package com.hjduan.spring.cloud.ribbon.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* Created by Administrator on 2017/7/11.
*/
@Service
public class RibbonHelloWordService {
@Autowired
RestTemplate restTemplate;
public String ribbonHelloWordService(String name) {
return restTemplate.getForObject("http://HJDUAN-SPRING-CLOUD-CONSUMER/hi/{name}",String.class,name);
}
}
4、编写controller
package com.hjduan.spring.cloud.ribbon.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.hjduan.spring.cloud.ribbon.service.RibbonHelloWordService;
/**
*
*
* 作者: 段浩杰 2017年7月25日
*/
@RestController
public class RibbonHelloWordController {
@Autowired
private RibbonHelloWordService ribbonHelloWordService;
@GetMapping(value = "/hi/{name}")
public String ribbonHelloWord(@PathVariable String name) {
return ribbonHelloWordService.ribbonHelloWordService(name);
}
}
5、编写启动类
package com.hjduan.spring.cloud.ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* Ribbon 做负载客户端负载均衡
*
* 作者: 段浩杰 2017年7月25日
*/
@EnableEurekaClient
@SpringBootApplication
public class RibbinApplicationConfiguration {
public static void main(String[] args) {
new SpringApplication(RibbinApplicationConfiguration.class).run(args);
}
/**
* @ LoadBalanced注册表明,这个restRemplate是负载均衡的。 @return
*/
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
6、运行
然后分别启动上一节的hjduan.spring.cloud.eureka, hjduan.spring.cloud.consumer,之后修改hjduan.spring.cloud.consumer的端口在启动一次。之后再启动这个项目访问地址http://localhost:8764/hi/123,运行两次观察浏览器的显示。