SpringCloud(四) ribbon负载均衡

痛定思痛。 2022-05-22 20:36 351阅读 0赞

在集群环境下,负载均衡很重要。下面演示客户端的负载均衡ribbon

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>

eureka client的依赖中已经集成了ribbon的依赖,所以不用引依赖了。

创建第一个服务端:

controller代码:

  1. package com.xhx.springcloud.controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. /**
  5. * xuhaixing
  6. * 2018/6/3 16:18
  7. */
  8. @RestController
  9. @RequestMapping(value = "hello")
  10. public class HelloController {
  11. @RequestMapping(value = "getName")
  12. public String getName(String name){
  13. return name+2;
  14. }
  15. }

配置文件

  1. server:
  2. port: 8081
  3. eureka:
  4. client:
  5. service-url:
  6. defaultZone: http://localhost:8761/eureka
  7. spring:
  8. application:
  9. name: eureka-service

创建第二个服务端,spring.spplication.name应该和第一个服务端一样

  1. package com.xhx.springcloud.controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. /**
  5. * xuhaixing
  6. * 2018/6/3 16:18
  7. */
  8. @RestController
  9. @RequestMapping(value = "hello")
  10. public class HelloController {
  11. @RequestMapping(value = "getName")
  12. public String getName(String name){
  13. return name+1;
  14. }
  15. }

配置文件:

  1. server:
  2. port: 8080
  3. eureka:
  4. client:
  5. service-url:
  6. defaultZone: http://localhost:8761/eureka
  7. spring:
  8. application:
  9. name: eureka-service

启动eureka与两个服务端,然后打开网页127.0.0.1:8761

70

可以看到两个服务端都注册到一起了。

创建一个eureka-client

配置ribbon

  1. package com.xhx.springcloud.config;
  2. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.client.RestTemplate;
  6. /**
  7. * xuhaixing
  8. * 2018/6/3 16:24
  9. */
  10. @Configuration
  11. public class RibbonConfig {
  12. @Bean
  13. @LoadBalanced
  14. public RestTemplate getRestTemplate(){
  15. return new RestTemplate();
  16. }
  17. }

配置文件:

  1. server:
  2. port: 8085
  3. eureka:
  4. client:
  5. service-url:
  6. defaultZone: http://localhost:8761/eureka
  7. spring:
  8. application:
  9. name: eureka-client

controller,注入restTemplate,通过服务名称请求eureka-server

  1. package com.xhx.springcloud.controller;
  2. import com.netflix.discovery.converters.Auto;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import org.springframework.web.client.RestTemplate;
  7. /**
  8. * xuhaixing
  9. * 2018/6/3 16:27
  10. */
  11. @RestController
  12. public class RibbionController {
  13. @Autowired
  14. private RestTemplate restTemplate;
  15. @RequestMapping(value = "getName")
  16. public String getName(String name){
  17. return restTemplate.getForObject("http://eureka-service/hello/getName?name="+name,String.class);
  18. }
  19. }

用postman请求,一次返回 xxx1 一次返回xxx2,采用的轮询策略

70 1

实时内容请关注微信公众号,公众号与博客同时更新:程序员星星

实时内容请关注微信公众号,公众号与博客同时更新

发表评论

表情:
评论列表 (有 0 条评论,351人围观)

还没有评论,来说两句吧...

相关阅读