天天看點

SpringBoot -- 負載均衡RibbonRibbon負載均衡

Ribbon負載均衡

Ribbon是基于HTTP與TCP用戶端的負載均衡;

采用輪詢server list的方式進行負載均衡;

與Eureka內建後,讀取服務注冊中心的server作為server list輪詢;

內建Ribbon

建立RibbonServer module,引入org.springframework.cloud:spring-cloud-starter-ribbon、org.springframework.cloud:spring-cloud-starter-eureka、org.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 -- 負載均衡RibbonRibbon負載均衡

繼續閱讀