什么是Spring Boot? 百度百科 官网解释
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
Spring Boot微服务详细搭建配置
- 下载IntelliJ IDEA开发工具: (http://www.jetbrains.com/idea/) [IntelliJ IDEA下载包]
- https://blog.csdn.net/qq_41879385/article/details/81952656) [IntelliJ IDEA安装步骤]
- 点击File-new Project-Spring Intializr-一路next即可
- 在com.example.demo中新建一个controller包:在该包下新建一个控制器DemoController
并添加如下代码:
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class DemoController {
@RequestMapping("/index")
public Map index(){
Map map=new HashMap();
map.put("name","一只流浪的KK");
map.put("type","公猿");
map.put("sex","male");
return map;
}
}
主程序DemoApplication添加如下代码和头文件
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages={"com.example.demo.controller"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
5:启动应用如果报端口占用则可在application.properties中添加新的端口:
server.port=8181
server:
port: 8089
spring:
datasource:
name: test
url: jdbc:mysql://107.30.3.257:3306/workmanual?characterEncoding=utf-8
username: root
password: 520silenceA!
# 使用druid数据源
type: 'com.alibaba.druid.pool.DruidDataSource'
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
## 该配置节点为独立的节点,有很多同学容易将这个配置放在spring的节点下,导致配置无法被识别
mybatis:
mapper-locations: classpath:mapping/*.xml #注意:一定要对应mapper映射xml文件的所在路径
type-aliases-package: com.ssm.entity # 注意:对应实体类的路径
#pagehelper分页插件
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countS
但需注意的是pom.xml中需要配置
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
具体的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 https://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>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo_02</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>demo_02</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mybatis-spring适配器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--mysql驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
8:spring boot项目打包本地部署及线上部署
本地:点击工具栏view - toolwindows-maven-
package即可,然后在相应目录找到jar包,终端执行
java -jar demo_02-0.0.1-SNAPSHOT.jar
即可通过localhost:8089/product
线上部署同本地:上传jar包到服务器:
scp /Users/charles/Desktop/demo_02/target/demo_02-0.0.1-SNAPSHOT.jar root@103.31.3.254:/root/demos
然后cd 到demos目录执行jar命令:
java -jar demo_02-0.0.1-SNAPSHOT.jar
部署成功后即可通过http://118.31.3.252:8089/product访问
网友评论