SpringBoot -- 负载均衡Ribbon

Ribbon负载均衡

Ribbon是基于HTTP与TCP客户端的负载均衡;
采用轮询server list的方式进行负载均衡;
与Eureka集成后,读取服务注册中心的server作为server list轮询;

集成Ribbon

创建RibbonServer module,引入org.springframework.cloud:spring-cloud-starter-ribbonorg.springframework.cloud:spring-cloud-starter-eurekaorg.springframework.boot:spring-boot-starter-web

build.gradle

apply plugin: 'org.springframework.boot'
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:" + springCloudVersion
        mavenBom "org.springframework.boot:spring-boot-starter:"+ springBootVersion    }
}

dependencies {
    compile ('org.springframework.cloud:spring-cloud-starter-ribbon')
    compile('org.springframework.cloud:spring-cloud-starter-eureka')

    compile ('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-log4j2')
    compile('org.apache.logging.log4j:log4j-1.2-api:'+ log4jAPIVersion)

    testCompile ('org.springframework.boot:spring-boot-starter-test')
    testCompile group: 'junit', name: 'junit', version: '4.11'
}
configurations {
    all*.exclude module: 'spring-boot-starter-logging'
    all*.exclude module: 'logback-classic'
    all*.exclude module: 'log4j-over-slf4j'
    all*.exclude module: 'snappy-java'
}
jar {
    baseName = 'ribbonserver-bootcwenao'
}

创建Application,使用@LoadBalanced获得负载均衡能力

/**
 * @author cwenao
 * @version $Id RibbonServerApplication.java, v 0.1 2017-01-14 16:50 cwenao Exp $$
 */
@SpringBootApplication(scanBasePackages = {"com.bootcwenao.ribbonserver"})
@EnableDiscoveryClient
@EnableCircuitBreaker
public class RibbonServerApplication {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate () {

        RestTemplate restTemplate = new RestTemplate();
        SimpleClientHttpRequestFactory factory = (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
        //TODO there can do some for request

        return restTemplate;

    }

    public static void main(String[] args) {
        new SpringApplicationBuilder(RibbonServerApplication.class).web(true).run(args);
    }
}

创建server与controller

因为只想验证下Ribbon能否调用到我们需要的server,所以写了两个controller
一个作为服务请求方,另外一个作为服务提供方

服务请求

通过RestTemplate调用服务
ribbonserver为启动的服务在服务注册中心注册的serviceId

/**
 * @author cwenao
 * @version $Id RibbonController.java, v 0.1 2017-01-15 10:46 cwenao Exp $$
 */
@Controller
public class RibbonController {

    @Autowired
    RestTemplate restTemplate;

    private final static String serverURI = "http://ribbonserver/";
    @RequestMapping("/test")
    public String testRibbon(String content) {

        System.out.println(content);
        restTemplate.getForEntity(serverURI+"testRealRibbon?content="+content,String.class);
        return "index";
    }

}

服务提供方

/**
 * @author cwenao
 * @version $Id RibbonRealVontroller.java, v 0.1 2017-01-15 11:33 cwenao Exp $$
 */
@RestController
public class RibbonRealVontroller {
    @Autowired
    private RibbonBootcwenaoServer ribbonBootcwenaoServerImpl;

    @RequestMapping("/testRealRibbon")
    public String testRealRibbon(String content) {
        String resultStr = ribbonBootcwenaoServerImpl.testRibbon(content);
        System.out.println(resultStr);
        return resultStr;
    }

}

server

@Service("ribbonBootcwenaoServerImpl")
public class RibbonBootcwenaoServerImpl implements RibbonBootcwenaoServer{
    public String testRibbon(String content) {
        return content + " for Spring Boot";
    }
}

application.yml

没有特殊配置如其他

关于全程没见Ribbon有什么动作,然而我们用了他

Ribbon与Eureka集成时,ribbon相当于 client
Ribbon server list将会被重写以获取Eureka上的服务列表
可以关闭Eureka

ribbon:
  eureka:
    enabled: false

代码

代码请移步 Github参考地址

如有疑问请加公众号(K171),如果觉得对您有帮助请 github start
SpringBoot -- 负载均衡Ribbon_第1张图片

你可能感兴趣的:(spring,boot/spring,cloud)