mirror of
https://github.com/ityouknow/spring-boot-examples.git
synced 2026-05-31 18:09:18 +08:00
add dockercompose-springboot-mysql-nginx
This commit is contained in:
@@ -16,7 +16,7 @@ Spring Boot使用的各种示例,以最简单、最实用为标准
|
||||
- [spring-boot-hello](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-hello):Spring Boot 2.0 Hello World 示例
|
||||
- [spring-boot-banner](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-banner):Spring Boot 定制 Banner 示例
|
||||
- [spring-boot-docker](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-banner):使用 Docker 部署 Spring Boot 示例
|
||||
|
||||
- [dockercompose-springboot-mysql-nginx](https://github.com/ityouknow/spring-boot-examples/tree/master/dockercompose-springboot-mysql-nginx) :Docker Compose + Spring Boot + Nginx + Mysql 示例
|
||||
|
||||
**参考文章**
|
||||
|
||||
|
||||
@@ -15,6 +15,8 @@ Spring Boot Examples, Use the simplest and most useful scene demo.
|
||||
- [spring-boot-hello](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-hello):Spring Boot 2.0 Hello World Demo
|
||||
- [spring-boot-banner](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-banner):Spring Boot Customized Banner
|
||||
- [spring-boot-docker](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-banner):Spring Boot with Docker
|
||||
- [dockercompose-springboot-mysql-nginx](https://github.com/ityouknow/spring-boot-examples/tree/master/dockercompose-springboot-mysql-nginx) :Docker Compose + Spring Boot + Nginx + Mysql example
|
||||
|
||||
|
||||
---
|
||||
|
||||
|
||||
1
dockercompose-springboot-mysql-nginx/app/Dockerfile
Normal file
1
dockercompose-springboot-mysql-nginx/app/Dockerfile
Normal file
@@ -0,0 +1 @@
|
||||
FROM maven:3.5-jdk-8
|
||||
56
dockercompose-springboot-mysql-nginx/app/pom.xml
Normal file
56
dockercompose-springboot-mysql-nginx/app/pom.xml
Normal file
@@ -0,0 +1,56 @@
|
||||
<?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/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.neo</groupId>
|
||||
<artifactId>dockercompose-springboot-mysql-nginx</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>dockercompose-springboot-mysql-nginx</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.0.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<defaultGoal>compile</defaultGoal>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.neo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ComposeApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ComposeApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.neo.controller;
|
||||
|
||||
import com.neo.entity.Visitor;
|
||||
import com.neo.repository.VisitorRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@RestController
|
||||
public class VisitorController {
|
||||
|
||||
@Autowired
|
||||
private VisitorRepository repository;
|
||||
|
||||
@RequestMapping("/")
|
||||
public String index(HttpServletRequest request) {
|
||||
String ip=request.getRemoteAddr();
|
||||
Visitor visitor=repository.findByIp(ip);
|
||||
if(visitor==null){
|
||||
visitor=new Visitor();
|
||||
visitor.setIp(ip);
|
||||
visitor.setTimes(1);
|
||||
}else {
|
||||
visitor.setTimes(visitor.getTimes()+1);
|
||||
}
|
||||
repository.save(visitor);
|
||||
return "I have been seen ip "+visitor.getIp()+" "+visitor.getTimes()+" times.";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.neo.entity;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Visitor {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
@Column(nullable = false)
|
||||
private long times;
|
||||
@Column(nullable = false)
|
||||
private String ip;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public long getTimes() {
|
||||
return times;
|
||||
}
|
||||
|
||||
public void setTimes(long times) {
|
||||
this.times = times;
|
||||
}
|
||||
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
public void setIp(String ip) {
|
||||
this.ip = ip;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.neo.repository;
|
||||
|
||||
import com.neo.entity.Visitor;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface VisitorRepository extends JpaRepository<Visitor, Long> {
|
||||
Visitor findByIp(String ip);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/test
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=root
|
||||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
@@ -0,0 +1,4 @@
|
||||
spring.datasource.url=jdbc:mysql://mysql:3306/test
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=root
|
||||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
@@ -0,0 +1,5 @@
|
||||
spring.jpa.properties.hibernate.hbm2ddl.auto=update
|
||||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
|
||||
spring.jpa.show-sql=true
|
||||
|
||||
spring.profiles.active=dev
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.neo;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class ComposeApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
System.out.println("Hello Spring Boot Docker Compose!");
|
||||
}
|
||||
|
||||
}
|
||||
36
dockercompose-springboot-mysql-nginx/docker-compose.yaml
Normal file
36
dockercompose-springboot-mysql-nginx/docker-compose.yaml
Normal file
@@ -0,0 +1,36 @@
|
||||
version: '3'
|
||||
services:
|
||||
nginx:
|
||||
container_name: v-nginx
|
||||
image: nginx:1.13
|
||||
restart: always
|
||||
ports:
|
||||
- 80:80
|
||||
- 443:443
|
||||
volumes:
|
||||
- ./nginx/conf.d:/etc/nginx/conf.d
|
||||
|
||||
mysql:
|
||||
container_name: v-mysql
|
||||
image: mysql/mysql-server:5.7
|
||||
environment:
|
||||
MYSQL_DATABASE: test
|
||||
MYSQL_ROOT_PASSWORD: root
|
||||
MYSQL_ROOT_HOST: '%'
|
||||
ports:
|
||||
- "3306:3306"
|
||||
restart: always
|
||||
|
||||
app:
|
||||
restart: always
|
||||
build: ./app
|
||||
working_dir: /app
|
||||
volumes:
|
||||
- ./app:/app
|
||||
- ~/.m2:/root/.m2
|
||||
expose:
|
||||
- "8080"
|
||||
depends_on:
|
||||
- nginx
|
||||
- mysql
|
||||
command: mvn clean spring-boot:run -Dspring-boot.run.profiles=docker
|
||||
20
dockercompose-springboot-mysql-nginx/nginx/conf.d/app.conf
Normal file
20
dockercompose-springboot-mysql-nginx/nginx/conf.d/app.conf
Normal file
@@ -0,0 +1,20 @@
|
||||
server {
|
||||
listen 80;
|
||||
charset utf-8;
|
||||
access_log off;
|
||||
|
||||
location / {
|
||||
proxy_pass http://app:8080;
|
||||
proxy_set_header Host $host:$server_port;
|
||||
proxy_set_header X-Forwarded-Host $server_name;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
}
|
||||
|
||||
location /static {
|
||||
access_log off;
|
||||
expires 30d;
|
||||
|
||||
alias /app/static;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user