SpringCloud Ribbon 示例

目标

基于 eureka,创建一个后端service,使用不同端口运行3个实例,创建一个client service,通过 ribbon 调用后端service,验证负载均衡效果。

创建 eureka server

artifactId:eureka-server,spring boot: 1.5.16,依赖:


        
            org.springframework.cloud
            spring-cloud-starter-eureka-server
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.6
        
        
            com.fasterxml.jackson.core
            jackson-core
            2.9.6
        

        
            com.fasterxml.jackson.core
            jackson-annotations
            2.9.6
        
    

启动类添加注解 @EnableEurekaServer,配置文件:

spring.application.name=eurekaserver
server.port=8082
 
eureka.instance.hostname=localhost
 
eureka.client.serviceUrl.defaultZone=http://localhost:8082/eureka/
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

启动,查看页面 http://localhost:8082,看是否正常显示。

创建后端服务

artifactId:backend-service,依赖:


            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.6
        
        
            com.fasterxml.jackson.core
            jackson-core
            2.9.6
        

        
            com.fasterxml.jackson.core
            jackson-annotations
            2.9.6
        

启动类添加 @EnableEurekaClient,配置文件:

spring.application.name=server
server.port = 9090
 
eureka.client.serviceUrl.defaultZone= http://localhost:8082/eureka/
eureka.client.healthcheck.enabled= true
eureka.instance.leaseRenewalIntervalInSeconds= 1
eureka.instance.leaseExpirationDurationInSeconds= 2

测试接口:

@RestController
public class MyRestController {
 
    @Autowired
    Environment environment;
 
    @GetMapping("/")
    public String health() {
        return "I am Ok";
    }
 
    @GetMapping("/backend")
    public String backend() {
        System.out.println("Inside MyRestController::backend...");
 
        String serverPort = environment.getProperty("local.server.port");
 
        System.out.println("Port : " + serverPort);
 
        return "Hello form Backend!!! " + " Host : localhost " + " :: Port : " + serverPort;
    }
}

启动,访问接口,看是否正常。

创建 ribbon client 服务

artifactId:ribbon-client,依赖:

    
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
            org.springframework.cloud
            spring-cloud-starter-ribbon
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.6
        
        
            com.fasterxml.jackson.core
            jackson-core
            2.9.6
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            2.9.6
        
    

配置文件:

spring.application.name=client
server.port=8888
 
eureka.client.serviceUrl.defaultZone= http://localhost:8082/eureka/
eureka.client.healthcheck.enabled= true
eureka.instance.leaseRenewalIntervalInSeconds= 1
eureka.instance.leaseExpirationDurationInSeconds= 2
 
server.ribbon.eureka.enabled=true
server.ribbon.ServerListRefreshInterval=1000

ribbon 配置类:

public class RibbonConfiguration {
 
    @Autowired
    IClientConfig config;
    
    @Bean
    public IPing ribbonPing(IClientConfig config) {
        return new PingUrl();
    }
 
    @Bean
    public IRule ribbonRule(IClientConfig config) {
        return new AvailabilityFilteringRule();
    }
}

启动类:

@SpringBootApplication
@EnableDiscoveryClient
@RibbonClient(name = "server", configuration = RibbonConfiguration.class)
public class RibbonClientApplication {

    @Bean
    @LoadBalanced
    public RestTemplate proviceRestTemplate() {
        return new RestTemplate();
    }
    
    public static void main(String[] args) {
        SpringApplication.run(RibbonClientApplication.class, args);
    }
}

测试类:

@RestController
public class HelloController {
    @Autowired
    private RestTemplate restTemplate;
    
    @GetMapping("/hi")
    public String hi() {
        return restTemplate.getForObject("http://server/backend", String.class);
    }
}

运行测试

启动 eureka server,然后启动3个后端服务:

java -jar -Dserver.port=9090 target/backend-service-0.0.1-SNAPSHOT.jar
java -jar -Dserver.port=9091 target/backend-service-0.0.1-SNAPSHOT.jar
java -jar -Dserver.port=9092 target/backend-service-0.0.1-SNAPSHOT.jar

启动 ribbon client,访问测试接口:

http://localhost:8888/hi

多次刷新,就可以看到显示不同的端口号,说明负载均衡生效。

你可能感兴趣的:(SpringCloud Ribbon 示例)