2016년 7월 19일 화요일

Client side loadBalacing with eureka(2)

You can set the load balance client that using eureka by ribbon client library as below.

- Structure

























- pom.xml



    4.0.0

    org.blog.test
    loadbalance-client-project
    1.0-SNAPSHOT


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

    
        UTF-8
        1.8
    

    
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        

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

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

    

    
        
            
                org.springframework.cloud
                spring-cloud-starter-parent
                Angel.SR4
                pom
                import
            
        
    

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





- application.properties

server.port=8080
spring.application.name=eureka-client


- ClientApplication.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.eureka.EnableEurekaClient;
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
@EnableEurekaClient
@RestController
@RibbonClient(name = "eureka-server")
public class ClientApplication {

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

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

    @Autowired
    RestTemplate restTemplate;

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


- result

You can find the response from the eureka-server by using client side loadbalancing.

댓글 없음 :

댓글 쓰기