各位读者,大家好!
新建一个springCloud项目。需要我们配置最基本的配置文件。
1. 配置yml文件如下:
1 spring:
2 application:
3 name: custom-output
4 cloud:
5 config:
6 discovery:
7 # 是否启用配置中心
8 enabled: true
9 serviceId: config-server
10 # 获取配置的策略
11 profile: ${spring.profiles.active}
12 password: root1234
13 username: user
14 # 读取配置中心的分支名
15 label: master
16 # 设置配置文件的名称
17 name: custom-output
18 fail-fast: false
19 retry:
20 initial-interval: 3600000
21 max-attempts: 1
22 max-interval: 3600000
23 multiplier: 0.1
24 # 部署所属环境
25 profiles:
26 active: dev
27
28 eureka:
29 instance:
30 prefer-ip-address: true
31 instance-id: ${spring.cloud.client.ipAddress}:${server.port}
32 client:
33 service-url:
34 # 注册中心地址
35 defaultZone: http://registerserver-pool.${DOMAIN}:8889/eureka
36 register-with-eureka: true
37 fetch-registry: true
38
39 server:
40 # 接口访问前缀
41 context-path: /third
42 # 服务端口
43 port: 8009
44 connection-timeout: 120000
45
46 mybatis:
47 # 指定xml文件路径(若mapper类和mapper.xml不在同一个路径下时需要配置)
48 mapper-locations: classpath*:mybatis/*.xml
49 configuration:
50 cache-enabled: false
51
52 management:
53 health:
54 rabbit:
55 enabled: false
56
57 feign:
58 hystrix:
59 enabled: true
60 hystrix:
61 threadpool:
62 default:
63 coreSize: 500
64 command:
65 default:
66 fallback:
67 isolation:
68 semaphore:
69 maxConcurrentRequests: 1000
70 execution:
71 timeout:
72 enabled: true
73 isolation:
74 thread:
75 timeoutInMilliseconds: 60000
76 ribbon:
77 ReadTimeout: 60000
78 ConnectTimeout: 10000
2. 当然也可以配置.properties文件,会先读取yml文件,再读取.properties文件,如果配置冲突。最终会以.properties中配置为准,也可以把yml中的配置到.properties文件中,如下application-dev.properties文件:
1 #熔断配置
2 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=120000
3 hystrix.threadpool.default.coreSize=100
4 #Hystrix支持,如果为true,hystrix库必须在classpath中
5 feign.hystrix.enabled=true
6 #ribbon设置
7 ribbon.ConnectTimeout=120000
8 ribbon.ReadTimeout=120000
9
10 # 数据源连接设置
11 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
12 # 指定driver的类名,默认从jdbc url中自动探测
13 spring.datasource.driverClassName=com.mysql.jdbc.Driver
14 spring.datasource.url=jdbc:mysql://host:3306/dbname?useUnicode=true&allowMultiQuerie=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
15 spring.datasource.username=uname
16 spring.datasource.password=password
17 # 初始化连接
18 spring.datasource.initialSize=5
19 # 最小空闲连接
20 spring.datasource.minIdle=5
21 # 最大连接数量
22 spring.datasource.maxActive=20
23 # 超时等待时间 60秒
24 spring.datasource.maxWait=60000
25 # 指定空闲连接检查、废弃连接清理、空闲连接池大小调整之间的操作时间间隔
26 spring.datasource.timeBetweenEvictionRunsMillis=60000
27 # 指定一个空闲连接最少空闲多久后可被清除.
28 spring.datasource.minEvictableIdleTimeMillis=300000
29 # 指定获取连接时连接校验的sql查询语句.
30 spring.datasource.validationQuery=SELECT 1 FROM DUAL
31 # 当连接空闲时,是否执行连接测试.
32 spring.datasource.testWhileIdle=true
33 # 当从连接池借用连接时,是否测试该连接.
34 spring.datasource.testOnBorrow=false
35 # 在连接归还到连接池时是否测试该连接.
36 spring.datasource.testOnReturn=false
37 # 指定是否池化statements.
38 spring.datasource.poolPreparedStatements=true
39 spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
40 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
41 spring.datasource.filters=stat,wall,log4j
42 # 是否在自动回收超时连接的时候打印连接的超时错误
43 spring.datasource.logAbandoned=true
44 # 是否自动回收超时连接
45 spring.datasource.removeAbandoned=true
46 # 超时时间(以秒数为单位)
47 spring.datasource.removeAbandonedTimeout=180
48 # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
49 spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
50
51 # REDIS配置
52 spring.redis.database=0
53 spring.redis.host=redis.test.baiwang-inner.com
54 spring.redis.port=6379
55 spring.redis.password=rm$wdFcZd0gdJ*WD
56 spring.redis.pool.max-active=200
57 spring.redis.pool.max-wait=10000
58 spring.redis.pool.max-idle=50
59 spring.redis.pool.min-idle=5
60 spring.redis.timeout=10000
61
62 # 日志级别(用于log4j日志打印配置)
63 logging.level.com.baiwang.cloud.output=DEBUG
64
65 # 第三方系统地址
66 custom.third.requestUrl=http://
注意:在yml中遇到-的配置,则首字母需要大写,比如配置eureka.client.service-url.defaultZone则需要写为eureka.client.serviceUrl.defaultZone=http:localhost:8889/eureka
3. 启动主类
1 package com.cheng2839;
2
3 import com.github.pagehelper.PageHelper;
4 import org.mybatis.spring.annotation.MapperScan;
5 import org.springframework.boot.SpringApplication;
6 import org.springframework.boot.autoconfigure.SpringBootApplication;
7 import org.springframework.boot.web.servlet.MultipartConfigFactory;
8 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
9 import org.springframework.cloud.netflix.feign.EnableFeignClients;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.context.annotation.ComponentScan;
12 import org.springframework.context.annotation.EnableAspectJAutoProxy;
13 import org.springframework.jdbc.datasource.DataSourceTransactionManager;
14 import org.springframework.scheduling.annotation.EnableScheduling;
15 import org.springframework.transaction.PlatformTransactionManager;
16
17 import javax.servlet.MultipartConfigElement;
18 import javax.sql.DataSource;
19 import java.util.Properties;
20
21 @SpringBootApplication
22 @EnableDiscoveryClient
23 @EnableFeignClients
24 @EnableScheduling
25 @EnableAspectJAutoProxy(proxyTargetClass = true)
26 @MapperScan(basePackages = {"com.cheng2839.*.mapper.**"})
27 @ComponentScan(basePackages = {"com.cheng2839"})
28 public class Application {
29
30 public static void main(String[] args) {
31 SpringApplication.run(Application.class, args);
32 }
33
34 // 创建事务管理器
35 @Bean(name = "transactionManager")
36 public PlatformTransactionManager txManager(DataSource dataSource) {
37 return new DataSourceTransactionManager(dataSource);
38 }
39
40 /**
41 * 配置mybatis的分页插件pageHelper
42 *
43 * @return
44 */
45 @Bean
46 public PageHelper pageHelper() {
47 PageHelper pageHelper = new PageHelper();
48 Properties properties = new Properties();
49 properties.setProperty("offsetAsPageNum", "true");
50 properties.setProperty("rowBoundsWithCount", "true");
51 properties.setProperty("reasonable", "true");
52 // 配置mysql数据库的方言
53 properties.setProperty("dialect", "mysql");
54 pageHelper.setProperties(properties);
55 return pageHelper;
56 }
57
58 @Bean
59 public MultipartConfigElement multipartConfigElement() {
60 MultipartConfigFactory factory = new MultipartConfigFactory();
61 //单个文件最大
62 factory.setMaxFileSize(ThirdConstants.FILE_MAX_FILE_SIZE);
63 /// 设置总上传数据总大小
64 factory.setMaxRequestSize(ThirdConstants.FILE_MAX_REQUEST_SIZE);
65 return factory.createMultipartConfig();
66 }
67 }
4. pom文件配置
<?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>
<groupId>com.cheng2839.custom</groupId>
<artifactId>custom-output</artifactId>
<version>1.0.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>custom-output</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-data-redis.version>1.7.3.RELEASE</spring-data-redis.version>
<jedis.version>2.7.2</jedis.version>
<mybatis-plus.version>2.1-gamma</mybatis-plus.version>
<mybatis.pagehelper.version>4.2.1</mybatis.pagehelper.version>
<skipTests>true</skipTests>
</properties>
<dependencies>
<dependency>
<groupId>com.cheng2839.custom</groupId>
<artifactId>cheng2839-cloud-dependencies</artifactId>
<version>1.0</version>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>fastjson</artifactId>
<groupId>com.alibaba</groupId>
</exclusion>
<exclusion>
<artifactId>commons-lang3</artifactId>
<groupId>org.apache.commons</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.17</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring-data-redis.version}</version>
</dependency>
<!-- -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
<exclusions>
<exclusion>
<artifactId>commons-io</artifactId>
<groupId>commons-io</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<exclusions>
<exclusion>
<artifactId>netty-buffer</artifactId>
<groupId>io.netty</groupId>
</exclusion>
<exclusion>
<artifactId>netty-codec</artifactId>
<groupId>io.netty</groupId>
</exclusion>
<exclusion>
<artifactId>netty-common</artifactId>
<groupId>io.netty</groupId>
</exclusion>
<exclusion>
<artifactId>netty-handler</artifactId>
<groupId>io.netty</groupId>
</exclusion>
<exclusion>
<artifactId>netty-transport</artifactId>
<groupId>io.netty</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- lombok注解,需要安装lombok插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<!-- mybatis分页 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.2.2</version>
</dependency>
<!--Spring 官方提供的热部署插件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--阿里OSS 客户端-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>biz.paluch.redis</groupId>
<artifactId>lettuce</artifactId>
<version>3.5.0.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>1.3.0</version>
<scope>test</scope>
</dependency>
<!-- mybatis分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${mybatis.pagehelper.version}</version>
<exclusions>
<exclusion>
<artifactId>jsqlparser</artifactId>
<groupId>com.github.jsqlparser</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.0.12</version>
</dependency>
<!-- 引入log4j2依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.2</version>
</dependency>
<!-- 生成二维码 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<distributionManagement>
<repository>
<id>releases</id>
<name>bazaar releases</name>
<url>http://mvn.dev.cheng2839.com:8081/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>bazaar snapshots</name>
<url>http://mvn.dev.cheng2839.com:8081/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
<repositories>
<repository>
<id>nexus</id>
<name>Team Nexus Repository</name>
<url>http://mvn.dev.cheng2839.com:8081/content/groups/public</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>Team Nexus Repository</name>
<url>http://mvn.dev.cheng2839.com:8081/content/groups/public</url>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>${project.artifactId}</finalName>
<outputDirectory>target</outputDirectory>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<phase>deploy</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
注意:其中pom文件中很多是不需要的依赖,直接去掉即可