forked from ityouknow/spring-boot-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add dockercompose-springboot-mysql-nginx
- Loading branch information
Showing
14 changed files
with
239 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
FROM maven:3.5-jdk-8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
12 changes: 12 additions & 0 deletions
12
dockercompose-springboot-mysql-nginx/app/src/main/java/com/neo/ComposeApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...ompose-springboot-mysql-nginx/app/src/main/java/com/neo/controller/VisitorController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
dockercompose-springboot-mysql-nginx/app/src/main/java/com/neo/entity/Visitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ompose-springboot-mysql-nginx/app/src/main/java/com/neo/repository/VisitorRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
4 changes: 4 additions & 0 deletions
4
dockercompose-springboot-mysql-nginx/app/src/main/resources/application-dev.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
4 changes: 4 additions & 0 deletions
4
dockercompose-springboot-mysql-nginx/app/src/main/resources/application-docker.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
5 changes: 5 additions & 0 deletions
5
dockercompose-springboot-mysql-nginx/app/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
18 changes: 18 additions & 0 deletions
18
dockercompose-springboot-mysql-nginx/app/src/test/java/com/neo/ComposeApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 20 additions & 0 deletions
20
dockercompose-springboot-mysql-nginx/nginx/conf.d/app.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |