Skip to content

Commit

Permalink
add dockercompose-springboot-mysql-nginx
Browse files Browse the repository at this point in the history
  • Loading branch information
ityouknow committed Dec 28, 2022
1 parent 9df27f3 commit 49ffb0d
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Spring Boot 使用的各种示例,以最简单、最实用为标准,此开
- [spring-boot-file-upload](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-file-upload):Spring Boot 3.0 上传文件使用案例
- [spring-boot-commandLineRunner](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-commandLineRunner):Spring Boot 3.0 目启动时初始化资源案例
- [spring-boot-docker](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-docker):Spring Boot 3.0 Docker 使用案例
- [dockercompose-springboot-mysql-nginx](https://github.com/ityouknow/spring-boot-examples/tree/master/dockercompose-springboot-mysql-nginx):Spring Boot 3.0 Docker Compose + Spring Boot + Nginx + Mysql 使用案例
- [spring-boot-memcache-spymemcached](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-memcache-spymemcached):Spring Boot 3.0 集成 Memcached 使用案例


Expand Down
1 change: 1 addition & 0 deletions dockercompose-springboot-mysql-nginx/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM maven:3.8.3-openjdk-17
67 changes: 67 additions & 0 deletions dockercompose-springboot-mysql-nginx/app/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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>3.0.0</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</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>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<defaultGoal>compile</defaultGoal>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>
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);
}
}
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 jakarta.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.";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.neo.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.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;
}
}
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);
}
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.cj.jdbc.Driver
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.cj.jdbc.Driver
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.MySQLDialect
spring.jpa.show-sql=true

spring.profiles.active=dev
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!");
}

}
36 changes: 36 additions & 0 deletions dockercompose-springboot-mysql-nginx/docker-compose.yaml
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:
- 81:81
- 445:445
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:
- "3308:3308"
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 dockercompose-springboot-mysql-nginx/nginx/conf.d/app.conf
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;
}
}

0 comments on commit 49ffb0d

Please sign in to comment.