Ribbon实现负载均衡

比眉伴天荒 2023-03-01 12:56 108阅读 0赞

环境:IDEA,springboot2.3.1

eureka7001这个是我本地配置了hosts,可以改为localhost

1:首先是注册中心pom文件,添加eureka服务端,然后是application.yml文件配置

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  4. </dependency>
  5. server:
  6. port: 7001
  7. eureka:
  8. instance:
  9. hostname: eureka7001
  10. client:
  11. register-with-eureka: false
  12. fetch-registry: false
  13. service-url:
  14. defaultZone: http://eureka7001:7001/eureka/

完成之后在启动类添加相应注解@EnableEurekaServer

  1. @SpringBootApplication
  2. @EnableEurekaServer
  3. public class Eureka7001Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Eureka7001Application.class, args);
  6. }
  7. }

启动项目访问http://eureka7001:7001/

20200726142004238.png

服务注册中心完成

2:ribbon服务提供者

首先是pom和application配置

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>
  5. server:
  6. port: 8001
  7. mybatis:
  8. config-location: classpath:mybatis/mybatis.cfg.xml #mybatis
  9. type-aliases-package: com.sinosoft.springcloud.cloudapi.entities #所有实体类所在路径
  10. mapper-locations: classpath:mybatis/mapper/**/*.xml
  11. spring:
  12. application:
  13. name: product-dept
  14. datasource:
  15. type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型
  16. driver-class-name: com.mysql.cj.jdbc.Driver #mysql驱动包
  17. url: jdbc:mysql://ip:3306/cloud01 #数据库地址
  18. username: root
  19. password: MyNewPass4!
  20. dbcp2:
  21. min-idle: 5 #数据库连接池最少维持连接数
  22. initial-size: 5
  23. max-total: 5
  24. max-wait-millis: 200
  25. eureka:
  26. client:
  27. service-url:
  28. defaultZone: http://eureka7001:7001/eureka/ #注册中心地址

然后是controller,service以及dao代码

  1. @RestController
  2. public class DeptController {
  3. @Autowired
  4. private DeptService deptService;
  5. @RequestMapping("/deptList")
  6. public List<DeptEntity> queryDeptEntityList(){
  7. return deptService.queryListDeptEmtity();
  8. }
  9. }
  10. @Service
  11. public class DeptServiceImpl implements DeptService {
  12. @Autowired
  13. private DeptDao dao;
  14. @Override
  15. public List<DeptEntity> queryListDeptEmtity() {
  16. return dao.selListDeptEntity();
  17. }
  18. }
  19. @Mapper
  20. public interface DeptDao {
  21. List<DeptEntity> selListDeptEntity();
  22. }

然后是启动类,添加@EnableEurekaClient

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. public class Product8001Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Product8001Application.class, args);
  6. }
  7. }

启动访问http://localhost:8001/deptList

2020072614293693.png

再新建一个相同的服务提供者,端口号改为8002,数据库使用另一个,这样查询的数据不一样就能保证使用的是不同的服务提供者,不再贴出代码

3:消费者

消费者pom和application.yml,有时候坐标无效,可以添加版本号试试

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  8. </dependency>
  9. server:
  10. port: 80
  11. spring:
  12. application:
  13. name: dept80
  14. eureka:
  15. client:
  16. service-url:
  17. defaultZone: http://eureka7001:7001/eureka/

然后是controller代码,注意product-dept是服务提供者名称

  1. @RestController
  2. public class DeptController {
  3. @Autowired
  4. private RestTemplate restTemplate;
  5. private static String RIBBO_URL="http://product-dept";
  6. @SuppressWarnings("unchecked")
  7. @RequestMapping("/deptList")
  8. public List<DeptEntity> queryEntityList(){
  9. return restTemplate.getForObject(RIBBO_URL+"/deptList",List.class);
  10. }
  11. }

新建类,添加注解@LoadBalanced实现负载均衡

  1. @Configuration
  2. public class ConfigBean {
  3. @Bean
  4. @LoadBalanced
  5. public RestTemplate getRestTemplate(){
  6. return new RestTemplate();
  7. }
  8. }

然后启动类

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. public class Dept80Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Dept80Application.class, args);
  6. }
  7. }

多次访问http://localhost:80/deptList,发现会轮询访问两个微服务

至此ribbon的负载均衡完成

发表评论

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

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

相关阅读

    相关 负载均衡Ribbon

    1. Feign 默认集成了 Ribbon Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单。使用 Feign,只需要创建一个接口并注

    相关 负载均衡---ribbon

    Ribbon:提供云端负载均衡,有多种负载均衡策略可供选择,可配合服务发现和断路器使用。 上一篇简单讲解了eureka的使用,这一篇文章基于上一篇的基础上,讲一下spring

    相关 Ribbon负载均衡

    1. 集中式负载均衡 > 在客户端和服务端之间使用独立的负载均衡设施(可以是硬件,如F5, 也可以是软件,如nginx、LVS等), 由该设施负责把访问请求通过某种策略转发