zoukankan      html  css  js  c++  java
  • SpringCloud组件---Ribbon

    Ribbon的介绍:
    
        Ribbon是spring cloud的一个组件,是Netflix发布的负载均衡器,有助于控制HTTP客户端行为。为Ribbon配置服务提供者地址列表后,
    Ribbon就可基于负载均衡算法,自动帮助服务消费者请求。
        Ribbon默认提供的负载均衡算法有:轮询,随机,重试法,加权等。当然,我们也可以用自己定义负载均衡算法。
        
        Ribbon的使用:
            1. 在微服务(consumer端)添加依赖,因为eureka-client已经集成了eureka,所以只需添加eureka客户端依赖即可:
                <!--eureka客户端-->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
                </dependency>
                
            2. 使用注解@LoadBalanced即可:Ribbon集成RestTemplate,实现了负载均衡
                
                Bean
                @LoadBalanced//Ribbon集成RestTemplate,实现了负载均衡
                public RestTemplate restTemplate(){
                    return new RestTemplate();
                }
                
            3. 使用RestTemplate调用即可,Ribbon默认采用轮询策略:
                
                调用:user-provider为集群中的微服务名称
                User user = restTemplate.getForObject("http://user-provider/user/findByUser", User.class);
                
                
            
                user-provider端的Controller:URL:http://localhost:18082:/user/findByUser/张三
                    package com.it.controller;
    
                    import com.it.pojo.User;
                    import com.it.service.UserService;
                    import org.springframework.beans.factory.annotation.Autowired;
                    import org.springframework.web.bind.annotation.GetMapping;
                    import org.springframework.web.bind.annotation.RequestMapping;
                    import org.springframework.web.bind.annotation.RestController;
    
                    import java.util.List;
    
                    /**
                     * ToDo
                     *
                     * @author Lyle
                     * @date 2020/4/3
                     */
                    @RestController
                    @RequestMapping(value = "/user")
                    public class UserController {
    
                        @Autowired
                        private UserService userService;
    
                        @RequestMapping(value = "findAll")
                        public List<User> findAll(){
                            return userService.findAll();
                        }
    
                        @RequestMapping(value = "findByUser")
                        public User findByUser(){
                            System.out.println("111111111111111111111111");
                            return userService.findAll().get(0);
                        }
                    }
                    
                user-consumer端的Controller:
                    package com.it.controller;
    
                    import com.it.pojo.User;
                    import org.springframework.beans.factory.annotation.Autowired;
                    import org.springframework.cloud.client.ServiceInstance;
                    import org.springframework.cloud.client.discovery.DiscoveryClient;
                    import org.springframework.web.bind.annotation.PathVariable;
                    import org.springframework.web.bind.annotation.RequestMapping;
                    import org.springframework.web.bind.annotation.RestController;
                    import org.springframework.web.client.RestTemplate;
    
                    import java.util.List;
    
                    /**
                     * ToDo
                     *
                     * @author Lyle
                     * @date 2020/4/3
                     */
                    @RestController
                    @RequestMapping("/movie")
                    public class MovieController {
    
                        @Autowired
                        private RestTemplate restTemplate;
    
                        @Autowired
                        private DiscoveryClient discoveryClient;
    
                        @RequestMapping("/look/{username}")
                        public String look(@PathVariable(name = "username") String username){
                            //接收用户信息
                           
                            List<ServiceInstance> instances = discoveryClient.getInstances("user-provider");
                            ServiceInstance serviceInstance = instances.get(0);
                            User user = restTemplate.getForObject("http://user-provider/user/findByUser", User.class);
                            
                            System.out.println(username+"===========>"+user.getName());
                            return "success";
                        }
                    }
    
            4. Ribbon轮询策略的配置:
                在user-consumer端进行配置:
                    # 修改服务地址轮询策略,默认是轮询
                    user-provider:(指定要调用的微服务名称是user-provider,即spring.application.name的名称是user-provider)
                      ribbon:
                        #轮询
                        #NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule
                        #随机算法
                        #NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
                        #重试算法,该算法先按照轮询的策略获取服务,如果获取服务失败则在指定的时间内会进行重试,获取可用的服务
                        #NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RetryRule
                        #加权法,会根据平均响应时间计算所有服务的权重,响应时间越快服务权重越大被选中的概率越大。刚启动时如果同统计信息不足,则使用轮询的策略,等统计信息足够会切换到自身规则。
                        NFLoadBalancerRuleClassName: com.netflix.loadbalancer.ZoneAvoidanceRule (从可用的微服务系统中进行轮询)
            
                
    
    
    
        
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
        
                
  • 相关阅读:
    文字
    <script type="text/x-template"> 模板
    防xss攻击
    url
    symmfony
    composer
    header 和http状态码
    bootstrap
    linux的设置ip连接crt,修改主机名,映射,建文件
    Centos上传下载小工具lrzsz
  • 原文地址:https://www.cnblogs.com/lyle-liu/p/12628747.html
Copyright © 2011-2022 走看看