Authored by liangyi.chen@yoho.cn

添加数据源

Showing 35 changed files with 205 additions and 63 deletions
package com.yoho.datasync.consumer.dal.repository.grass;
import com.yoho.datasync.core.base.model.yh_grass.GrassArticle;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface GrassArticleRepository extends JpaRepository<GrassArticle, Integer> {
Optional<GrassArticle> findById(Integer id);
}
... ...
package com.yoho.datasync.consumer.dal.repository;
package com.yoho.datasync.consumer.dal.repository.pcms;
import com.yoho.datasync.core.base.model.yh_pcms.PublicArticleAudit;
import org.springframework.data.jpa.repository.JpaRepository;
... ...
package com.yoho.datasync.consumer.dal.repository;
package com.yoho.datasync.consumer.dal.repository.pcms;
import com.yoho.datasync.core.base.model.yh_pcms.PublicArticleBlock;
import org.springframework.data.jpa.repository.JpaRepository;
... ...
package com.yoho.datasync.consumer.dal.repository;
package com.yoho.datasync.consumer.dal.repository.pcms;
import com.yoho.datasync.core.base.model.yh_pcms.PublicArticleLabel;
import org.springframework.data.jpa.repository.JpaRepository;
... ...
package com.yoho.datasync.consumer.dal.repository;
package com.yoho.datasync.consumer.dal.repository.pcms;
import com.yoho.datasync.core.base.model.yh_pcms.PublicArticleProduct;
import org.springframework.data.jpa.repository.JpaRepository;
... ...
package com.yoho.datasync.consumer.dal.repository;
package com.yoho.datasync.consumer.dal.repository.pcms;
import com.yoho.datasync.core.base.model.yh_pcms.PublicArticle;
import org.springframework.data.jpa.repository.JpaRepository;
... ...
package com.yoho.datasync.consumer.dal.repository;
package com.yoho.datasync.consumer.dal.repository.pcms;
import com.yoho.datasync.core.base.model.yh_pcms.PublicLabelGroup;
... ...
package com.yoho.datasync.consumer.dal.repository;
package com.yoho.datasync.consumer.dal.repository.pcms;
import com.yoho.datasync.core.base.model.yh_pcms.PublicLabel;
import org.springframework.data.jpa.repository.JpaRepository;
... ...
package com.yoho.datasync.consumer.dal.repository;
package com.yoho.datasync.consumer.dal.repository.pcms;
import com.yoho.datasync.core.base.model.yh_pcms.PublicTopic;
import org.springframework.data.jpa.repository.JpaRepository;
... ...
package com.yoho.datasync.consumer.dal.repository;
package com.yoho.datasync.consumer.dal.repository.pcms;
import com.yoho.datasync.core.base.model.yh_pcms.PublicUserAttention;
import org.springframework.data.jpa.repository.JpaRepository;
... ...
package com.yoho.datasync.consumer.dal.repository;
package com.yoho.datasync.consumer.dal.repository.pcms;
import com.yoho.datasync.core.base.model.yh_pcms.PublicUserComment;
... ...
package com.yoho.datasync.consumer.dal.repository;
package com.yoho.datasync.consumer.dal.repository.pcms;
import com.yoho.datasync.core.base.model.yh_pcms.PublicUserFavorite;
import org.springframework.data.jpa.repository.JpaRepository;
... ...
package com.yoho.datasync.consumer.dal.repository;
package com.yoho.datasync.consumer.dal.repository.pcms;
import com.yoho.datasync.core.base.model.yh_pcms.PublicUserHomepage;
import org.springframework.data.jpa.repository.JpaRepository;
... ...
package com.yoho.datasync.consumer.dal.repository;
package com.yoho.datasync.consumer.dal.repository.pcms;
import com.yoho.datasync.core.base.model.yh_pcms.PublicUserPraise;
import org.springframework.data.jpa.repository.JpaRepository;
... ...
package com.yoho.datasync.consumer.dal.repository;
package com.yoho.datasync.consumer.dal.repository.pcms;
import com.yoho.datasync.core.base.model.yh_pcms.PublicVirtualUser;
import org.springframework.data.jpa.repository.JpaRepository;
... ...
... ... @@ -38,8 +38,10 @@ public class GrassUserAttentionListener extends AbstractMqListener<GrassUserAtte
logger.error("deleteData failed , publicUserAttention is null");
throw new Exception("GrassUserAttentionListener deleteData failed , publicUserAttention is null");
}
//grass_user_attention.attention_type取0时public_user_attention.target_id取值:
//select id from public_topic where relate_id = #{grass_user_attention.topic_id} and src_channel = 1
/**
* grass_user_attention.attention_type取0时public_user_attention.target_id取值:
* select id from public_topic where relate_id = #{grass_user_attention.topic_id} and src_channel = 1
*/
Integer targetId = null;
switch (sourceObject.getAttentionType()) {
case ServiceConstant.Grass_User_Attention.ATTENTION_TYPE_TOPIC:
... ... @@ -62,8 +64,10 @@ public class GrassUserAttentionListener extends AbstractMqListener<GrassUserAtte
logger.error("updateData failed , publicUserAttention is null");
return;
}
//关注状态的转换grass_user_attention.status(0 关注,1 取消)
// public_user_attention.status(0-已取消 1-已关注 2-互相关注)
/**
* 关注状态的转换grass_user_attention.status(0 关注,1 取消)
* public_user_attention.status(0-已取消 1-已关注 2-互相关注)
*/
switch (sourceObject.getStatus()) {
case ServiceConstant.Grass_User_Attention.ATTENTION_STATUS:
publicUserAttention.setStatus(ServiceConstant.Public_User_Attention.ATTENTION_STATUS);
... ... @@ -71,8 +75,10 @@ public class GrassUserAttentionListener extends AbstractMqListener<GrassUserAtte
case ServiceConstant.Grass_User_Attention.CANCEL_STATUS:
publicUserAttention.setStatus(ServiceConstant.Public_User_Attention.CANCEL_STATUS);
}
//grass_user_attention.attention_type取0时public_user_attention.target_id取值:
//select id from public_topic where relate_id = #{grass_user_attention.topic_id} and src_channel = 1
/**
* grass_user_attention.attention_type取0时public_user_attention.target_id取值:
* select id from public_topic where relate_id = #{grass_user_attention.topic_id} and src_channel = 1
*/
Integer targetId = null;
switch (sourceObject.getAttentionType()) {
case ServiceConstant.Grass_User_Attention.ATTENTION_TYPE_TOPIC:
... ... @@ -98,8 +104,10 @@ public class GrassUserAttentionListener extends AbstractMqListener<GrassUserAtte
@Override
protected Object checkData(GrassUserAttention sourceObject, String action) {
//grass_user_attention.attention_type取0时public_user_attention.target_id取值:
//select id from public_topic where relate_id = #{grass_user_attention.topic_id} and src_channel = 1
/**
* grass_user_attention.attention_type取0时public_user_attention.target_id取值:
* select id from public_topic where relate_id = #{grass_user_attention.topic_id} and src_channel = 1
*/
if (sourceObject.getAttentionType() != null &&
ServiceConstant.Grass_User_Attention.ATTENTION_TYPE_TOPIC == sourceObject.getAttentionType()) {
return publicTopicService.findTopicIdByRelateIdAndSrcChannel(sourceObject.getTopicId(),
... ...
... ... @@ -2,8 +2,8 @@ package com.yoho.datasync.consumer.service;
import com.yoho.datasync.consumer.common.BeanCopyUtils;
import com.yoho.datasync.consumer.common.ServiceConstant;
import com.yoho.datasync.consumer.dal.repository.PublicArticleBlockRepository;
import com.yoho.datasync.consumer.dal.repository.PublicArticleRepository;
import com.yoho.datasync.consumer.dal.repository.pcms.PublicArticleBlockRepository;
import com.yoho.datasync.consumer.dal.repository.pcms.PublicArticleRepository;
import com.yoho.datasync.core.base.model.yh_pcms.PublicArticleBlock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ...
package com.yoho.datasync.consumer.service;
import com.yoho.datasync.consumer.dal.repository.PublicArticleLabelRepository;
import com.yoho.datasync.consumer.dal.repository.pcms.PublicArticleLabelRepository;
import com.yoho.datasync.core.base.model.yh_pcms.PublicArticleLabel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ...
package com.yoho.datasync.consumer.service;
import com.yoho.datasync.consumer.dal.repository.PublicArticleProductRepository;
import com.yoho.datasync.consumer.dal.repository.pcms.PublicArticleProductRepository;
import com.yoho.datasync.core.base.model.yh_pcms.PublicArticleProduct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ...
... ... @@ -2,8 +2,8 @@ package com.yoho.datasync.consumer.service;
import com.yoho.datasync.consumer.common.BeanCopyUtils;
import com.yoho.datasync.consumer.common.ServiceConstant;
import com.yoho.datasync.consumer.dal.repository.PublicArticleAuditRepository;
import com.yoho.datasync.consumer.dal.repository.PublicArticleRepository;
import com.yoho.datasync.consumer.dal.repository.pcms.PublicArticleAuditRepository;
import com.yoho.datasync.consumer.dal.repository.pcms.PublicArticleRepository;
import com.yoho.datasync.core.base.model.yh_pcms.PublicArticle;
import com.yoho.datasync.core.base.model.yh_pcms.PublicArticleAudit;
import org.slf4j.Logger;
... ...
package com.yoho.datasync.consumer.service;
import com.yoho.datasync.consumer.common.BeanCopyUtils;
import com.yoho.datasync.consumer.dal.repository.PublicLabelGroupRepository;
import com.yoho.datasync.consumer.dal.repository.pcms.PublicLabelGroupRepository;
import com.yoho.datasync.core.base.model.yh_pcms.PublicLabelGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ...
package com.yoho.datasync.consumer.service;
import com.yoho.datasync.consumer.common.BeanCopyUtils;
import com.yoho.datasync.consumer.dal.repository.PublicLabelRepository;
import com.yoho.datasync.consumer.dal.repository.pcms.PublicLabelRepository;
import com.yoho.datasync.core.base.model.yh_pcms.PublicLabel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ...
package com.yoho.datasync.consumer.service;
import com.yoho.datasync.consumer.dal.repository.PublicTopicRepository;
import com.yoho.datasync.consumer.dal.repository.pcms.PublicTopicRepository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
... ...
package com.yoho.datasync.consumer.service;
import com.yoho.datasync.consumer.common.BeanCopyUtils;
import com.yoho.datasync.consumer.dal.repository.PublicUserAttentionRepository;
import com.yoho.datasync.consumer.dal.repository.pcms.PublicUserAttentionRepository;
import com.yoho.datasync.core.base.model.yh_pcms.PublicUserAttention;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ...
package com.yoho.datasync.consumer.service;
import com.yoho.datasync.consumer.common.BeanCopyUtils;
import com.yoho.datasync.consumer.dal.repository.PublicUserCommentRepository;
import com.yoho.datasync.consumer.dal.repository.pcms.PublicUserCommentRepository;
import com.yoho.datasync.core.base.model.yh_pcms.PublicUserComment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ...
package com.yoho.datasync.consumer.service;
import com.yoho.datasync.consumer.common.BeanCopyUtils;
import com.yoho.datasync.consumer.dal.repository.PublicUserFavoriteRepository;
import com.yoho.datasync.consumer.dal.repository.pcms.PublicUserFavoriteRepository;
import com.yoho.datasync.core.base.model.yh_pcms.PublicUserFavorite;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ...
package com.yoho.datasync.consumer.service;
import com.yoho.datasync.consumer.common.BeanCopyUtils;
import com.yoho.datasync.consumer.dal.repository.PublicUserHomepageRepository;
import com.yoho.datasync.consumer.dal.repository.pcms.PublicUserHomepageRepository;
import com.yoho.datasync.core.base.model.yh_pcms.PublicUserHomepage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ...
... ... @@ -2,7 +2,7 @@
package com.yoho.datasync.consumer.service;
import com.yoho.datasync.consumer.common.BeanCopyUtils;
import com.yoho.datasync.consumer.dal.repository.PublicUserPraiseRepository;
import com.yoho.datasync.consumer.dal.repository.pcms.PublicUserPraiseRepository;
import com.yoho.datasync.core.base.model.yh_pcms.PublicUserPraise;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ...
package com.yoho.datasync.consumer.service;
import com.yoho.datasync.consumer.common.BeanCopyUtils;
import com.yoho.datasync.consumer.dal.repository.PublicVirtualUserRepository;
import com.yoho.datasync.consumer.dal.repository.pcms.PublicVirtualUserRepository;
import com.yoho.datasync.core.base.model.yh_pcms.PublicVirtualUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ...
... ... @@ -2,14 +2,8 @@ package com.yoho.datasync.consumer.starter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication(scanBasePackages = "com.yoho.datasync")
@EnableJpaRepositories(basePackages = "com.yoho.datasync.consumer.dal.repository")
@EntityScan(basePackages = "com.yoho.datasync.core.base.model")
@RestController
public class YohoDatasyncConsumerApplication {
public static void main(String[] args) {
... ...
package com.yoho.datasync.consumer.handler.config;
package com.yoho.datasync.consumer.starter.config;
import org.apache.commons.codec.binary.Base64;
... ...
package com.yoho.datasync.consumer.handler.config;
package com.yoho.datasync.consumer.starter.config;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
@Data
@Configuration
@ConfigurationProperties(prefix="spring.datasource")
public class DataSourceConfig {
@Value("${spring.datasource.primary.url}")
private String url;
@Value("${spring.datasource.primary.username}")
private String username;
@Value("${spring.datasource.primary.password}")
private String password;
@Value("${spring.datasource.yhGrass.url}")
private String yhGrassUrl;
@Value("${spring.datasource.yhGrass.username}")
private String yhGrassUsername;
@Value("${spring.datasource.yhGrass.password}")
private String yhGrassPassword;
public static final String text = "1qaz2wsx3edc4rfv";
@Bean
public DataSource getDataSource() throws Exception{
/**
* 连接池配置
*/
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
@Primary
public DataSource primaryDataSource() throws Exception {
return getDataSource(url, username, password);
}
@Bean(name = "yhGrassDataSource")
public DataSource yhGrassDataSource() throws Exception {
return getDataSource(yhGrassUrl, yhGrassUsername, yhGrassPassword);
}
private DataSource getDataSource(String url, String userName, String passWord) throws Exception {
/*
// * 连接池配置
// */
int initialSize = 5; //初始化连接
int maxTotal = 30; //最大活动连接
int maxIdle = 15; //最大空闲连接
int minIdle = 10; //最小空闲连接
int defaultQueryTimeout = 1000; //查询超时时间
/**
/*
* 定期检测连接是否有效
*/
boolean testWhileIdle = true;
... ... @@ -49,15 +75,15 @@ public class DataSourceConfig {
//池中的连接空闲120秒后被回收
int minEvictableIdleTimeMillis = 120000;
DruidDataSource dataSource = new DruidDataSource();
password = StringUtils.trim(password);
passWord = StringUtils.trim(passWord);
//对密码进行解密
password = AES.decrypt(text, password);
passWord = AES.decrypt(text, passWord);
dataSource.setUrl(url);
dataSource.setUsername(username);// 用户名
dataSource.setPassword(password);// 密码
dataSource.setUsername(userName);// 用户名
dataSource.setPassword(passWord);// 密码
dataSource.setInitialSize(initialSize);
dataSource.setMaxActive(maxTotal);
dataSource.setMaxIdle(maxIdle);
// dataSource.setMaxIdle(maxIdle);
dataSource.setMinIdle(minIdle);
dataSource.setQueryTimeout(defaultQueryTimeout);
dataSource.setTestWhileIdle(testWhileIdle);
... ... @@ -71,5 +97,4 @@ public class DataSourceConfig {
}
}
... ...
package com.yoho.datasync.consumer.starter.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactoryPrimary",
basePackages = {"com.yoho.datasync.consumer.dal.repository.pcms"})
public class PrimaryDataSourceConfig {
@Autowired
@Qualifier(value = "primaryDataSource")
private DataSource primaryDataSource;
@Resource
private Properties jpaProperties;
@Primary
@Bean(name = "entityManagerPrimary")
public EntityManager entityManager(@Qualifier("entityManagerFactoryPrimary") LocalContainerEntityManagerFactoryBean entityManagerFactory) {
return entityManagerFactory.getObject().createEntityManager();
}
@Primary
@Bean(name = "entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
LocalContainerEntityManagerFactoryBean entityManagerFactory
= builder
.dataSource(primaryDataSource)
.packages("com.yoho.datasync.core.base.model.yh_pcms")//设置实体类所在位置
.persistenceUnit("primaryPersistenceUnit")//持久化单元创建一个默认即可,多个便要分别命名
.build();
entityManagerFactory.setJpaProperties(jpaProperties);
return entityManagerFactory;
}
}
... ...
package com.yoho.datasync.consumer.starter.config;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactoryYhGrass",
basePackages = "com.yoho.datasync.consumer.dal.repository.grass")
public class YhGrassDataSourceConfig {
@Resource(name="yhGrassDataSource")
private DataSource dataSource;
@Resource
private Properties jpaProperties;
@Bean(name = "entityManagerYhCms")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) throws Exception {
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
}
@Bean(name = "entityManagerFactoryYhGrass")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) throws Exception {
LocalContainerEntityManagerFactoryBean entityManagerFactory
= builder
.dataSource(dataSource)
.packages("com.yoho.datasync.core.base.model.yh_grass")//设置实体类所在位置
.persistenceUnit("grassPersistenceUnit")//持久化单元创建一个默认即可,多个便要分别命名
.build();
entityManagerFactory.setJpaProperties(jpaProperties);
return entityManagerFactory;
}
}
... ...
... ... @@ -8,10 +8,17 @@ rabbitmq:
spring:
datasource:
url: jdbc:mysql://192.168.102.219:3306/yh_pcms?useUnicode=true&amp;characterEncoding=UTF-8
username: yh_test
password: 9nm0icOwt6bMHjMusIfMLw==
driver-class-name: com.mysql.jdbc.Driver
primary:
url: jdbc:mysql://192.168.102.219:3306/yh_pcms?useUnicode=true&amp;characterEncoding=UTF-8
username: yh_test
password: 9nm0icOwt6bMHjMusIfMLw==
driver-class-name: com.mysql.jdbc.Driver
yhGrass:
url: jdbc:mysql://192.168.102.219:3306/yh_grass?useUnicode=true&amp;characterEncoding=UTF-8
username: yh_test
password: 9nm0icOwt6bMHjMusIfMLw==
driver-class-name: com.mysql.jdbc.Driver
logging:
config: classpath:logback-boot.xml
... ...