1. 程式人生 > >SpringBoot -- 負載均衡Ribbon

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

程式碼

如有疑問請加公眾號(K171),如果覺得對您有幫助請 github start
公眾號_k171