spring-cloud-Eureka-Ribbon负载均衡

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

配置:

@Configuration
public class WebConfig {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate () {
        return new RestTemplate();
    }
}

使用:

@RestController
public class TestController {
    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/test")
    public String test () {
        return restTemplate.getForObject("http://happlicationinterface/tet", String.class);
    }

}

以上假设有两个application.name为happlicationinterface的客户端启动了,如果访问会实现负载均衡:
这两个客户端的实现如下:

@RestController
public class TestController {
    @GetMapping("/tet")
    public String test () {
        return "my test";
    }
}
@RestController
public class TestController {
    @GetMapping("/tet")
    public String test () {
        return "your test";
    }
}

这时如果访问:http://localhost/test则会轮流出现“my test”和“your test”,说明负载均衡功能已经实现了
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/a13662080711/article/details/108440636