2、Springboot入門-整合篇

目錄

 

1、springboot緩存

2、springboot消息隊列

3、springboot與檢索

4、springboot與分佈式

5、springboot與服務監控


1、springboot緩存

註解式緩存:將方法的運行結果進行緩存;以後再要相同的數據,直接從緩存中獲取,不用調用方法; * CacheManager管理多個Cache組件的,對緩存的真正CRUD操作在Cache組件中,每一個緩存組件有自己唯一一個名字;

 原理:

1、自動配置類;CacheAutoConfiguration *

2、緩存的配置類 

org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration org.springframework.boot.autoconfigure.cache.InfinispanCacheConfiguration org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration

......

org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration【默認】

3、哪個配置類默認生效:SimpleCacheConfiguration;

4、給容器中註冊了一個CacheManager:ConcurrentMapCacheManager

5、可以獲取和創建ConcurrentMapCache類型的緩存組件;他的作用將數據保存在ConcurrentMap中;

核心:

1)、使用CacheManager【ConcurrentMapCacheManager】按照名字得到Cache【ConcurrentMapCache】組件

2)、key使用keyGenerator生成的,默認是SimpleKeyGenerator

常用緩存註解:

二、快速體驗緩存
     步驟:
        1、開啓基於註解的緩存 @EnableCaching
        2、標註緩存註解即可
           @Cacheable
           @CacheEvict
           @CachePut
 默認使用的是ConcurrentMapCacheManager==ConcurrentMapCache;將數據保存在   ConcurrentMap<Object, Object>中
 開發中使用緩存中間件;redis、memcached、ehcache;
 三、整合redis作爲緩存
 Redis 是一個開源(BSD許可)的,內存中的數據結構存儲系統,它可以用作數據庫、緩存和消息中間件。
  1、安裝redis
  2、引入redis的starter
  3、application.yml配置redis鏈接地址
  4、測試緩存
     原理:CacheManager===Cache 緩存組件來實際給緩存中存取數據
     1)、引入redis的starter,容器中保存的是 RedisCacheManager;
     2)、RedisCacheManager 幫我們創建 RedisCache 來作爲緩存組件;RedisCache通過操作redis緩存數據的
     3)、默認保存數據 k-v 都是Object;利用序列化保存;如何保存爲json
            1、引入了redis的starter,cacheManager變爲 RedisCacheManager;
            2、默認創建的 RedisCacheManager 操作redis的時候使用的是 RedisTemplate<Object, Object>
            3、RedisTemplate<Object, Object> 是 默認使用jdk的序列化機制
      4)、自定義CacheManager;

2、springboot消息隊列

3、springboot與檢索

3、springboot與安全

使用步驟:

 1、引入SpringSecurity;
 2、編寫SpringSecurity的配置類;@EnableWebSecurity   extends WebSecurityConfigurerAdapter
 3、控制請求的訪問權限:
         configure(HttpSecurity http) {
              http.authorizeRequests().antMatchers("/").permitAll()
                  .antMatchers("/level1/**").hasRole("XXX")
         }
 4、定義認證規則:
         configure(AuthenticationManagerBuilder auth){
              auth.inMemoryAuthentication()
                  .withUser("zhangsan").password("123456").roles("VIP1","VIP2")
         }
 5、開啓自動配置的登陸功能:
         configure(HttpSecurity http){
              http.formLogin();
         }
 6、註銷:http.logout();
 7、記住我:Remeberme();

4、springboot與分佈式

將服務提供者註冊到註冊中心

生產者:
1、引入dubbo和zkclient相關依賴
2、配置dubbo的掃描包和註冊中心地址
3、使用dubbo的@Service發佈服務

消費者:

1、引入依賴,
2、配置dubbo的註冊中心地址
3、引用服務dubbo的@Reference註解注入接口,必須有和生產者相同的接口

消費者入口類要配置發現服務:

@EnableDiscoveryClient //開啓發現服務功能、@LoadBalanced //使用負載均衡機制

使用@Autowired
    RestTemplate restTemplate;進行遠程通信,因爲springcloud是基於http實現,

遠程調用:String s = restTemplate.getForObject("http://PROVIDER-TICKET/ticket", String.class);

5、springboot與服務監控

監控的各項端點有:端點也可進行定製,通過endpoints+端點名+屬性名來設置

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章