快速构建spring boot 微服务工程

1 篇文章 0 订阅
1 篇文章 0 订阅

pom.xml如下:

<?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/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>org.gvinaxu.spring.boot</groupId>
  <artifactId>SpringBootDemo</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>

  <name>A Camel Route</name>

    <properties>
        <mybatis.version>1.1.1</mybatis.version>
        <druid.version>1.0.7</druid.version>
        <start-class>org.gvinaxu.springboot.demo.Application</start-class>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.14</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>


        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.21</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--替换tomcat为jetty -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <!-- 数据库 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.5</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>

        <!-- log4j2 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>2.6.2</version>
        </dependency>

        <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.25.2-RELEASE</version>
            <exclusions>
                <exclusion>
                    <artifactId>logback-classic</artifactId>
                    <groupId>ch.qos.logback</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.7.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
        </dependency>
    </dependencies>
    <!-- build -->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <!-- <includes> <include>**/*.xml</include> </includes> <filtering>false</filtering> -->
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <!-- <excludes> <exclude>*</exclude> </excludes> -->
            </resource>
        </resources>
        <plugins>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                    <compilerArguments>
                        <extdirs>library</extdirs>
                    </compilerArguments>
                </configuration>
                <version>2.3.2</version>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>  <!--执行器 mvn assembly:assembly -->
                    <execution>
                        <id>make-zip</id><!--名字任意 -->
                        <phase>package</phase><!-- 绑定到package生命周期阶段上 -->
                        <goals>
                            <goal>single</goal><!-- 只运行一次 -->
                        </goals>
                        <configuration>
                            <descriptors> <!--描述文件路径 -->
                                <descriptor>src/main/assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

结构

创建配置文件
其中log4j2.properties为日志配置文件
application.properties为主配置文件
这里用properties配置文件方式配置,大家也可以用yml配置文件方式配置,看大家喜好
其余三个则对应环境

主配置文件:
spring.profiles.active=dev
其中 active代表对应的环境

server.display-name: org-gvinaxu-spring
server.port:6666
server.contextPath=/boot

#ConfigurableEnvironment
spring.profiles.active=dev

#configure 日志配置文件
spring.logging.config=classpath:log4j2.properties

#configure fastdfs
fdfs.soTimeout=1500
fdfs.connectTimeout=600
fdfs.thumbImage.width=150
fdfs.thumbImage.height=150
#fdfs.trackerList[0]=215.128.18.152:22122
fdfs.trackerList[0]=192.168.1.20:22122
spring.jmx.enabled=false
spring.http.multipart.maxFileSize=4Mb
spring.http.multipart.maxRequestSize=4Mb
multipart.maxFileSize=4Mb
multipart.maxRequestSize=4Mb

其余环境的配置文件(这里只贴dev环境)

spring.datasource.name=DEMO
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=3368
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.filters=mergeStat
spring.datasource.maxActive=500
spring.datasource.initialSize=0
spring.datasource.maxWait=60000
spring.datasource.minIdle=0
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=25200000
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxOpenPreparedStatements=20
spring.datasource.removeAbandoned=true
spring.datasource.removeAbandonedTimeout=1800
spring.datasource.logAbandoned=true

##redis configure
# REDIS (RedisProperties)
spring.redis.database=0  
# Redis\u670D\u52A1\u5668\u5730\u5740
spring.redis.host=192.168.1.3
spring.redis.port=6379  
spring.redis.password=foobared
spring.redis.pool.max-active=8  
spring.redis.pool.max-wait=-1  
spring.redis.pool.max-idle=8  
spring.redis.pool.min-idle=0  
spring.redis.timeout=0  

发布服务启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;


@SpringBootApplication
@EnableTransactionManagement
@ComponentScan(basePackages={"org.gvinaxu.springboot"})
@ServletComponentScan
public class Application {

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

使用请求访问服务:

@RestController
@RequestMapping(value = "/demo")
public class DemoController {

    @PostMapping(value = "/post")
    public String post(HttpServletRequest request, HttpServletResponse response){
         return "HelloWorld";
    }

    @GetMapping(value = "/get")
    public String get(HttpServletRequest request, HttpServletResponse response){
        return "HelloWorld";
    }
}

启动服务
这里写图片描述

接下来访问我们的服务

这里写图片描述

访问成功

boot作为一个为服务框架很好作为springmvc的替代品,总体来说也很简单;总之spring的全家桶基本上每个子项目都很好的支持spring框架,例如Spring Security代替shiro;

先写到这里吧,有空会写有关spring boot 的其他内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值