Skip to content

Commit

Permalink
init push
Browse files Browse the repository at this point in the history
  • Loading branch information
chxfantasy committed Dec 17, 2017
0 parents commit 6631653
Show file tree
Hide file tree
Showing 184 changed files with 8,740 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
*.iml
*/*.iml
.idea/

.classpath
.project
.settings/
bin/
logs
.DS_Store
**.DS_Store
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1. Spring cloud demo
2. With eureka, ribbon feign hystrix
3. All modules depend on spring-cloud-demo-starter, which configs the spring cloud.
4. The heart beat time of eureka is configed to 5s, instead of 15s
5. This code does not contain spring cloud config until now
6. curl -H 'Accept:application/json' -X POST localhost:7004/shutdown
20 changes: 20 additions & 0 deletions spring-cloud-account/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
*.iml
*/*.iml
.idea/

.classpath
.project
.settings/
bin/
logs
.DS_Store
**.DS_Store
44 changes: 44 additions & 0 deletions spring-cloud-account/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<!-- created by harry, https://github.com/chxfantasy/spring-cloud-demo -->

<groupId>com.tech.bixin</groupId>
<artifactId>spring.cloud.account</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring.cloud.app</name>
<description>spring.cloud, by Spring Boot</description>

<parent>
<groupId>com.tech.bixin</groupId>
<artifactId>spring.bixin.parent</artifactId>
<version>1.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
</properties>

<dependencies>
<dependency>
<groupId>com.tech.harry</groupId>
<artifactId>spring.cloud.client</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.tech.harry</groupId>
<artifactId>spring.cloud.starter</artifactId>
<version>1.0.0</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package spring.cloud.account;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;


@SpringBootApplication
@EnableTransactionManagement
@EnableScheduling
@MapperScan("spring.cloud.account.dataaccess.mapper")
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrixDashboard
public class AccountApplication implements CommandLineRunner{

public static void main(String[] args) {
SpringApplication.run(AccountApplication.class, args);
}

@Override
public void run(String... arg0) throws Exception {
//do something after spring-boot started
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package spring.cloud.account.config;

import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import spring.cloud.account.config.interceptors.GlobalAspectInteceptor;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@RefreshScope
public class GlobalBeanConfig {

@Bean(name = "loggerInteceptor")
public GlobalAspectInteceptor getLoggerInteceptor() {
return new GlobalAspectInteceptor();
}

@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo( getApiInfo() )
.host("127.0.0.1")
.select()
.apis(RequestHandlerSelectors.basePackage("spring.cloud.account"))
.paths(PathSelectors.any())
.build();
}

private ApiInfo getApiInfo() {
return new ApiInfoBuilder()
.title("spring-cloud-demo by harryChen, https://github.com/chxfantasy")
.description("https://github.com/chxfantasy")
.termsOfServiceUrl("https://github.com/chxfantasy")
.contact("[email protected]")
.version("1.0")
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package spring.cloud.account.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import spring.cloud.demo.cache.CacheService;
import spring.cloud.demo.model.TraceIdHelper;

import java.util.concurrent.ConcurrentSkipListSet;

/**
* Created by wangxiaohu on 2017/6/13.
*/
@Component
public class GlobalCacheHelper {

private static final Logger LOGGER = LoggerFactory.getLogger(GlobalCacheHelper.class);

@Autowired private CacheService cacheService;
private static ConcurrentSkipListSet<String> needToDeleteKeySet = new ConcurrentSkipListSet();

@Scheduled(fixedDelay = 1000)
private void cacheDelete(){
if (!needToDeleteKeySet.isEmpty()){
String key = needToDeleteKeySet.first();
try {
cacheService.deleteObjectByKey(key);
LOGGER.info("traceId:{}, cacheDelete by GlobalCacheDelete, delete key {}", TraceIdHelper.getTraceId(), key);
needToDeleteKeySet.remove(key);
}catch (Exception e){
e.printStackTrace();
}
}
}

public static void putDeleteKeys( String key ) {
if ( GlobalCacheHelper.needToDeleteKeySet.size() >= GlobalConstants.MAX_LIST_SIZE) {
LOGGER.warn("traceId:{} , needToDeleteKey is over MAX_LIST_SIZE", TraceIdHelper.getTraceId());
}
else {
GlobalCacheHelper.needToDeleteKeySet.add(key);
}

// GlobalCacheDelete.needToDeleteKeySet.add(key);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package spring.cloud.account.config;

/**
* Created by wangxiaohu on 2017/6/19.
*/
public class GlobalConstants {

public static final String ACCOUNT_SERVICE_NAME = "spring.cloud.account";

public static final String BIZ_SERVICE_NAME = "spring.cloud.biz";

public static final int MAX_LIST_SIZE = 100000;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package spring.cloud.account.config.datasourceConfig;

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
public class PrimarySourceConfiguration {

@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
DruidDataSource dataSource = (DruidDataSource) DataSourceBuilder.create().type(DruidDataSource.class).build();
return dataSource;
}

@Bean
public DataSourceTransactionManager txManager( DataSource dataSource ) {
return new DataSourceTransactionManager(dataSource);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package spring.cloud.account.config.datasourceConfig;

import java.util.Properties;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import com.github.pagehelper.PageHelper;

import javax.sql.DataSource;

@Configuration
public class SessionFactoryConfig {

@Value("${mybatis.mapper-locations}")
private String mapperLocations;
@Value("${mybatis.type-aliases-package}")
private String typeAliasesPackage;
@Value("${mybatis.config-location}")
private String configLocation;

@Bean
public SqlSessionFactory sqlSessionFactory(@Autowired DataSource dataSource ) {
try {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setTypeAliasesPackage(this.typeAliasesPackage);
sessionFactory.setMapperLocations( new PathMatchingResourcePatternResolver().getResources(mapperLocations) );
sessionFactory.setConfigLocation(new PathMatchingResourcePatternResolver().getResource(configLocation));

PageHelper pageHelper = new PageHelper();
Properties props = new Properties();
props.setProperty("reasonable", "false");
props.setProperty("supportMethodsArguments", "true");
props.setProperty("returnPageInfo", "check");
props.setProperty("params", "count=countSql");
pageHelper.setProperties(props);

sessionFactory.setPlugins(new Interceptor[] { pageHelper });

return sessionFactory.getObject();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package spring.cloud.account.config.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
@WebFilter(filterName="FilterDemo", urlPatterns={"/**"})
@Order(1) //当有多个filter时,指定filter的顺序
public class FilterDemo implements Filter{
private static final Logger LOGGER = LoggerFactory.getLogger( FilterDemo.class );

@Override
public void destroy() {

}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
LOGGER.debug(" filter demo ");
chain.doFilter(request, response);
}

@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}

}
Loading

0 comments on commit 6631653

Please sign in to comment.