一、项目搭建

    首先创建的Spring Boot工程(war包),然后导入相关依赖:

<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    <groupId>com.shen.demo</groupId>
    <artifactId>project</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <!-- 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
​
        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- 分页助手 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>3.7.5</version>
        </dependency>
        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>0.9.1</version>
        </dependency>
​
        <!-- 通用Mapper -->
        <dependency>
            <groupId>com.github.abel533</groupId>
            <artifactId>mapper</artifactId>
            <version>2.3.4</version>
        </dependency>
        <!-- MySql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- 连接池 -->
        <dependency>
            <groupId>com.jolbox</groupId>
            <artifactId>bonecp-spring</artifactId>
            <version>0.8.0.RELEASE</version>
        </dependency>
​
        <!-- httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
​
        <!-- JSP相关 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
​
        <!-- Apache工具组件 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
            <version>1.4.0.RELEASE</version>
        </dependency>
​
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <!-- 排除传递spring依赖 -->
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
​
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.3.3</version>
        </dependency>
​
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
​
    <build>
        <plugins>
            <!-- 资源文件拷贝插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- java编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
​
</project>

二、开始配置
1、编写主配置

    编写Spring配置类ProjectApplication,内容如下:

@Configuration
@PropertySource(value = { "classpath:jdbc.properties", "classpath:httpclient.properties" })
@ComponentScan(basePackages = "com.shen")
@ImportResource(value = "classpath:dubbo/dubbo-consumer.xml")
@SpringBootApplication
public class ProjectApplication extends SpringBootServletInitializer{
    @Value("${jdbc.url}")
    private String jdbcUrl;
    @Value("${jdbc.driverClassName}")
    private String jdbcDriverClassName;
    @Value("${jdbc.username}")
    private String jdbcUsername;
    @Value("${jdbc.password}")
    private String jdbcPassword;
​
    @Bean(destroyMethod = "close")
    public DataSource dataSource() {
        BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
        // 数据库驱动
        boneCPDataSource.setDriverClass(jdbcDriverClassName);
        // 相应驱动的jdbcUrl
        boneCPDataSource.setJdbcUrl(jdbcUrl);
        // 数据库的用户名
        boneCPDataSource.setUsername(jdbcUsername);
        // 数据库的密码
        boneCPDataSource.setPassword(jdbcUsername);
        // 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0
        boneCPDataSource.setIdleConnectionTestPeriodInMinutes(60);
        // 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0
        boneCPDataSource.setIdleMaxAgeInMinutes(30);
        // 每个分区最大的连接数
        boneCPDataSource.setMaxConnectionsPerPartition(100);
        // 每个分区最小的连接数
        boneCPDataSource.setMinConnectionsPerPartition(5);
        return boneCPDataSource;
    }
​
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(ProjectApplication.class);
    }
}

上面的代码主要包含如下配置内容:

读取外部的配置文件
设置扫描包
定义数据源

附加,上面引入的资源文件如下:

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/project?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
jdbc.username=root
jdbc.password=root

httpclient.properties

http.defaultMaxPerRoute=100
http.maxTotal=300
http.connectTimeout=1000
http.connectionRequestTimeout=500
http.socketTimeout=10000
http.staleConnectionCheckEnabled=true

2、整合Mybatis

Mybatis和Spring Boot的整合有两种方式:

    第一种:使用mybatis官方提供的Spring Boot整合包实现,地址:https://github.com/mybatis/spring-boot-starter 

    第二种:使用mybatis-spring整合的方式,也就是我们传统的方式。这里我们推荐使用第二种,因为这样我们可以很方便的控制Mybatis的各种配置。首先,创建一个Mybatis的配置类(MyBatisConfig):

@Configuration
public class MyBatisConfig {
    @Autowired
    private DataSource dataSource;
    @Bean
    @ConditionalOnMissingBean //当容器里没有指定的Bean的情况下创建该对象
    public SqlSessionFactoryBean sqlSessionFactoryBean() {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        // 设置数据源
        sqlSessionFactoryBean.setDataSource(dataSource);
        // 设置mybatis的主配置文件
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource mybatisConfigXml = resolver.getResource("classpath:mybatis/mybatis-config.xml");
        sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml);
        // 设置别名包
        sqlSessionFactoryBean.setTypeAliasesPackage("com.shen.cart.pojo");
        return sqlSessionFactoryBean;
    }
}

其中注入方式也可以写成参数注入:

public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {}

然后,创建Mapper接口的扫描类MapperScannerConfig:

@Configuration
@AutoConfigureAfter(MyBatisConfig.class) //保证在MyBatisConfig实例化之后再实例化该类
public class MapperScannerConfig {
    // mapper接口的扫描器
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.shen.cart.mapper");
        return mapperScannerConfigurer;
    }
}

3、设置事务管理

    在Spring Boot中推荐使用@Transactional注解来申明事务。首先需要导入依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

    当引入jdbc依赖之后,Spring Boot会自动默认分别注入DataSourceTransactionManager或JpaTransactionManager,所以我们不需要任何额外配置就可以用@Transactional注解进行事务的使用。然后在Service中添加@Transactional注解即可。

4、整合 Redis

    在Spring Boot中提供了RedisTempplate的操作,我们暂时不做使用,先按照我们之前的实现来完成。我们创建RedisSpringConfig如下:

@Configuration
@PropertySource(value = "classpath:redis.properties")
public class RedisSpringConfig {
    @Value("${redis.maxTotal}")
    private Integer redisMaxTotal;
    @Value("${redis.node1.host}")
    private String redisNode1Host;
    @Value("${redis.node1.port}")
    private Integer redisNode1Port;
    private JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(redisMaxTotal);
        return jedisPoolConfig;
    }
    @Bean
    public ShardedJedisPool shardedJedisPool() {
        List<JedisShardInfo> jedisShardInfos = new ArrayList<JedisShardInfo>();
        jedisShardInfos.add(new JedisShardInfo(redisNode1Host, redisNode1Port));
        return new ShardedJedisPool(jedisPoolConfig(), jedisShardInfos);
    }
}

附加,包含的资源文件:

redis.properties

redis.maxTotal=200
redis.node1.host=127.0.0.1
redis.node1.port=6379

说明:在整合redis集群继续add即可。
5、整合Httpclient

@Configuration
@PropertySource(value = "classpath:httpclient.properties")
public class HttpclientSpringConfig {
    @Value("${http.maxTotal}")
    private Integer httpMaxTotal;
    @Value("${http.defaultMaxPerRoute}")
    private Integer httpDefaultMaxPerRoute;
    @Value("${http.connectTimeout}")
    private Integer httpConnectTimeout;
    @Value("${http.connectionRequestTimeout}")
    private Integer httpConnectionRequestTimeout;
    @Value("${http.socketTimeout}")
    private Integer httpSocketTimeout;
    @Value("${http.staleConnectionCheckEnabled}")
    private Boolean httpStaleConnectionCheckEnabled;
​
    @Autowired
    private PoolingHttpClientConnectionManager manager;
​
    @Bean
    public PoolingHttpClientConnectionManager poolingHttpClientConnectionManager() {
        PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
        // 最大连接数
        poolingHttpClientConnectionManager.setMaxTotal(httpMaxTotal);
        // 每个主机的最大并发数
        poolingHttpClientConnectionManager.setDefaultMaxPerRoute(httpDefaultMaxPerRoute);
        return poolingHttpClientConnectionManager;
    }
​
    // 定期关闭无效连接
    @Bean
    public IdleConnectionEvictor idleConnectionEvictor() {
        return new IdleConnectionEvictor(manager);
    }
​
    // 定义Httpclient对
    @Bean
    @Scope("prototype")
    // 多例对象
    public CloseableHttpClient closeableHttpClient() {
        return HttpClients.custom().setConnectionManager(this.manager).build();
    }
​
    // 请求配置
    @Bean
    public RequestConfig requestConfig() {
        return RequestConfig.custom().setConnectTimeout(httpConnectTimeout) // 创建连接的最长时间
                .setConnectionRequestTimeout(httpConnectionRequestTimeout) // 从连接池中获取到连接的最长时间
                .setSocketTimeout(httpSocketTimeout) // 数据传输的最长时间
                .setStaleConnectionCheckEnabled(httpStaleConnectionCheckEnabled) // 提交请求前测试连接是否可用
                .build();
    }
​
}

附加,包含的资源文件内容如下:

httpclient.properties

http.defaultMaxPerRoute=100
http.maxTotal=300
http.connectTimeout=1000
http.connectionRequestTimeout=500
http.socketTimeout=10000
http.staleConnectionCheckEnabled=true

6、整合RabbitMQ

    我们之前使用的Spring-Rabbit的xml方式,现在我们要改造成java方式,并且Spring Boot对RabbitMQ的使用做了自动配置,更加的简化了我们的使用。首先导入spring-boot-starter-amqp的依赖: 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

然后在application.properties(全局配置文件)文件中配置RabbitMQ的连接信息:

spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.password=123456
spring.rabbitmq.username=shen
spring.rabbitmq.virtual-host=/project

之后编写Rabbit的Spring配置类如下:

@Configuration
public class RabbitMQSpringConfig {
    @Autowired
    private ConnectionFactory connectionFactory;
    // 管理
    @Bean
    public RabbitAdmin rabbitAdmin() {
        return new RabbitAdmin(connectionFactory);
    }
    // 声明队列
    @Bean
    public Queue taotaoCartLoginQueue() {
        // 默认就是自动声明的
        return new Queue("PROJECT-CART-LOGIN-QUEUE", true);
    }
    // 声明队列
    @Bean
    public Queue taotaoCartOrderSuccessQueue() {
        // 默认就是自动声明的
        return new Queue("PROJECT-CART-ORDER-SUCCESS-QUEUE", true);
    }
}

最后设置监听如下:

@Component
public class rabbitmqHadler {

    @RabbitListener(queues="PROJECT-CART-LOGIN-QUEUE")
    public void execute(String msg){
    ///...
    }

}

7、设置SpringMVC的配置

    在以前的配置中有如下内容:

现在需要在全局配置文件中配置视图解析器:

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

然后再配置一个自定义拦截器:

@Configuration
public class SpringMVCConfig extends WebMvcConfigurerAdapter {
    @Autowired
    private UserLoginHandlerInterceptor userLoginHandlerInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 判断用户是否登录的拦截器
        registry.addInterceptor(userLoginHandlerInterceptor).addPathPatterns("/cart/**");
    }
}

其中自定义拦截器内容如下:

@Component
public class UserLoginHandlerInterceptor implements HandlerInterceptor {
​
    public static final String COOKIE_NAME = "TT_TOKEN";
    @Autowired
    private UserService userService;
​
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        String token = CookieUtils.getCookieValue(request, COOKIE_NAME);
        if (StringUtils.isEmpty(token)) {
            // 未登录状态
            return true;
        }
        User user = this.userService.queryUserByToken(token);
        if (null == user) {
            return true;
        }
        UserThreadLocal.set(user); // 将对象放入到本地线程中
        return true;
    }
​
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
​
    }
​
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception ex) throws Exception {
        UserThreadLocal.set(null);
    }
}

8、设置dubbo的配置

    Dubbo目前只能使用xml配置的方式,所以我们需要保留xml,并且需要将该xml加入到现有的Spring容器中才能生效。

第一步:将dubbo目录以及下面的xml配置文件拷贝到springboot项目中,内容如下:

dubbo-consumer.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
​
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="project-cart-consumer" />
​
    <!-- 这里使用的注册中心是zookeeper -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient"/>

    <!-- 从注册中心中查找服务 -->
    <dubbo:reference id="userQueryService" interface="com.shen.sso.query.service.UserQueryService"/>

</beans>

​然后将dubbo的xml文件加入到spring容器

@ImportResource(value = "classpath:dubbo/dubbo-consumer.xml")
@SpringBootApplication
public class Projectplication extends SpringBootServletInitializer{
}

三、编写入口类

然后编写入口类如下:

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

此时全局配置文件(application.properties)内容如下:

server.port=8086
server.servlet-path=/
​
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
​
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.password=123456
spring.rabbitmq.username=shen
spring.rabbitmq.virtual-host=/project
​
#logging.level.org.springframework=DEBUG

整合完毕。

四、将spring boot程序发布到独立的tomcat中运行

    在开发阶段我们推荐使用内嵌的tomcat进行开发,因为这样会方便很多,但是到生成环境,我希望在独立的tomcat容器中运行,因为我们需要对tomcat做额外的优化,这时我们需要将工程打包成war包发进行发布。

第一步:工程的打包方式为war。

第二步:将spring-boot-starter-tomcat的范围设置为provided,设置为provided是在打包时会将该包排除,因为要放到独立的tomcat中运行,是不需要的。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

第三步:修改代码,设置启动配置。需要集成SpringBootServletInitializer,然后重写configure,将Spring Boot的入口类设置进去。

@Configuration
@PropertySource(value = { "classpath:jdbc.properties", "classpath:httpclient.properties" })
@ComponentScan(basePackages = "com.Project")
@ImportResource(value = "classpath:dubbo/dubbo-consumer.xml")
@SpringBootApplication
public class ProjectApplication extends SpringBootServletInitializer{

    @Value("${jdbc.url}")
    private String jdbcUrl;

    @Value("${jdbc.driverClassName}")
    private String jdbcDriverClassName;

    @Value("${jdbc.username}")
    private String jdbcUsername;

    @Value("${jdbc.password}")
    private String jdbcPassword;

    @Bean(destroyMethod = "close")
    public DataSource dataSource() {
        BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
        // 数据库驱动
        boneCPDataSource.setDriverClass(jdbcDriverClassName);
        // 相应驱动的jdbcUrl
        boneCPDataSource.setJdbcUrl(jdbcUrl);
        // 数据库的用户名
        boneCPDataSource.setUsername(jdbcUsername);
        // 数据库的密码
        boneCPDataSource.setPassword(jdbcUsername);
        // 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0
        boneCPDataSource.setIdleConnectionTestPeriodInMinutes(60);
        // 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0
        boneCPDataSource.setIdleMaxAgeInMinutes(30);
        // 每个分区最大的连接数
        boneCPDataSource.setMaxConnectionsPerPartition(100);
        // 每个分区最小的连接数
        boneCPDataSource.setMinConnectionsPerPartition(5);
        return boneCPDataSource;
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(ProjectApplication.class);
    }

}

第四步:打war包

第五步:部署到tomcat。将war包解压到webapps下的ROOT目录中,启动即可。



关注微信服务号,手机看文章
关注微信服务号,手机看文章