SpringCloud笔记总结1

认识微服务

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

1. 服务治理

分布式架构的要考虑的问题:

•服务拆分粒度如何?

•服务集群地址如何维护?

•服务之间如何实现远程调用?

•服务健康状态如何感知?

2 服务拆分及调用

在这里插入图片描述

3.Eureka注册中心

  1. EurekaServer:服务注册

    • 记录服务信息
    • 心跳监控
  2. EurekaClient:客户端

    • Provider:服务提供者
      • 注册自己的信息到EurekaServer
      • 每隔30秒像EurekaServer发送心跳
    • Consumer
      • 根据服务名称从EurekaServer中拉去服务列表
      • 基于服务列表做负载均衡,选中一个微服务后发起远程请求
  3. 搭建eureka

    引入依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
    

    添加@EnableEurekaServer

    package cn.itcast;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekaApplication.class,args);
        }
    }
    

    在application.yml中配置eureka地址

    server:
      port: 10086 # 服务端口
    
    spring:
      application:  # eureka服务名称
        name: eurekaserver
    eureka:
      client:
        service-url:  #eureka 的地址信息
          defaultZone: http://127.0.0.1:10086/eureka
    
  4. 注册user-service

    <!--eureka-client-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    
    server:
      port: 8081
    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/cloud_user?useSSL=false
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver
      application:  # user服务名称
        name: userservice
    
    
    mybatis:
      type-aliases-package: cn.itcast.user.pojo
      configuration:
        map-underscore-to-camel-case: true
    logging:
      level:
        cn.itcast: debug
      pattern:
        dateformat: MM-dd HH:mm:ss:SSS
    
    eureka:
      client:
        service-url:  #eureka 的地址信息
          defaultZone: http://127.0.0.1:10086/eureka
    
  5. order-service完成拉取服务

    String url= "http://userservice/user/" +order.getUserId();
    
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
       return new RestTemplate();
    }
    
  6. Ribbon负载均衡
    轮询

    • 调整负载均衡的规则,定义新的IRule,全体服务
       @Bean
      public IRule randomRule(){
          return new RandomRule();
      }
    
    • 在yml文件中配置,针对某个服务
     userservice:
       ribbon:
         NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
    

    饥饿加载

    #开启饥饿加载
    #指定饥饿加载的服务名称
    ribbon:
      eager-load:
        enabled: true
        clients: userservice
        - 更多的服务加载
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
整理自尚硅谷视频教程springboot高级篇,并增加部分springboot2.x的内容 一、Spring Boot与缓存 一、JSR107 Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry。 • CachingProvider定义了创建、配置、获取、管理和控制多个CacheManager。一个应用可 以在运行 期访问多个CachingProvider。 • CacheManager定义了创建、配置、获取、管理和控制多个唯一命名 的Cache,这些Cache 存在于CacheManager的上下文中。一个CacheManager仅被一个 CachingProvider所拥有。 • Cache是一个类似Map的数据结构并临时存储以Key为索引的值。一个 Cache仅被一个 CacheManager所拥有。 • Entry是一个存储在Cache中的key-value对。 • Expiry 每一 个存储在Cache中的条目有一个定义的有效期。一旦超过这个时间,条目为过期 的状态。一旦过期,条 目将不可访问、更新和删除。缓存有效期可以通过ExpiryPolicy设置。 二、Spring缓存抽象 Spring从3.1开始定义了org.springframework.cache.Cache 和 org.springframework.cache.CacheManager接口来统一不同的缓存技术; 并支持使用JCache(JSR- 107)注解简化我们开发; • Cache接口为缓存的组件规范定义,包含缓存的各种操作集合; • Cache接 口下Spring提供了各种xxxCache的实现;如RedisCache,EhCacheCache , ConcurrentMapCache 等; • 每次调用需要缓存功能的方法时,Spring会检查检查指定参数的指定的目标方法是否 已经被调用 过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法 并缓存结果后返回给用户。下 次调用直接从缓存中获取。 • 使用Spring缓存抽象时我们需要关注以下两点; 1、确定方法需要被缓存 以及他们的缓存策略 2、从缓存中读取之前缓存存储的数据 Cache 缓存接口,定义缓存操作。实现有:RedisCache、EhCacheCache、 ConcurrentMapCache等 CacheManager 缓存管理器,管理各种缓存(Cache)组件 @Cacheable 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 @CacheEvict 清空缓存 @CachePut 保证方法被调用,又希望结果被缓存。 @EnableCaching 开启基于注解的缓存 keyGenerator 缓存数据时key生成策略 serialize 缓存数据时value序列化策略 @CacheConfig 抽取缓存的公共配置 三、几个重要概念&缓存注解 1、常用注解 2、常用参数 名字 位置 描述 示例 methodName root object 当前被调用的方法名 #root.methodName method root object 当前被调用的方法 #root.method.name target root object 当前被调用的目标对象 #root.target targetClass root object 当前被调用的目标对象类 #root.targetClass args root object 当前被调用的方法的参数列表 #root.args[0] 3、常用参数SPEL说明 名字 位置 描述 示例 caches root object 当前方法调用使用的缓存列表(如 @Cacheable(value= {"cache1","cache2"}) ), 则有两 个cache #root.caches[0].name argument name evaluation context 方法参数的名字. 可以直接 #参数 名 ,也可以使用 #p0或#a0 的形 式,0代表参数的索引; #iban 、 #a0 、 #p0 result evaluation context 方法执行后的返回值(仅当方法执 行之后的判断有效,如‘unless’ , ’cache put’的表达式 ’cache evict’的表达式 beforeInvocation=false ) #result 四、代码中使用缓存 1、搭建基本环境 1、导入数据库文件 创建出department和employee表 2、创建javaBean封装数据 3、整合MyBatis操作数据库 1.配置数据源信息 2.使用注解版的MyBatis; 1)、@MapperScan指定需要扫描的mapper接口所在的包
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值