GitHub轻松阅读微服务实战项目流程详解【第四天:账户服务的设计与实现】
生活随笔
收集整理的這篇文章主要介紹了
GitHub轻松阅读微服务实战项目流程详解【第四天:账户服务的设计与实现】
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Four Day
- 1.配置文件精解
- (1)boootstrap.yml
- (2)配置中心的yml文件
- 2.Config
- (1)書架線程池配置
- (2)Mybatis及其Swagger配置
- (3)Redis配置
- (4)公用線程池配置
- (5)數(shù)據(jù)源配置
github地址:https://github.com/Zealon159/light-reading-cloud
1.配置文件精解
(1)boootstrap.yml
spring:# 服務(wù)邏輯名稱application:name: light-reading-cloud-accountcloud:nacos:# 配置中心config:server-addr: xxxxxfile-extension: ymlrefresh: trueshared-dataids: light-reading-cloud-account.yml # Data IDnamespace: 4d109a4d-f34d-4e86-9e39-c2d36db24b00 #命名空間# 注冊中心discovery:server-addr: xxxxxxnamespace: 4d109a4d-f34d-4e86-9e39-c2d36db24b00(2)配置中心的yml文件
2.Config
當(dāng)然在配置文件更方便,以下使用配置類進行配置
(1)書架線程池配置
@Configuration @ConfigurationProperties(prefix = "spring.thread-pool.bookshelf") public class BookshelfThreadPoolConfig {/** 核心線程數(shù) */private int corePoolSize;/** 最大線程數(shù) */private int maximumPoolSize;/** 線程存活時間 */private Long keepAliveTime;/** 隊列容量 */private int queueCapacity;/*** 云書架數(shù)據(jù)消費線程池* @return*/@Bean(value = "userBookshelfQueueThreadPool")public ExecutorService buildUserBookshelfQueueThreadPool(){ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("user-bookshelf-queue-thread-%d").build();// 實例化線程池ExecutorService pool = new ThreadPoolExecutor(this.getCorePoolSize(), this.getMaximumPoolSize(), this.getKeepAliveTime(), TimeUnit.MILLISECONDS,new ArrayBlockingQueue<>(this.getQueueCapacity()),namedThreadFactory,new ThreadPoolExecutor.AbortPolicy());return pool;}public int getCorePoolSize() {return corePoolSize;}public void setCorePoolSize(int corePoolSize) {this.corePoolSize = corePoolSize;}public int getMaximumPoolSize() {return maximumPoolSize;}public void setMaximumPoolSize(int maximumPoolSize) {this.maximumPoolSize = maximumPoolSize;}public Long getKeepAliveTime() {return keepAliveTime;}public void setKeepAliveTime(Long keepAliveTime) {this.keepAliveTime = keepAliveTime;}public int getQueueCapacity() {return queueCapacity;}public void setQueueCapacity(int queueCapacity) {this.queueCapacity = queueCapacity;} }(2)Mybatis及其Swagger配置
@Configuration @MapperScan(basePackages = "cn.zealon.readingcloud.account.dao",sqlSessionTemplateRef="accountCenterSqlSessionTemplate") public class MybatisConfig {private final static String MAPPER_LOCATIONS = "classpath*:mappers/*.xml";/** 工廠配置 */@Beanpublic SqlSessionFactory sqlSessionFactoryBean(@Qualifier("accountCenterDataSource") DataSource dataSource) throws Exception {// 設(shè)置數(shù)據(jù)源SqlSessionFactoryBean factory = new SqlSessionFactoryBean();factory.setDataSource(dataSource);// 添加XML映射ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();factory.setMapperLocations(resolver.getResources(MAPPER_LOCATIONS));//添加插件factory.setPlugins(new Interceptor[]{ this.getPageHelper() });return factory.getObject();}/** 會話模板 */@Bean(name = "accountCenterSqlSessionTemplate")public SqlSessionTemplate setSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}/** 分頁插件 */private PageHelper getPageHelper(){//配置分頁插件,詳情請查閱官方文檔PageHelper pageHelper = new PageHelper();Properties properties = new Properties();//分頁尺寸為0時查詢所有紀錄不再執(zhí)行分頁properties.setProperty("pageSizeZero", "true");//頁碼<=0 查詢第一頁,頁碼>=總頁數(shù)查詢最后一頁properties.setProperty("reasonable", "true");//支持通過 Mapper 接口參數(shù)來傳遞分頁參數(shù)properties.setProperty("supportMethodsArguments", "true");properties.setProperty("params", "count=countSql");//切換數(shù)據(jù)源,自動解析不同數(shù)據(jù)庫的分頁properties.setProperty("autoRuntimeDialect", "true");pageHelper.setProperties(properties);return pageHelper;}/*** swagger 配置類* http://localhost:8080/swagger-ui.html* @author zealon* @since 2019-07-04*/@Configuration@EnableSwagger2public static class AccountSwaggerConfig {/*** swagger生成* @return Docket*/@Beanpublic Docket customDocket() {Docket docket = new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("cn.zealon.readingcloud.account.controller")).paths(PathSelectors.any()).build().apiInfo(apiInfo());return docket;}/*** swagger基礎(chǔ)信息* @return ApiInfo swagger信息*/private ApiInfo apiInfo() {return new ApiInfoBuilder().title("賬戶中心接口").description("賬戶中心").termsOfServiceUrl("").contact(new Contact("", "", "")).license("").licenseUrl("").version("1.0.0").build();}} }(3)Redis配置
@EnableCaching @Configuration public class RedisConfig {@Beanpublic RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> redis = new RedisTemplate<>();redis.setConnectionFactory(redisConnectionFactory);this.setSerializer(redis);return redis;}/** 配置Key的生成方式 */@Beanpublic KeyGenerator keyGenerator() {return new KeyGenerator() {@Overridepublic Object generate(Object o, Method method, Object... objects) {StringBuilder stringBuilder = new StringBuilder();stringBuilder.append(o.getClass().getName()).append(method.getName());for (Object object : objects) {stringBuilder.append(object.toString());}return stringBuilder.toString();}};}/** 緩存管理器 */@Beanpublic CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {//初始化一個RedisCacheWriterRedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);//設(shè)置CacheManager的值序列化方式為json序列化RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer();RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair.fromSerializer(jsonSerializer);RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair);//設(shè)置默認超過期時間是30秒defaultCacheConfig = defaultCacheConfig.entryTtl(Duration.ofSeconds(300));//初始化RedisCacheManagerreturn new RedisCacheManager(redisCacheWriter, defaultCacheConfig);}/** 設(shè)置RedisTemplate的序列化方式 */public void setSerializer(RedisTemplate redisTemplate) {StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);//設(shè)置鍵(key)的序列化方式redisTemplate.setKeySerializer(stringRedisSerializer);//設(shè)置值(value)的序列化方式redisTemplate.setHashKeySerializer(stringRedisSerializer);redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);redisTemplate.afterPropertiesSet();} }(4)公用線程池配置
@Configuration @ConfigurationProperties(prefix = "spring.thread-pool.common") public class ThreadPoolConfig {/** 核心線程數(shù) */private int corePoolSize;/** 最大線程數(shù) */private int maximumPoolSize;/** 線程存活時間 */private Long keepAliveTime;/** 隊列容量 */private int queueCapacity;/*** 云書架數(shù)據(jù)消費線程池* @return*/@Bean(value = "commonQueueThreadPool")public ExecutorService buildCommonQueueThreadPool(){ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("common-queue-thread-%d").build();// 實例化線程池ExecutorService pool = new ThreadPoolExecutor(this.getCorePoolSize(), this.getMaximumPoolSize(), this.getKeepAliveTime(), TimeUnit.MILLISECONDS,new ArrayBlockingQueue<>(this.getQueueCapacity()),namedThreadFactory,new ThreadPoolExecutor.AbortPolicy());return pool;}public int getCorePoolSize() {return corePoolSize;}public void setCorePoolSize(int corePoolSize) {this.corePoolSize = corePoolSize;}public int getMaximumPoolSize() {return maximumPoolSize;}public void setMaximumPoolSize(int maximumPoolSize) {this.maximumPoolSize = maximumPoolSize;}public Long getKeepAliveTime() {return keepAliveTime;}public void setKeepAliveTime(Long keepAliveTime) {this.keepAliveTime = keepAliveTime;}public int getQueueCapacity() {return queueCapacity;}public void setQueueCapacity(int queueCapacity) {this.queueCapacity = queueCapacity;} }(5)數(shù)據(jù)源配置
@Configuration public class AccountCenterDataSourceConfig {/** 數(shù)據(jù)源Bean */@Bean(name = "accountCenterDataSource")@ConfigurationProperties(prefix = "spring.datasource.account-center")public DataSource accountCenterDataSource(){return new DruidDataSource();}/** 事務(wù)管理器 */@Bean(name = "accountCenterTransactionManager")public DataSourceTransactionManager setTransactionManager(@Qualifier("accountCenterDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);} }總結(jié)
以上是生活随笔為你收集整理的GitHub轻松阅读微服务实战项目流程详解【第四天:账户服务的设计与实现】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GitHub轻松阅读微服务实战项目流程详
- 下一篇: 二进制代码查看器Binary Viewe