2016년 7월 17일 일요일

Client loadbalancing with ribbon (2)

You can set the loadbalnace server like below.

- Structure













- pom.xml



    4.0.0

    org.blog.test
    load-balance-project
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        1.3.6.RELEASE
         
    

    
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.cloud
            spring-cloud-starter-ribbon
            1.1.4.RELEASE
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    




- application.properties

load-client.ribbon.eureka.enabled=false
load-client.ribbon.listOfServers=localhost:8888,localhost:9999
load-client.ribbon.ServerListRefreshInterval:15000


- LoadClientConfiguration.java

package org.blog.test;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AvailabilityFilteringRule;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.PingUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

public class LoadClientConfiguration {

    @Autowired
    IClientConfig ribbonClientConfig;

    @Bean
    public IPing ribbonPing(IClientConfig config) {
        return new PingUrl(false, "/info");
    }

    @Bean
    public IRule ribbonRule(IClientConfig config) {
        return new AvailabilityFilteringRule();
    }

}


- LoadBalanceApplication.java

package org.blog.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@RestController
@RibbonClient(name = "load-client", configuration = LoadClientConfiguration.class)
public class LoadBalanceApplication {

    public static void main(String[] args) {
        SpringApplication.run(LoadBalanceApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/info")
    public String getInfo() {
        return restTemplate.getForObject("http://load-client/server/info", String.class);
    }
}


You can find the response as below.

- result

댓글 없음 :

댓글 쓰기