SpringCloud与SpringCloudAlibaba学习笔记!!

目录

SpringCloud

服务注册中心

eureka

zookepper

consul

服务调用负载均衡

Ribbon

OpenFeign

Hystrix 服务治理

Gateway 网关

Gateway

SpringCloud Config 分布式配置中心, BUS 消息总线

分布式配置中心 SpringCloud Config

消息总线 Bus

SpringCloud Stream 消息驱动

SpringCloud Stream 消息提供者项目配置 (简单使用)

SpringCloud Stream 分组消费 & 持久化

Sleuth 分布式请求链路跟踪

SpringCloud Alibaba

Nacos 注册中心

添加依赖

项目配置文件

主程序启动

结合 Openfeign 调用 服务

Nacos config配置中心

Nacos config配置中心 分类配置

Sentinel 服务熔断与降级

Sentinel 服务被管方 配置

seata 分布式事务


SpringCloud

服务注册中心

eureka

ap 高可用 分布式容错

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
eureka:
  instance:
    hostname: eureka7003.com #eureka服务端的实例名称
    instance-id: payment8001 
    prefer-ip-address: true
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      #集群指向其它eureka
      #defaultZone: http://eureka7002.com:7002/eureka/
      #单机就是7001自己
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
    #server:
    #关闭自我保护机制,保证不可用服务被及时踢除
    #enable-self-preservation: false
    #eviction-interval-timer-in-ms: 2000

Ribbon 启用负载均衡

@EnableEurekaServer
@EnableDiscoveryClient

@LoadBalanced
public RestTemplate getTemp() {
    return new RestTemplate();
}

zookepper

cp 强一致 分布式容错

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.6.1</version>
</dependency>
spring:
  application:
    name: cloud-zoo-consumer-order
  cloud:
    zookeeper:
      connect-string: 192.168.10.58:2181
@SpringBootApplication
@EnableDiscoveryClient

consul

cp 强一致 分布式容错

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
spring:
  application:
    name: consul-payment-provider
  cloud:
    consul:
      host: 192.168.10.58
      port: 8500
      discovery:
        service-name: ${spring.application.name}
@SpringBootApplication
@EnableDiscoveryClient

服务调用负载均衡

Ribbon

Ribbon 切换 负载规则

  1. 在springboot 包扫描外层建立 配置

@Configuration
public class Myrule {
    @Bean
    public IRule initRule() {
        return new RandomRule();
    }
}
  1. 启动类给指定服务加载随机方法

@RibbonClient(name = "CLOUD-PAYMENT-SERVICE", configuration = Myrule.class)

OpenFeign

  1. 添加maven依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
  1. 启动类启用Feign

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
  1. 新建接口 并注册Feign信息

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")  //提供方服务名
public interface Service {
    @GetMapping(value = "/payment/get/{id}")
    Response<Payment> getPaymentById(@PathVariable("id") Long id);
}
  1. 提供方接口演示

@GetMapping(value = "/payment/get/{id}")
public Response<Payment> getPaymentById(@PathVariable("id") Long id) {
    Payment payment = paymentService.getPaymentById(id);

    if (payment != null) {
        return Result.success(200, "查询成功,serverPort:  " + serverPort, payment);
    } else {
        return Result.success(444, "没有对应记录,查询ID: " + id, null);
    }
}

OpenFeign超时设置

ribbon:
#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
  ReadTimeout: 5000
#指的是建立连接后从服务器读取到可用资源所用的时间
  ConnectTimeout: 5000

OpenFeign 日志打印功能

  1. 配置Openfeign 日志级别

@Configuration
public class FeignLogConfig {
    @Bean
    public Logger.Level getLevel() {
        return Logger.Level.FULL;
    }
}
  1. yml 项目配置文件中,给指定Feign interface 配置日志级别

logging:
  level:
    ml.ytooo.feignservice.Service: debug

Hystrix 服务治理

  • 服务降级 出险异常时,返回友好提示,防止程序异常或者阻塞

  • 服务熔断 保险丝,当超出服务承载能力时,返回提示,拒绝请求

  • 服务限流 闸门,配置服务承载能力

Hystrix

服务降级

当服务处理超时或者运行异常时,启动备选方案返回给调用者预期结果

主方法

@EnableCircuitBreaker

需要降级处理的程序

其中

  • paymentInfo_TimeOut 为预计超时程序

  • paymentInfo_TimeOut_Handler 为超时备选方案

@HystrixCommand(fallbackMethod = "paymentInfo_TimeOut_Handler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
})
public String paymentInfo_TimeOut(Integer id) {

    int time = 5;
    try { TimeUnit.MILLISECONDS.sleep(time * 1000); } catch (InterruptedException e) { e.printStackTrace(); }
    return "线程池:  " + Thread.currentThread().getName() + " paymentInfo_TimeOut,id:  " + id + "\t" + "O(∩_∩)O哈哈~" + "  耗时(秒): " + time;
}

public String paymentInfo_TimeOut_Handler(Integer id) {
    return "线程池:  " + Thread.currentThread().getName() + " paymentInfo_TimeOut_Handler,id:  " + id +
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值