Ribbon负载均衡的实现

导读:本篇文章讲解 Ribbon负载均衡的实现,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

一,创建服务提供者

创建三个服务提供者分别为8001,8002,8003端口并连接不同的数据库来模拟来自不同的服务器从而实现负载均衡
在这里插入图片描述
三个服务提供者实现相同的业务并提供相同的Rest接口
在这里插入图片描述

mapper

<mapper namespace="com.yyx.springcloud.dao.DeptDao">
    <insert id="addDept" parameterType="Dept">
    insert into dept(dname,db_source) values(#{dname},DATABASE())
    </insert>
    <select id="queryAll" resultType="Dept">
        select * from dept
    </select>
    <select id="queryById" parameterType="Long" resultType="Dept">
        select * from dept where deptno=#{deptno}
    </select>
</mapper>

controller

@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    @PostMapping("/dept")
    public int addDept(@RequestBody Dept dept){
        return deptService.addDept(dept);
    }
    @GetMapping("/dept/{id}")
    public Dept queryById(@PathVariable("id") Long id){
        return deptService.queryById(id);
    }
    @GetMapping("/dept")
    public List<Dept> queryAll(){
        return deptService.queryAll();
    }
}

application.yml
三个服务提供者的配置中application.name和instance-id和所连接的数据库修改为自己对应的

server:
  port: 8001
#mybatis的配置
mybatis:
  type-aliases-package: com.yyx.springcloud.pojo
  mapper-locations: classpath:mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true
#spring的配置
spring:
  application:
    name: SPRINGCLOUD-PROVIDER-DEPT
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mydb4?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 123456
#eureka的配置
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: springcloud-provider-dept8001
#info配置
info:
  app.name: springcloud-study
  company.name: yangyongxin.top

所需依赖:(注意 服务提供者中需导入eureka依赖)

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.yyx</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

二,eureka注册服务

在这里插入图片描述
启动类中添加@EnableEurekaServer注解

application.yml中配置(这里做了eureka集群)

server:
  port: 7001

#eureka配置
eureka:
  instance:
    hostname: eureka7001.com #eureka服务端的实例名称
  client:
    register-with-eureka: false #表示是否向eureka注册中心注册自己
    fetch-registry: false #fetch-registry为false,则表示自己为注册中心
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

三,服务消费者中设置Ribbon负载均衡

在这里插入图片描述
ConfigBean
注入RestTemplate并添加注解LoadBalanced配置负载均衡实现RestTemplate

@Configuration
public class ConfigBean {
    @Bean
    @LoadBalanced//配置负载均衡实现RestTemplate
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

controller
Ribbon实现负载均衡时间不应写为服务的某个地址,写为服务的名字,负载均衡随机访问这个服务所有实例地址

@RestController
public class DeptConsumerController {
    @Autowired
    private RestTemplate restTemplate;//提供多种便捷访问远程http服务的方法,简单的RestFul服务模板
    //Ribbon实现负载均衡时间不应写为服务的某个地址,写为服务的名字,负载均衡随机访问这个服务所有地址
    //private static final String REST_URL_PREFIX="http://localhost:8001";
    private static final String REST_URL_PREFIX="http://SPRINGCLOUD-PROVIDER-DEPT";
    @RequestMapping("/consumer/dept/add")
    public int add(Dept dept){
        return restTemplate.postForObject(REST_URL_PREFIX+"/dept",dept,int.class);
    }
    @RequestMapping("/consumer/dept/get/{deptno}")
    public Dept dept(@PathVariable("deptno") Long deptno){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/"+deptno,Dept.class);
    }
    @RequestMapping("/consumer/dept/list")
    public List<Dept> dept(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept",List.class);
    }
}

application.yml中配置(这里做了eureka集群的配置)

server:
  port: 80
#Eureka配置
eureka:
  client:
    register-with-eureka: false #不向eureka中注册自己
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

所需依赖(注意 服务消费者中需同时添加eureka和ribbon依赖

        <dependency>
            <groupId>com.yyx</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
    </dependencies>

四,测试

访问服务消费者接口地址测试
刷新后会轮询访问三个服务提供者(这里使用数据库名字段用于区分是哪个服务提供者提供的服务)
在这里插入图片描述
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/4986.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!