# my-springboot
**Repository Path**: wangzhe_spring/my-springboot
## Basic Information
- **Project Name**: my-springboot
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2021-01-29
- **Last Updated**: 2021-01-29
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
## Mysql服务安装、springboot项目部署、浏览器客户端访问调用
### 1. 准备mysql的deployment文件
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deploy
labels:
app: mysql-deploy
spec:
replicas: 1
template:
metadata:
name: mysql-deploy
labels:
app: mysql-deploy
spec:
containers:
- name: mysql-deploy
image: mysql:5.7
imagePullPolicy: IfNotPresent
env:
- name: MYSQL_ROOT_PASSWORD
value: admin #这是root用户的密码
- name: TZ
value: Asia/Shanghai
args:
- "--character-set-server=utf8mb4"
- "--collation-server=utf8mb4_unicode_ci"
ports:
- containerPort: 3306
volumeMounts:
- mountPath: /var/lib/mysql
name: mysql-volume
restartPolicy: Always
volumes:
- name: mysql-volume
hostPath:
path: /data/mysql
type: Directory
selector:
matchLabels:
app: mysql-deploy
---
apiVersion: v1
kind: Service
metadata:
name: mysql-svc
spec:
selector:
app: mysql-deploy
ports:
- port: 3306
targetPort: 3306
nodePort: 30036
type: NodePort
```
### 2. 准备springboot项目
#### 2.1 创建springboot项目,pom文件如下
```xml
4.0.0
com.wangzhe
my-springboot
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.1.0.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
org.springframework.boot
spring-boot-maven-plugin
```
#### 2.2 配置文件application.properties
```
# 端口
server.port=8080
# 数据库配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.42.177:30036/wangzhe?charset=utf8mb4&useSSL=false
spring.datasource.username=root
spring.datasource.password=admin
```
#### 2.3 项目启动类
```java
package com.wangzhe.my.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author wangzhe
* @date 2021-01-29 14:23
*/
@SpringBootApplication
public class MySpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringbootApplication.class, args);
}
}
```
#### 2.4 实体类 Student
```java
package com.wangzhe.my.springboot.entity;
import javax.persistence.*;
/**
* @author wangzhe
* @date 2021-01-29 14:24
*/
@Entity
@Table(name = "student")
public class Student {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "age")
private Integer age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
```
#### 2.5 DAO层 StudentDAO
```java
package com.wangzhe.my.springboot.dao;
import com.wangzhe.my.springboot.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* @author wangzhe
* @date 2021-01-29 14:31
*/
@Repository
public interface StudentDAO extends JpaRepository {
}
```
#### 2.6 控制层 StudentController
```java
package com.wangzhe.my.springboot.controller;
import com.wangzhe.my.springboot.dao.StudentDAO;
import com.wangzhe.my.springboot.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @author wangzhe
* @date 2021-01-29 14:34
*/
@RestController
public class StudentController {
@Autowired
private StudentDAO studentDAO;
@RequestMapping(value = "/student", method = RequestMethod.GET)
public Object queryAll() {
return studentDAO.findAll();
}
@RequestMapping(value = "/student/{id}", method = RequestMethod.GET)
public Object queryOne(@PathVariable Long id) {
return studentDAO.findById(id);
}
@RequestMapping(value = "/student", method = RequestMethod.POST)
public void addOrUpDate(Student student) {
studentDAO.save(student);
}
}
```
### 3. 构建springboot项目的docker镜像
将项目打包成jar包my-springboot.jar,将jar包和jdk8上传至k8s-node01,路径为/root/dockerfile/。
```shell
# 编辑dockerfile
cd /root/dockerfile
vi dockerfile
# 添加以下内容
FROM centos:7
RUN mkdir /usr/local/java
ADD jdk-8u161-linux-x64.tar.gz /usr/local/java/
ENV JAVA_HOME=/usr/local/java/jdk1.8.0_161
ENV JRE_HOME=$JAVA_HOME/jre
ENV PATH=$JAVA_HOME/bin:$PATH
COPY my-springboot.jar /usr/local/
COPY my-springboot.sh /usr/local/
EXPOSE 8080/tcp
CMD [ "sh", "-c", "/usr/local/my-springboot.sh" ]
```
准备项目启动脚本
```shell
# 编辑my-springboot.sh
cd /root/dockerfile
vi my-springboot.sh
# 添加以下内容
#! /bin/sh
java -jar /usr/local/my-springboot.jar
# 修改文件权限
chmod 777 my-springboot.sh
```
构建docker镜像
```shell
docker build -f dockerfile -t wangzhe/my-springboot:1.0 .
```
### 4. 准备springboot项目的deployment文件
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: springboot-deploy
labels:
app: springboot-deploy
spec:
replicas: 1
template:
metadata:
name: springboot-deploy
labels:
app: springboot-deploy
spec:
containers:
- name: springboot-deploy
image: wangzhe/my-springboot:1.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
restartPolicy: Always
selector:
matchLabels:
app: springboot-deploy
---
apiVersion: v1
kind: Service
metadata:
name: springboot-svc
spec:
selector:
app: springboot-deploy
ports:
- port: 8080
targetPort: 8080
nodePort: 30080
type: NodePort
```
### 5. k8s创建svc、deployment、pod
将mysql.yml、springboot.yml上传至服务器k8s-master,路径为/root/k8s/springboot/。
```shell
cd /root/k8s/springboot/
kubectl apply -f .
```