將zookeeper curator與springboot專案進行整合(重點)
第一步:用java類進行配置
package com.lpy;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
//需要springboot去掃描, springboot是通過java類來進行配置的
@Component
public class ZookeeperCuratorClient {
//zookeeper客戶端
private CuratorFramework client =null;
//日誌(中介軟體的工具類都需要新增日誌)
final static Logger log=LoggerFactory.getLogger(ZookeeperCuratorClient.class);
public static final String ZOOKEEPER_SERVER="192.168.4.245:2181";
public void init() {
if(client!=null) {
return;
}
//第一步:建立重試策略
RetryPolicy retryPolicy=new ExponentialBackoffRetry(1000,5);
//第二步:建立zookeeper客戶端
client=CuratorFrameworkFactory.builder().connectString(ZOOKEEPER_SERVER)
.sessionTimeoutMs(10000)
.retryPolicy(retryPolicy)
.namespace("admin").build();
//第三步:啟動客戶端
client.start();
//測試
try {
String testNodeData = new String(client.getData().forPath("/bgm/..."));
log.info("測試的節點資料為:{}",testNodeData);
} catch (Exception e) {
e.printStackTrace();
}
}
}
第二步:在webconfig進行配置初始化
package com.lpy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* 以前spring配置實在xml裡面,現在springboot配置實在java類裡面
* @author Richard
*
*/
import com.lpy.controller.interceptor.MiniInterceptor;
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
/**
* 顯示圖片的配置,我們的個人資訊的頭像
* @author Richard
* 可以直接通過http://localhost:8082/181127743X140568/face/wx3ce7c79e96132bd8.o6zAJs-h4oiS8tWZhwsiPN2jEeD8.3LCbAy49NFap02319d110a7896da0e48d2c34a1259d6.JPG
*訪問圖片
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/META-INF/resources/") //解決swagger2的訪問,注意後面多了一個斜線
.addResourceLocations("file:D:\\java_all\\workspace-wxxcs\\video-space\\");
}
@Bean(initMethod="init")
public ZookeeperCuratorClient zookeeperCuratorClient() {
return new ZookeeperCuratorClient();
}
@Bean
public MiniInterceptor miniInterceptor() {
return new MiniInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
//將攔截器註冊進去了
registry.addInterceptor(miniInterceptor()).addPathPatterns("/user/**")
.addPathPatterns("/video/upload","/video/uploadCover",
"/video/userLike","/video/userUnLike")
.addPathPatterns("/bgm/**")
.excludePathPatterns("/user/queryPublisher");//這方法就不會被攔截了
super.addInterceptors(registry);
}
}
相關文章
- Apache Curator 操作Zookeeper apiApacheAPI
- SpringBoot專案整合MinIOSpring Boot
- springboot專案整合mybatisSpring BootMyBatis
- SpringBoot專案整合RabbitMQSpring BootMQ
- SpringBoot之Dubbo和Zookeeper整合Spring Boot
- SpringBoot專案整合日誌Spring Boot
- Flowable與springBoot專案整合及出現的問題Spring Boot
- springboot實戰專案-寰宇外賣重難點總結Spring Boot
- SpringBoot整合Actuator進行健康監控Spring Boot
- Springboot2+vue2整合專案Spring BootVue
- SpringBoot + Mybatis + Redis 整合入門專案Spring BootMyBatisRedis
- 聊一聊 Zookeeper 客戶端之 Curator客戶端
- Zookeeper分散式鎖實現Curator十一問分散式
- SpringBoot專案整合阿里Druid連線池Spring Boot阿里UI
- ZooKeeper 分散式鎖 Curator 原始碼 02:可重入鎖重複加鎖和鎖釋放分散式原始碼
- IDEA+gradle將springBoot專案打可執行的Jar包IdeaGradleSpring BootJAR
- ZooKeeper 分散式鎖 Curator 原始碼 01:可重入鎖分散式原始碼
- DUBBO與ZOOKEEPER、SPRINGMVC整合和使用SpringMVC
- SpringBoot整合Canal進行資料庫 快取同步Spring Boot資料庫快取
- springboot專案中整合ip2region包 FileNotFoundExceptionSpring BootException
- springboot+vue專案如何整合企業微信Spring BootVue
- springboot專案整合druid資料庫連線池Spring BootUI資料庫
- zookeeper:spark-project專案的zookeeper配置SparkProject
- SpringBoot專案建立與第一個SSM專案示例Spring BootSSM
- Springboot整合ElasticSearch進行簡單的測試及用Kibana進行檢視Spring BootElasticsearch
- SpringBoot專案的html頁面使用axios進行get post請求Spring BootHTMLiOS
- 將Abp移植進.NET MAUI專案(一):搭建專案UI
- Springboot專案中需整合的依賴集和檔案配置Spring Boot
- 貴州:將涉及高邊坡和深基坑支護的危大工程等列為行業重點專案,並重點監管行業
- Netty整合SpringBoot並使用Protobuf進行資料傳輸NettySpring Boot
- springBoot專案部署整合阿里雲https證書報錯Spring Boot阿里HTTP
- 在前後端分離專案中使用SpringBoot整合Shiro後端Spring Boot
- idea執行springboot開源專案GunsIdeaSpring Boot
- SpringBoot專案在Eclipse/MyEclipse中執行Spring BootEclipse
- 探索大型專案怎麼進行專案管理?專案管理
- Springboot整合JUnit5優雅進行單元測試Spring Boot
- JMeter 如何與 MySQL 進行整合測試JMeterMySql
- Flutter整合舊專案並重構帖子詳情頁Flutter