zoukankan      html  css  js  c++  java
  • springcloud(十四)、ribbon负载均衡策略应用案例

    一、eureka-server服务中心项目不再创建

    二、eureka-common-empdept公共组件项目不再掩饰

    三、创建eureka-client-provider-empdept-one提供者项目

    3.1 结构如下

    pom.xml文件内容如下:

     1 <dependencies>
     2         <dependency>
     3             <groupId>cn.kgc</groupId>
     4             <artifactId>eureka-common-empdept</artifactId>
     5             <version>0.0.1-SNAPSHOT</version>
     6         </dependency>
     7         <dependency>
     8             <groupId>org.springframework.boot</groupId>
     9             <artifactId>spring-boot-starter-jdbc</artifactId>
    10         </dependency>
    11         <dependency>
    12             <groupId>org.springframework.boot</groupId>
    13             <artifactId>spring-boot-starter-web</artifactId>
    14         </dependency>
    15         <dependency>
    16             <groupId>org.mybatis.spring.boot</groupId>
    17             <artifactId>mybatis-spring-boot-starter</artifactId>
    18             <version>2.1.0</version>
    19         </dependency>
    20         <dependency>
    21             <groupId>org.springframework.cloud</groupId>
    22             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    23         </dependency>
    24 
    25         <dependency>
    26             <groupId>mysql</groupId>
    27             <artifactId>mysql-connector-java</artifactId>
    28             <version>5.1.38</version>
    29         </dependency>
    30         <dependency>
    31             <groupId>org.springframework.boot</groupId>
    32             <artifactId>spring-boot-starter-test</artifactId>
    33             <scope>test</scope>
    34         </dependency>
    35     </dependencies>
    pom.xml

    3.2 DeptMapper.java

     1 package cn.kgc.mapper;
     2 
     3 import cn.kgc.vo.Dept;
     4 import org.apache.ibatis.annotations.Select;
     5 
     6 import java.util.List;
     7 
     8 /**
     9  * Created by Administrator on 2019/8/29.
    10  */
    11 public interface DeptMapper {
    12     @Select("select * from dept")
    13     List<Dept> optionData();
    14 }
    DeptMapper.java

    3.3EmpMapper.java

     1 package cn.kgc.mapper;
     2 
     3 import cn.kgc.vo.Emp;
     4 import org.apache.ibatis.annotations.Select;
     5 
     6 import java.util.List;
     7 import java.util.Map;
     8 
     9 /**
    10  * Created by Administrator on 2019/8/29.
    11  */
    12 public interface EmpMapper {
    13     List<Map<String,Object>> showData(Emp emp);
    14 }
    EmpMapper.java

    3.4EmpMapper.xml

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper
     3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     5 <mapper namespace="cn.kgc.mapper.EmpMapper">
     6     <select id="showData" resultType="map" parameterType="emp">
     7         select d.dname,d.loc,e.* from emp e,dept d where e.deptno=d.deptno
     8         <if test="empno!=null">
     9             and e.empno=#{empno}
    10         </if>
    11         <if test="deptno!=null and deptno!=-1">
    12             and e.deptno=#{deptno}
    13         </if>
    14     </select>
    15 </mapper>
    EmpMapper.xml

    3.5DeptService.java

     1 package cn.kgc.service;
     2 
     3 import cn.kgc.vo.Dept;
     4 import org.apache.ibatis.annotations.Select;
     5 
     6 import java.util.List;
     7 
     8 /**
     9  * Created by Administrator on 2019/8/29.
    10  */
    11 public interface DeptService {
    12     List<Dept> optionData();
    13 }
    DeptService.java

    3.6  DeptServiceImpl.java

     1 package cn.kgc.service;
     2 
     3 import cn.kgc.mapper.DeptMapper;
     4 import cn.kgc.vo.Dept;
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.stereotype.Service;
     7 
     8 import java.util.List;
     9 
    10 @Service
    11 public class DeptServiceImpl implements DeptService{
    12 
    13     @Autowired
    14     private DeptMapper deptMapper;
    15 
    16     public List<Dept> optionData() {
    17         return deptMapper.optionData();
    18     }
    19 }
    DeptServiceImpl.java

    3.7 EmpService.java

     1 package cn.kgc.service;
     2 
     3 import cn.kgc.vo.Emp;
     4 
     5 import java.util.List;
     6 import java.util.Map;
     7 
     8 /**
     9  * Created by Administrator on 2019/8/29.
    10  */
    11 public interface EmpService {
    12     List<Map<String,Object>> showData(Emp emp);
    13 }
    EmpService.java

    3.8  EmpServiceImpl.java

     1 package cn.kgc.service;
     2 
     3 import cn.kgc.mapper.EmpMapper;
     4 import cn.kgc.vo.Emp;
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.stereotype.Service;
     7 import org.springframework.transaction.annotation.Transactional;
     8 
     9 import java.util.List;
    10 import java.util.Map;
    11 
    12 @Service
    13 @Transactional
    14 public class EmpServiceImpl implements EmpService{
    15     @Autowired
    16     private EmpMapper empMapper;
    17 
    18     public List<Map<String, Object>> showData(Emp emp) {
    19         return empMapper.showData(emp);
    20     }
    21 }
    EmpServiceImpl.java

    3.9 CenterController,java

     1 package cn.kgc.controller;
     2 
     3 import cn.kgc.mapper.DeptMapper;
     4 import cn.kgc.mapper.EmpMapper;
     5 import cn.kgc.service.DeptService;
     6 import cn.kgc.service.EmpService;
     7 import cn.kgc.vo.Dept;
     8 import cn.kgc.vo.Emp;
     9 import org.slf4j.Logger;
    10 import org.slf4j.LoggerFactory;
    11 import org.springframework.beans.factory.annotation.Autowired;
    12 import org.springframework.web.bind.annotation.RequestBody;
    13 import org.springframework.web.bind.annotation.RequestMapping;
    14 import org.springframework.web.bind.annotation.RequestParam;
    15 import org.springframework.web.bind.annotation.RestController;
    16 
    17 import java.util.List;
    18 import java.util.Map;
    19 
    20 @RestController
    21 public class CenterController {
    22 
    23     @Autowired
    24     private EmpService empService;
    25 
    26     @Autowired
    27     private DeptService deptService;
    28 
    29     private Logger logger= LoggerFactory.getLogger(CenterController.class);
    30 
    31     @RequestMapping("/ribbon.do")
    32     public String ribbonTest(@RequestParam("count") Integer count){
    33         logger.info("provider>>> "+count+" <<<eureka-client-provider-one-8762");
    34         return "provider的ribbon测试";
    35     }
    36 
    37     @RequestMapping("/option.do")
    38     public List<Dept> optionData() {
    39         return deptService.optionData();
    40     }
    41 
    42     @RequestMapping("/data.do")
    43     public List<Map<String, Object>> showData(@RequestBody Emp emp) {
    44         return empService.showData(emp);
    45     }
    46 }
    CenterController.java

    3.10 启动类的设置

     1 package cn.kgc;
     2 
     3 import org.mybatis.spring.annotation.MapperScan;
     4 import org.springframework.boot.SpringApplication;
     5 import org.springframework.boot.autoconfigure.SpringBootApplication;
     6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
     7 
     8 @MapperScan("cn.kgc.mapper")
     9 @EnableEurekaClient
    10 @SpringBootApplication
    11 public class EurekaClientProviderEmpdeptOneApplication {
    12 
    13     public static void main(String[] args) {
    14         SpringApplication.run(EurekaClientProviderEmpdeptOneApplication.class, args);
    15     }
    16 
    17 }
    启动类

    3.11 编写属性文件

     1 spring.application.name=provider-empdept
     2 
     3 server.port=8762
     4 
     5 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
     6 
     7 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
     8 spring.datasource.url=jdbc:mysql://192.168.117.145:3306/kh66
     9 spring.datasource.username=root
    10 spring.datasource.password=ok
    11 
    12 mybatis.type-aliases-package=cn.kgc.vo
    13 
    14 mybatis.mapper-locations=classpath:mapper/*.xml
    application.properties

    3.12 启动eureka-server、eureka-client-provider-empdept-one 查看结果

     

    四、创建eureka-client-provider-empdept-two提供者项目,所有的代码模块和eureka-client-provider-empdept-one一模一样,只是端口号不一样即可

    五、创建eureka-client-consumer-empdept-p-one消费者项目,

    5.1 结构如下

    5.2 编写 EmpDeptProviderFeign.java

     1 package cn.kgc.fegin;
     2 
     3 import cn.kgc.vo.Dept;
     4 import cn.kgc.vo.Emp;
     5 import org.springframework.cloud.openfeign.FeignClient;
     6 import org.springframework.web.bind.annotation.RequestBody;
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 import org.springframework.web.bind.annotation.RequestParam;
     9 
    10 import java.util.List;
    11 import java.util.Map;
    12 
    13 @FeignClient("provider-empdept")
    14 public interface EmpDeptProviderFeign {
    15 
    16     @RequestMapping("/ribbon.do")
    17     public String ribbonTest(@RequestParam("count") Integer count);
    18 
    19     @RequestMapping("/option.do")
    20     public String optionData();
    21 
    22     @RequestMapping("/data.do")
    23     public String showData(@RequestBody Emp emp);
    24 }
    EmpDeptProviderFeign.java

    5.3 CenterController.java

     1 package cn.kgc.fegin;
     2 
     3 import cn.kgc.vo.Dept;
     4 import cn.kgc.vo.Emp;
     5 import org.springframework.cloud.openfeign.FeignClient;
     6 import org.springframework.web.bind.annotation.RequestBody;
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 import org.springframework.web.bind.annotation.RequestParam;
     9 
    10 import java.util.List;
    11 import java.util.Map;
    12 
    13 @FeignClient("provider-empdept")
    14 public interface EmpDeptProviderFeign {
    15 
    16     @RequestMapping("/ribbon.do")
    17     public String ribbonTest(@RequestParam("count") Integer count);
    18 
    19     @RequestMapping("/option.do")
    20     public String optionData();
    21 
    22     @RequestMapping("/data.do")
    23     public String showData(@RequestBody Emp emp);
    24 }
    CenterController.java

    5.4 application.properties属性文件编写

    1 spring.application.name=consumer-empdept-p-one
    2 
    3 server.port=8764
    4 
    5 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
    application.properties

    5.5 编写启动类

     1 package cn.kgc;
     2 
     3 import org.springframework.boot.SpringApplication;
     4 import org.springframework.boot.autoconfigure.SpringBootApplication;
     5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
     6 import org.springframework.cloud.netflix.ribbon.RibbonClient;
     7 import org.springframework.cloud.openfeign.EnableFeignClients;
     8 
     9 @EnableFeignClients
    10 @EnableEurekaClient
    11 @SpringBootApplication
    12 public class EurekaClientConsumerEmpdeptPOneApplication {
    13 
    14     public static void main(String[] args) {
    15         SpringApplication.run(EurekaClientConsumerEmpdeptPOneApplication.class, args);
    16     }
    17 
    18 }
    启动类

    5.6  启动项目顺序为:eureka-server、eureka-client-provider-empdept-one、eureka-client-provider-empdept-two、eureka-client-consumer-empdept-p-one

    因为没有设置负载均衡策略,因此他会按照默认方式既 轮询策略进行调用,

     

     

    5.7 再次选择配置ribbon负载均衡策略为随机分配,再看效果,属性文件设置如下

    1 spring.application.name=consumer-empdept-p-one
    2 
    3 server.port=8764
    4 
    5 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
    6 
    7 #随机分配策略
    8 provider-empdept.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
    application.properties

    5.8 再次按照刚才的启动顺序启动,再来查看

     

     

    可以挨个把负载均衡策略试一下

  • 相关阅读:
    ubuntu 如何 su 到 root(作为 root 用户操作)
    centos6.5 redis 安装配置及java调用
    springmvc 国际化
    springmvc 整合数据验证框架 jsr
    springmvc 整合shiro
    centos 6.5 安装mysql
    hive 报错 java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.metastore.HiveMetaStoreClient
    centos 关闭防火墙
    client.HConnectionManager$HConnectionImplementation: Can't get connection to ZooKeeper: KeeperErrorCode = ConnectionLoss for /hbase
    fms +fme 视频直播
  • 原文地址:https://www.cnblogs.com/holly8/p/11430924.html
Copyright © 2011-2022 走看看