In the previous article, we used Docker to build a spring-boot application, which built the compiled jar package into the image. This article runs spring-boot together with the database as a set of docker services. Here I just record my own operations. For the complete running code, please see the content in reference 1 in "Reference". Main steps:
Build a simple springboot application Make a web application to count the number of IP addresses that visit the site. And store it in mysql database, here we use jpa to access the database. rely <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> Dependencies of web, jpa, mysql, and tset libraries <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> Configuration Files 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 spring.jpa.properties.hibernate.hbm2ddl.auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.show-sql=true Core code @RestController public class VisitorController { @Autowired private VisitorRepository repository; @RequestMapping("/") public String index(HttpServletRequest request) { String ip = request.getHeader("X-Real-IP"); if(ip== null || "".equals(ip)) { ip = request.getRemoteAddr(); } Visitor visitor = repository.findByIp(ip); if(visitor == null) { visitor = new Visitor(); visitor.setIp(ip); visitor.setTimes(1L); } else { visitor.setTimes(visitor.getTimes()+1); } repository.save(visitor); return "ip:"+visitor.getIp()+" "+visitor.getTimes()+" times."; } } Entity Class @Entity public class Visitor { @Id @GeneratedValue private Long id; @Column(nullable=false) private Long times; @Column(nullable=false) private String ip; // get, set methods omitted} Repository layer code refers to jpa related content. The local database is opened, and the password is configured above. After running with mvn spring-boot:run, you can see the number of IPs, which increases automatically after each statistics. Docker Compose Configuration File Create a new docker-compose.yaml file as follows: 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" volumes: - ./mysqldata:/var/lib/mysql 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 Mainly explain this configuration file and add related configuration in the file system. There are three services under services: nginx, mysql, and app. The purpose of the volumes configuration under nginx is to directly overwrite the nginx configuration file we wrote to the default nginx configuration file in the container. The purpose of the volumes configuration under MySQL is to map the MySQL data files to the local mysqldata directory. When the container is deleted, the data remains. The function of the volumes configuration under app: The first line maps the code file to the container. The second line maps the Maven repository file to the local one. After deleting the container, you can build it again without re-downloading dependent packages. The command is to compile and run the project in the container, using Docker profiles. So we have to add the file
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; } } Deployment Validation Copy the entire file to the server and run it using The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Briefly describe the difference between Redis and MySQL
>>: js implements the pop-up login box by clicking the pop-up window
Here is a brief introduction to indexes: The purp...
Table of contents 1. Scenario 2. Basic functions ...
1. Introduction Responsive Web design allows a we...
This article briefly describes how to use Docker ...
<br />This article is mainly to let beginner...
Table of contents 1. Error message 2. Cause of er...
Table of contents 1. What is JSON 1.1 Array liter...
Table of contents 1. typeof 2. instanceof 3. Cons...
illustrate: VMware IOInsight is a tool that helps...
Table of contents Server Planning 1. Install syst...
Copy code The code is as follows: <!--[if IE]&...
For Windows User Using openGauss in Docker Pull t...
Recently, Xiao Ming just bought a new Mac and wan...
When you send a network request, the following sa...
Table of contents 1. Props parent component ---&g...