【云+社区年度征文】从零开始搭建一个SpringBoot应用并成功上云
相信大家都具有在本地编写项目的丰富经验,然而本地的单击项目始终不能满足我们的需求,为此,本篇文章将介绍如何编写一个SpringBoot应用并成功将其部署到云服务器上。
购买云服务器
既然要将应用部署到服务器上,首先我们得有一个服务器,采取云服务器是最佳的选择,市面上也有很多的云服务器商,这里以腾讯云服务器为例,首先来到官网:https://cloud.tencent.com/,在产品中选择轻量应用服务器:
然后点击立即选购:
接着选择一下服务器的一些配置:
地域就选离自己最近的,镜像选择系统镜像CentOS7.6,实例套餐就看自己需求进行选择,全部选择完毕后点击购买即可。
购买完成后点击右上角的控制台:
这里就能够看到我们刚刚购买的服务器了:
我们点击该服务器进入,然后选择重置密码:
然后把密码设置好:
设置完成后,使用XShell软件连接上云服务器:
服务器的ip在刚才的页面中可以找到:
这样服务器的初步配置就完成了,接下来我们搭建一下应用所需的环境。
搭建应用环境
由于是SpringBoot应用,所以我们只需在服务器上安装jdk和mysql即可,tomcat并不需要,但还是一并介绍了吧。
搭建Java环境
首先来到官网下载好jdk:https://www.oracle.com/index.html
下载完成后将其上传至云服务器,上传的操作应该不必多说,方式有很多种:
这里我已经将压缩包放到了/opt目录,接下来对其进行解压,执行指令:
tar -zxvf jdk-8u261-linux-x64.tar.gz
此时jdk就已经安装完成了,但是只有在jdk的bin目录下才能使用java命令,这是因为我们还未配置jdk的环境变量,所以修改/etc下的profile文件:
JAVA_HOME=/opt/jdk1.8.0_261
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH
在文件末尾添加这段内容即可:
这里解释一下这些配置的作用,首先是JAVA_HOME,它配置的是jdk的安装目录,这是压缩包解压后得到的一个目录;然后是PATH,类比Windows系统中的path变量即可,首先通过$JAVA_HOME引用上面配置的值,然后指定/bin目录,Linux中使用:
来分隔每个环境变量,配置完成jdk的环境后还要记得引用$PATH将系统原本的环境变量也引入进来。
在Linux中还需要使用export对这两个变量进行输出。
配置完毕后保存该文件,然后重新加载一下:
source /etc/profile
这样jdk就配置完成了。
安装MySQL
MySQL的安装这里推荐使用在线安装的方式,首先下载安装包,执行指令:
rpm -Uvh http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
然后下载mysql的服务端:
yum install mysql-community-server
下载完毕后启动mysql的服务:
service mysqld start
然后配置一下mysql,比如:默认的字符集、连接数、数据库引擎等等,修改/etc目录下的my.cnf文件,在【mysqld】节点下添加配置:
# 默认字符集
loose-default-character-set = utf8
# 服务器字符集
character_set_server = utf8
# 最大连接数
max_connections = 200
# 数据库引擎
default-storage-engine = InnoDB
在文件末尾添加如下配置:
[client]
#客户端默认字符集
loose-default-character-set = utf8
保存文件然后重启mysql服务:
systemctl restart mysqld
这样mysql就安装好了。
安装Tomcat
tomcat就非常简单了,来到官网下载tomcat的压缩包:
然后解压一下:
tar -zxvf apache-tomcat-8.0.32.tar.gz
这样就完成了。
创建一个SpringBoot应用
到这里关于云服务器的搭建就全部完成了,接下来编写一个SpringBoot应用,首先创建一个数据表:
create database springboot;
use springboot;
create table stu(
id int primary key auto_increment,
name varchar(20),
age int,
sex varchar(5)
);
insert into stu values(null,'张三',20,'男');
insert into stu values(null,'李四',22,'女');
insert into stu values(null,'王五',27,'男');
在idea中创建一个SpringBoot项目:
选择一些依赖:
然后简单编写一个应用,首先编写配置文件:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://42.192.234.4/springboot?serverTimezone=UTC
username: root
password: 123456
mybatis:
mapper-locations: classpath:mappers/*.xml
创建实体类:
package com.wwj.springbootdemo.bean;
public class Student {
private Integer id;
private String name;
private Integer age;
private String sex;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\\'' +
", age=" + age +
", sex='" + sex + '\\'' +
'}';
}
public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
}
}
创建Mapper接口:
package com.wwj.springbootdemo.dao;
import com.wwj.springbootdemo.bean.Student;
import com.wwj.springbootdemo.bean.StudentExample;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface StudentMapper {
long countByExample(StudentExample example);
int deleteByExample(StudentExample example);
int deleteByPrimaryKey(Integer id);
int insert(Student record);
int insertSelective(Student record);
List<Student> selectByExample(StudentExample example);
Student selectByPrimaryKey(Integer id);
int updateByExampleSelective(@Param("record") Student record, @Param("example") StudentExample example);
int updateByExample(@Param("record") Student record, @Param("example") StudentExample example);
int updateByPrimaryKeySelective(Student record);
int updateByPrimaryKey(Student record);
}
创建控制器:
package com.wwj.springbootdemo.controller;
import com.wwj.springbootdemo.bean.Student;
import com.wwj.springbootdemo.bean.StudentExample;
import com.wwj.springbootdemo.dao.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class StudentController {
@Autowired
private StudentMapper studentMapper;
@RequestMapping("/getStu")
public List<Student> getStu(){
return studentMapper.selectByExample(new StudentExample());
}
}
编写Mapper配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wwj.springbootdemo.dao.StudentMapper">
<resultMap id="BaseResultMap" type="com.wwj.springbootdemo.bean.Student">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="INTEGER" property="age" />
<result column="sex" jdbcType="VARCHAR" property="sex" />
</resultMap>
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
id, name, age, sex
</sql>
<select id="selectByExample" parameterType="com.wwj.springbootdemo.bean.StudentExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from stu
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from stu
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from stu
where id = #{id,jdbcType=INTEGER}
</delete>
<delete id="deleteByExample" parameterType="com.wwj.springbootdemo.bean.StudentExample">
delete from stu
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.wwj.springbootdemo.bean.Student">
insert into stu (id, name, age,
sex)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},
#{sex,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.wwj.springbootdemo.bean.Student">
insert into stu
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
<if test="age != null">
age,
</if>
<if test="sex != null">
sex,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
#{age,jdbcType=INTEGER},
</if>
<if test="sex != null">
#{sex,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.wwj.springbootdemo.bean.StudentExample" resultType="java.lang.Long">
select count(*) from stu
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map">
update stu
<set>
<if test="record.id != null">
id = #{record.id,jdbcType=INTEGER},
</if>
<if test="record.name != null">
name = #{record.name,jdbcType=VARCHAR},
</if>
<if test="record.age != null">
age = #{record.age,jdbcType=INTEGER},
</if>
<if test="record.sex != null">
sex = #{record.sex,jdbcType=VARCHAR},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
update stu
set id = #{record.id,jdbcType=INTEGER},
name = #{record.name,jdbcType=VARCHAR},
age = #{record.age,jdbcType=INTEGER},
sex = #{record.sex,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="com.wwj.springbootdemo.bean.Student">
update stu
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
age = #{age,jdbcType=INTEGER},
</if>
<if test="sex != null">
sex = #{sex,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.wwj.springbootdemo.bean.Student">
update stu
set name = #{name,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
sex = #{sex,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
应用这样就编写完成了,启动测试一下,访问http://localhost:8080/getStu
:
在本地测试成功后,我们就可以把它放到云服务器上了,这里我的应用是直接连接了云服务器的数据库,所以数据表的创建需要在云服务器上的mysql中进行,然后记得开放云服务器的3306端口:
mysql也需要开放远程访问权限来进行测试:
use mysql;
grant all on *.* to root@'%' identified by '123456' with grant option;
commit;
一切准备就绪,将SpringBoot应用打包, ·mvn install package`,此时会得到一个jar包,将这个jar包上传到云服务器:
现在就可以来启动它了,执行命令:
java -jar springbootdemo-0.0.1-SNAPSHOT.jar
启动成功,来访问一下,http://42.192.234.4:8080/getStu
:
到这一步如果无法访问的话,记得去开启一下云服务器的8080端口: