spring-cloud五大基本组件

Eureka注册中心

1.eureka注册中心需要对外暴露,其他服务需要注册到注册中心
2.注册中心会将服务的地址列表发送给订阅服务的消费者
3.各种服务需要定期向eureka发送心跳,刷新自己的状态
4.服务下线则注册中心就会剔除掉服务
5.如果某个服务无法正常工作,但并没有向服务器提出下线,则会创建一个定时任务,每隔60s发送一次确认,如果90s没有响应则剔除.
6.如果15分钟之内某个服务的续约成功率不足85%,则会剔除

Zuul网关

作用:用来控流,控制,路由
1.身份验证与安全,识别访问某个资源的需求,不满足则拒绝访问
2.审查,监控
3.动态路由
4.负载均衡
5.限流

Ribbon 负载均衡

Ribbon默认采用轮训算法,其他算法还有hash、权重、一致性hash

Feign服务调用

解决了不同服务之间的相互调用问题,feign自动实现了负载均衡,熔断

Hystix 熔断器

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker //开启断路器
public class UserServiceStartApp {
    public static void main(String[] args) {
        SpringApplication.run(UserServiceStartApp.class);
    }
}
@RestController
public class UserController {
    @Autowired
    private GoodFeign goodFeign;
    @GetMapping("showGoodById/{id}")
    @HystrixCommand(fallbackMethod = "showGoodByIdFallBack")
    public List<Good> showGoodById(@PathVariable Integer id){
        List<Good> goods = goodFeign.getGoodById(id);
                if (goods == null){
                    System.out.printf("这个id没有数据");
                    return  new ArrayList<>();
                }
        return goods;

    }
    /*@GetMapping("test1")
    public String test1(){
       return goodFeign.test();
    }*/
    public List<Good> showGoodByIdFallBack(Integer id){
        System.out.printf("showGoodById出现问题的降级方法");
        return new ArrayList<>();
    }
}

@FeignClient(value = "goodservice",fallback = GoodFeignImpl.class)
public interface GoodFeign {
    @GetMapping("getGoodById/{id}")
    public List<Good> getGoodById(@PathVariable Integer id);
   /* @HystrixCommand(fallbackMethod ="testFallBack" )
    public String test();*/
}
@Component
public class GoodFeignImpl implements GoodFeign {
    @Override
    public List<Good> getGoodById(Integer id) {
        System.out.printf("Feign+getGoodById方法的降级");
        return new ArrayList<>();
    }
    
}

1.线程隔离
为每一个服务分配一定数量的线程池,当线程池用完了,等一段时间还是没有通过,则服务请求失败断开连接
2.服务降级
为每个服务自定义一个降级方法,当服务出错时提高用户的使用体验
3.服务熔断
最近20次请求中,如果超过一半的请求失败,则打开断路器,后面的请求直接拒绝访问.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值