微服务------(2)--------ribbon+fegin

Eureka只作为服务的调用与发现,需要ribbon进行调用

ribbon是什么?


Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具。
简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服
务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中
列出Load Balancer(简称LB)后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接
等)去连接这些机器。我们也很容易使用Ribbon实现自定义的负载均衡算法。

Eureka的依赖中包括了Ribbon

要使用ribbon,只需要一个注解:

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

服务端负载均衡与客户端负载均衡

核心组件:IRule


IRule是什么? 它是Ribbon对于负载均衡策略实现的接口,  说白了就是你实现这个接口,就能自定义负载均衡策略,默认负载均衡策略

RetryRule=====规定时间内调用失败======用hystrix--返回降级方法

查看RandomRule算法源码------------->也就可以改写了

        if (server == null) {
                /*
                 * The only time this should happen is if the server list were
                 * somehow trimmed. This is a transient condition. Retry after
                 * yielding.
                 */
                Thread.yield();
                continue;
            }

 Thread.yield();让出当前时间片

手写负载均衡算法

见代码user——>iRule.myIRule

实现不同微服务,不同算法

新建com.configIRule包,完成不同微服务,不同算法

查看官网提示

The CustomConfiguration clas must be a @Configuration class, but take care that it is not in a @ComponentScan for the main application context. Otherwise, it is shared by all the @RibbonClients. If you use @ComponentScan (or @SpringBootApplication), you need to take steps to avoid it being included (for instance, you can put it in a separate, non-overlapping package or specify the packages to scan explicitly in the @ComponentScan).

启动类添加不同服务对应不同算法

@RibbonClients({
        @RibbonClient(name = "server-power",configuration= PowerRuleConfig.class),
        @RibbonClient(name = "server-order",configuration = OrderRuleConfig.class)
       })

ribbon+restTemplate实现

@RestController
public class UserController {
    /**
    spring提供用于不同系统间通信
     */
    @Autowired
    RestTemplate restTemplate;

    //通过ribbon调用
    private static final String POWER_URL="http://SERVER-POWER";
    private static final String ORDER_URL="http://server-order";

    @RequestMapping("/getUser.do")
    public R getUser(){
        Map<String,Object> map=new HashMap<>();
        map.put("key","用户数据");
        return R.success("返回成功",map);
    }

    @RequestMapping("/getPower.do")
    public R getPower(){
        return R.success("操作成功",restTemplate.getForObject(POWER_URL+"/getPower",R.class));
    }

    @RequestMapping("/getOrder.do")
    public R getOrder(){
        return R.success("操作成功",restTemplate.getForObject(ORDER_URL+"/getOrder.do",R.class));
    }
}

feign

feign是什么 :
Feign是一个声明式WebService客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一
个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring
Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和
Ribbon组合使用以支持负载均衡。
feign 能干什么:
Feign旨在使编写Java Http客户端变得更容易。 前面在使用Ribbon+RestTemplate时,利用RestTemplate对http
请求的封装处理,形成了一套模版化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往
一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以,
Feign在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义。在Feign的实现下,我们只需
创建一个接口并使用注解的方式来配置它(以前是Dao接口上面标注Mapper注解,现在是一个微服务接口上面标注一
个Feign注解即可),即可完成对服务提供方的接口绑定,简化了使用Spring cloud Ribbon时,自动封装服务调用客
户端的开发量。

添加依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

添加接口进行调用

@FeignClient(name ="server-power")
public interface PowerFeignClient {
    @RequestMapping("/getPower.do")
    public Object getPower();
}




@Autowired
private PowerFeignClient powerFeignClient;

 

feign默认使用ribbon的负载均衡算法

spring-cloud-alibaba正在开发,以后可能整合在一起

 

 

ribbon默认超时时间3s

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值