Spring Boot crawl Pit Journey-Data Source Configuration Integration Druid (2)

Source: Internet
Author: User
Tags aop html page min redis tomcat redis server log4j firewall

The project structure is as follows:


Start the Portal:

Package com.zm.blog;

Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import Org.springframework.boot.web.servlet.ServletComponentScan;
Import Org.springframework.context.annotation.EnableMBeanExport;
Import Org.springframework.context.annotation.Import;
Import Org.springframework.jmx.support.RegistrationPolicy;
Import org.springframework.transaction.annotation.EnableTransactionManagement;

Import Com.github.tobato.fastdfs.FdfsClientConfig;
Troubleshoot the JMX recurring bean issue
@EnableMBeanExport (registration = registrationpolicy.ignore_existing)
//Integrated FASTDSF
@Import (Fdfsclientconfig.class)//
start portal
@SpringBootApplication
//configure servlet
@ Servletcomponentscan
//transaction
@EnableTransactionManagement public
class Blogstart {public
	static void Main (string[] args) {
		springapplication.run (blogstart.class, args);
	}
}

Application.properties

############# #servlet容器 ##################################################### server.port=80 server.context-path=
/blog ############# #应用编码 ##################################################### spring.http.encoding.force=true Spring.http.encoding.charset=utf-8 spring.http.encoding.enabled=true Server.tomcat.uri-encoding=utf-8 ###########
######################################################## spring.mvc.view.prefix=/web-inf/jsp/ spring.mvc.view.suffix=.jsp #spring. thymeleaf.prefix=/web-inf/jsp/#spring. thymeleaf.suffix=.html # SPRING.THYMELEAF.MODE=LEGACYHTML5 ###################################### #驱动配置信息 spring.datasource.type= Com.alibaba.druid.pool.DruidDataSource spring.datasource.url=jdbc:mysql://101.200.49.60:3306/gytlv?useunicode= True&characterencoding=utf8 spring.datasource.username=root spring.datasource.password= ##################### ####################### #日志 # logging.level.com.zm.blog.mapper=debug ################################ #mq ####### ######### Spring.activemq.broker-url=tcp://www.gytlv.com:61616 # # # #druid不支持的补充配置 # # # Spring.datasource.driverclassname=com.mysql.jdbc.driver # Initialization size, MIN, max spring.datasource.initialsize=1 spring.datasource.minidle=1 spring.datasource.maxactive=20 # Configuration get connection Wait super  
Time spring.datasource.maxwait=60000 # Configures the minimum lifetime of a connection in a pool, in milliseconds Spring.datasource.validationquery=select 1 from DUAL  
Spring.datasource.testonborrow=false Spring.datasource.testonreturn=false Spring.datasource.testwhileidle=true  
spring.datasource.minevictableidletimemillis=25200000 # How long does the configuration interval take to detect idle connections that need to be closed, in milliseconds  
spring.datasource.timebetweenevictionrunsmillis=60000 Spring.datasource.removeabandoned=true  
spring.datasource.removeabandonedtimeout=1800 # Open Pscache, and specify the size of Pscache on each connection  
Spring.datasource.poolpreparedstatements=true spring.datasource.maxpoolpreparedstatementperconnectionsize=20 Spring.datasource.logabandoned=true # Configuration Monitoring statistics interception filters, removed after the monitoring interface SQL can not be counted, ' wall ' for the firewall spring.datasource.filters=stat, Wall,log4j #配置springAssociation #设置使用Cglib进行代理, because some of the agents need not interface not applicable to the JDK dynamic agent, will error spring.aop.proxy-target-class=true #配置Druid监控Spring包方法的调用 Spring.datasource.druid.aop-patterns=packages # Redis (redisproperties) # Redis Database index (default = 0) spring.redis.database=0 # R
Edis server address spring.redis.host=101.200.49.60 # Redis Server connection Port spring.redis.port=6379 # Redis Server connection password (default is empty)
spring.redis.password= # Connection Pool Maximum number of connections (using negative values for No limit) Spring.redis.pool.max-active=8 # Connection pool maximum blocking wait time (using negative values to indicate no limit)  
SPRING.REDIS.POOL.MAX-WAIT=-1 # Maximum idle connection in the connection pool spring.redis.pool.max-idle=8 # minimum idle connection in the connection pool spring.redis.pool.min-idle=0
# Connection time-out (milliseconds) #spring. redis.timeout= #fastdsf ################################################################### Image_server_url=http://101.200.49.60:8888/tracker_server= ####################################################
############### fdfs.sotimeout=1500 fdfs.connecttimeout=600 fdfs.thumbimage.width=150 fdfs.thumbImage.height=150 fdfs.trackerlist[0]=101.200.49.60:22122 ################################################################## # # ############# #servlet容器 ##################################################### server.port=80
Server.context-path=/blog ############# #应用编码 #####################################################
Spring.http.encoding.force=true Spring.http.encoding.charset=utf-8 Spring.http.encoding.enabled=true
Server.tomcat.uri-encoding=utf-8 ################################################################### Spring.mvc.view.prefix=/web-inf/jsp/spring.mvc.view.suffix=.jsp #spring. thymeleaf.prefix=/web-inf/jsp/#  
Spring.thymeleaf.suffix=.html #spring. THYMELEAF.MODE=LEGACYHTML5 ###################################### #驱动配置信息 Spring.datasource.type=com.alibaba.druid.pool.druiddatasource spring.datasource.url=jdbc:mysql://
101.200.49.60:3306/gytlv?useunicode=true&characterencoding=utf8 Spring.datasource.username=root spring.datasource.password= ############################################ #日志 # # # # # Logging.level.com.zm.blog.mapper =debug ################################ #mq ################ spring.activemq. broker-url=tcp://www.gytlv.com:61616 # # #druid不支持的补充配置 # # Spring.datasource.driverclassname= Com.mysql.jdbc.Driver # Initialize size, min, max spring.datasource.initialsize=1 spring.datasource.minidle=1 Spring.datasource. MAXACTIVE=20 # Configuration Gets the time that the connection waits timeout spring.datasource.maxwait=60000 # Configures the minimum lifetime of a connection in the pool, in milliseconds spring.datasource.validation Query=select 1 from DUAL spring.datasource.testonborrow=false spring.datasource.testonreturn=false Spring.datasource . testwhileidle=true spring.datasource.minevictableidletimemillis=25200000 # How long does the configuration interval take to detect idle connections that need to be closed, in milliseconds spring  
. datasource.timebetweenevictionrunsmillis=60000 Spring.datasource.removeabandoned=true  
spring.datasource.removeabandonedtimeout=1800 # Open Pscache, and specify the size of Pscache on each connection  
Spring.datasource.poolpreparedstatements=true spring.datasource.maxpoolpreparedstatementperconnectionsize=20 Spring.datasource.logabandoned=true # Configuration Monitoring statistics interception filters, removed after the monitoring interface SQL can not be counted, ' wall ' for the firewall spring.datasource.filters=stat, Wall,log4j #配置Spring is associated #设置使用Cglib进行代理 because some of the agents that are not interfaces that do not apply to the JDK Dynamic Agent will error spring.aop.proxy-target-class=true #配置Druid监控Spring包方法的调用 Spring.datasource.druid.aop-patterns=packages # Redis (redisproperties) # Redis Database index (default = 0) spring.redis.database=0 # R
Edis server address spring.redis.host=101.200.49.60 # Redis Server connection Port spring.redis.port=6379 # Redis Server connection password (default is empty)
spring.redis.password= # Connection Pool Maximum number of connections (using negative values for No limit) Spring.redis.pool.max-active=8 # Connection pool maximum blocking wait time (using negative values to indicate no limit)  
SPRING.REDIS.POOL.MAX-WAIT=-1 # Maximum idle connection in the connection pool spring.redis.pool.max-idle=8 # minimum idle connection in the connection pool spring.redis.pool.min-idle=0
# Connection time-out (milliseconds) #spring. redis.timeout= #fastdsf ################################################################### Image_server_url=http://101.200.49.60:8888/tracker_server= ####################################################
############### fdfs.sotimeout=1500 fdfs.connecttimeout=600 fdfs.thumbimage.width=150 fdfs.thumbImage.height=150 fdfs.trackerlist[0]=101.200.49.60:22122 ####################################################################


 
1. Using Druid for database access

1.1 Initializing data source properties based on resource.properties configuration

Package com.zm.blog.config.druid;

Import java.sql.SQLException;

Import Javax.sql.DataSource;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import Org.springframework.aop.Advisor;
Import Org.springframework.aop.support.DefaultPointcutAdvisor;
Import Org.springframework.aop.support.JdkRegexpMethodPointcut;
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 Com.alibaba.druid.pool.DruidDataSource;

Import Com.alibaba.druid.support.spring.stat.DruidStatInterceptor; @Configuration public class Druidconfig {private static final Logger Logger = Loggerfactory.getlogger (Druidconfig.class)

	;
	@Value ("${spring.datasource.driverclassname}") Private String driverclassname;
	@Value ("${spring.datasource.url}") Private String URL;
	@Value ("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password;
	@Value ("${spring.datasource.initialsize}") private int initialsize;
	@Value ("${spring.datasource.minidle}") private int minidle;
	@Value ("${spring.datasource.maxactive}") private int maxactive;
	@Value ("${spring.datasource.maxwait}") private int maxwait;
	@Value ("${spring.datasource.validationquery}") Private String validationquery;
	@Value ("${spring.datasource.testonborrow}") Private Boolean testonborrow;
	@Value ("${spring.datasource.testonreturn}") Private Boolean Testonreturn;
	@Value ("${spring.datasource.testwhileidle}") Private Boolean testwhileidle;
	@Value ("${spring.datasource.timebetweenevictionrunsmillis}") private int timebetweenevictionrunsmillis;
	@Value ("${spring.datasource.minevictableidletimemillis}") private int minevictableidletimemillis;
	@Value ("${spring.datasource.removeabandoned}") Private Boolean removeabandoned; @Value ("${spring.datasource.removeabandonedtimeout}") Private int RemoveabandonEdtimeout;
	@Value ("${spring.datasource.logabandoned}") Private Boolean logabandoned;
	@Value ("${spring.datasource.filters}") Private String filters;
	@Value ("${spring.datasource.poolpreparedstatements}") Private Boolean poolpreparedstatements; @Value ("${spring.datasource.maxpoolpreparedstatementperconnectionsize}") Private int

	Maxpoolpreparedstatementperconnectionsize;  Add an AOP advice @Bean public druidstatinterceptor druidstatinterceptor () {Druidstatinterceptor Druidstatinterceptor =
		New Druidstatinterceptor ();
	return druidstatinterceptor; }//Add AOP pointcut @Bean public jdkregexpmethodpointcut jdkregexpmethodpointcut () {jdkregexpmethodpointcut Jdkrege
		Xpmethodpointcut = new Jdkregexpmethodpointcut ();
		Jdkregexpmethodpointcut.setpatterns (new string[] {"com.zm.blog.uib.service.*", "com.zm.blog.uib.controller.*"});
	return jdkregexpmethodpointcut; }//Set the default AOP configuration corresponding to the original <aop:advisor> @Bean public advisor Druidadvisor () {defaultpointcutadvisor defaultPointcutadvisor = new Defaultpointcutadvisor ();
		Defaultpointcutadvisor.setpointcut (Jdkregexpmethodpointcut ());
		Defaultpointcutadvisor.setadvice (Druidstatinterceptor ());
	return defaultpointcutadvisor;

		} @Bean @Primary public DataSource DataSource () {Druiddatasource DataSource = new Druiddatasource ();
		Datasource.setdriverclassname (Driverclassname);
		Datasource.seturl (URL);
		Datasource.setusername (username);
		Datasource.setpassword (password);
		Other configuration datasource.setinitialsize (initialsize);
		Datasource.setminidle (Minidle);
		Datasource.setmaxactive (maxactive);
		Datasource.setmaxwait (maxwait);
		Datasource.settimebetweenevictionrunsmillis (Timebetweenevictionrunsmillis);
		Datasource.setminevictableidletimemillis (Minevictableidletimemillis);
		Datasource.setvalidationquery (Validationquery);
		Datasource.settestwhileidle (Testwhileidle);
		Datasource.settestonborrow (Testonborrow);
		Datasource.settestonreturn (Testonreturn); Datasource.setpoolpreparedstatements (Poolpreparedstatements);

		Datasource.setmaxpoolpreparedstatementperconnectionsize (maxpoolpreparedstatementperconnectionsize);
		try {datasource.setfilters (filters);
		} catch (SQLException e) {logger.error ("Druid Configuration Initialization Filter", e);
	} return datasource;
 }
}

1.2 Configuring filters for use with Druid

Package com.zm.blog.config.druid;

Import Javax.servlet.annotation.WebFilter;
Import Javax.servlet.annotation.WebInitParam;

Import Com.alibaba.druid.support.http.WebStatFilter;

@WebFilter (filtername= "Druidwebstatfilter", urlpatterns= "/*",
initparams={
    @WebInitParam (name= " Exclusions ", value=" *.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/* ")//Ignore Resource
}) public
class Druidstatfilter extends webstatfilter{
	
}

1.3 servlet Configuration

Package com.zm.blog.config.druid;

Import Javax.servlet.annotation.WebInitParam;
Import Javax.servlet.annotation.WebServlet;

Import Com.alibaba.druid.support.http.StatViewServlet;

@WebServlet (urlpatterns = "/druid/*", 
initparams={
        //@WebInitParam (name= "Allow", value= "127.0.0.1"),// IP whitelist (no configuration or null, then all access is allowed)
        //@WebInitParam (name= "Deny", value= "192.168.16.111"),//IP blacklist (Deny takes precedence over allow when common)
        @WebInitParam (name= "Loginusername", value= "admin"),//user name
        @WebInitParam (name= "Loginpassword", value= " ISAdmin "),//Password
        @WebInitParam (name=" resetenable ", value=" false ")//disable" Reset all "function on HTML page
)
public Class Druidstatviewservlet extends Statviewservlet {

	private static final long serialversionuid = 1L;

}
Note: The following configuration is based on the resource.properties file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.