1. 程式人生 > >將zookeeper curator與springboot專案進行整合(重點)

將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);
	}
	
	
}