您的位置:首页 > 编程语言 > Java开发

springCloud 学习笔记2 ribbon 实现客户端负载均衡

2019-01-04 20:43 766 查看
版权声明:尊重原创 https://blog.csdn.net/higher_gao/article/details/85798761

maven基础

  • 测试自定义负载均衡器,注解实现
  • 测试自定义负载均衡器,配置实现实现
  • ribbon基本介绍

    Ribbon是Netflix发布的负载均衡器,一个基于HTTP和TCP的客户端负载均衡工具,Spring Cloud集成了Ribbon,结合Eureka,可实现客户端的负载均衡

    项目案例 客户端实现负载均衡结构图

    准备工作

    1. 搭建eureak服务端,作为服务注册中心使用
    2. 搭建eureak客户端,作为服务提供者
    3. 搭建eureak客户端,作为服务消费者,集成ribbon模块

    搭建eureak 服务端

    1, pom文件配置

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    **属性文件配置**
    #服务端口
    server.port=8761
    #服务名称serviceCenter
    eureka.instance.hostname=yihongyuan
    #禁止本身注册
    eureka.client.register-with-eureka=false
    #禁止本身注册
    eureka.client.fetch-registry=false
    #服务中心地址
    eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

    2, 启动类

    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaRibboServerApplication {
    public static void main(String[] args) {
    SpringApplication.run(EurekaRibboServerApplication.class, args);
    }
    }

    搭建eureak客户端,服务提供者

    1, pom文件配置

    <dependency>
    <groupId>org.springframework.boot</groupId><
    4000
    /span>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    **属性文件配置**
    spring:
    application:
    name: polices
    eureka:
    client:
    eureka-server-url:
    defautZone: http://localhost:8761/eureka/

    2, 启动类

    @SpringBootApplication
    @EnableEurekaClient
    @ComponentScan(value = "com")
    public class EureakRibbonProductApplication {
    
    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String port = scan.nextLine();
    new SpringApplicationBuilder(EureakRibbonProductApplication.class).properties("server.port=" + port).run(args);
    }
    
    }

    3, 业务类

    @RestController
    public class PoliceController {
    
    @RequestMapping(value = "/call/{id}", method = RequestMethod.GET,
    produces = MediaType.APPLICATION_JSON_VALUE)
    public Police call(@PathVariable Integer id, HttpServletRequest request) {
    Police p = new Police();
    p.setId(id);
    p.setName("angus");
    p.setMessage(request.getRequestURL().toString());
    return p;
    }
    }

    4, 注册中心查看

    5, 业务类测试

    搭建eureak客户端,服务消费者,集成ribbon模块

    1, pom文件配置

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
      **属性文件配置**
    server:
    port: 9000
    spring:
    application:
    name: erueka_ribbon_customer
    eureka:
    client:
    eureka-server-url:
    defautZone: http://localhost:8761/eureka/

    2, 启动类

    @SpringBootApplication
    @EnableDiscoveryClient
    @ComponentScan(value = "com")
    @Configuration
    public class EurekaRibbonCustomerApplication {
    
    public static void main(String[] args) {
    SpringApplication.run(EurekaRibbonCustomerApplication.class, args);
    }
    
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
    return new RestTemplate();
    }
    }

    3, 业务类

    @Controller
    public class MyController {
    
    @Autowired
    RestTemplate tpl;
    
    @GetMapping("/router")
    @ResponseBody
    public String router() {
    System.out.println("rout...");
    //   RestTemplate tpl = getRestTemplate();
    String json = tpl.getForObject("http://polices/call/1", String.class);
    return json;
    }

    4, 注册中心查看
    如图见

    5, 业务类测试


    客户端默认使用轮询的方式访问8080,,8081实例

    测试自定义负载均衡器,注解实现

    1,自定义配置类

    规则类 rule
    public class MyRule implements IRule {
    
    private ILoadBalancer lb;
    
    public Server choose(Object key) {
    System.out.println("这是自定义的规则类");
    Random r = new Random();
    int randomNum = r.nextInt(10);
    List<Server> servers = lb.getAllServers();
    if (randomNum > 7) {
    Server s = getServerByPort(servers, 8081);
    return s;
    }
    return getServerByPort(servers, 8080);
    }
    
    private Server getServerByPort(List<Server> servers, int port) {
    for (Server s : servers) {
    if (s.getPort() == port) {
    return s;
    }
    }
    return null;
    }
    
    public void setLoadBalancer(ILoadBalancer lb) {
    this.lb = lb;
    }
    
    public ILoadBalancer getLoadBalancer() {
    return lb;
    }
    }
    配置类
    public class MyConfig {
    
    @Bean
    public IRule getRule() {
    return new MyRule();
    }
    }
    ribbon客户端类
    @RibbonClient(name = "polices", configuration = MyConfig.class)
    public class MyClient {
    
    }

    2,测试
    在访问http://127.0.0.1:9000/router 时候,服务提供着实例 8080被优先访问到

    测试自定义负载均衡器,配置实现实现

    1,在配置文件中使用配置类

    ## ribbon自定义规则类
    polices:
    ribbon:
    NFLoadBalancerRuleClassName: com.bw.rule.MyRule

    2,测试

    内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: