In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
The previous article uses docker to build a spring-boot application, which is to build the compiled jar package into an image.
This article is about running spring-boot, along with the database, as a docker service.
This is just to record your own operation, see Reference 1 in Reference for the complete running code.
(I modify mysql mapping directory and get remote ip method)
Main steps:
Build a simple springboot application Add docker support to write dockercompose configuration file Practice running
Build a simple springboot application
Make a web application and count the ip number of visits to the site.
And stored in the mysql database, where jpa access to the database.
dependent
org.springframework.boot spring-boot-starter-parent 2.0.0.RELEASE
Dependency of web,jpa,mysql,tset libraries
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa mysql mysql-connector-java org.springframework.boot spring-boot-starter-test test
profile
spring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.jpa.properties.hibernate.hbm2ddl.auto=updatespring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialectspring.jpa.show-sql=true
core code
@RestControllerpublic 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
@Entitypublic class Visitor { @Id @GeneratedValue private Long id; @Column(nullable=false) private Long times; @Column(nullable=false) private String ip; // get,set method omitted}
Repository layer code reference jpa related content.
The local database is opened. The password is in the above configuration. After running it with mvn spring-boot:run, you can see the number of ip, which will increase after each count.
dockercompose configuration file
Create a 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 the relevant configuration to the file system.
Services There are three services below: nginx,mysql, app.
images indicates the use of mirroring. Nginx and mysql are directly taken from docker repositories.
The app does not specify the mirror, but build specifies the directory where the Dockerfile is located.
volumes specifies the mapping of files in the local directory to container destinations.
environment configures the environment variables required by the container
ports is configured with local and container mapping ports, local port first, container port last
The role of volumes configuration under ngixn: overwrite the nginx configuration file we wrote directly into the default nginx configuration file in the container.
The role of volumes configuration under mysql: mapping mysql data files to the local mysqldata directory. When the container is deleted, the data remains.
What the volumes configuration under app does: The first line maps the code file to the container. The second line maps maven's repository files locally. After the container is deleted, it is built again without downloading the dependency package again.
command: mvn clean spring-boot:run -Dspring-boot.run.profiles=docker
The command is to compile and run the project in the container, using docker profiles.
So the files we're going to add
Dockerfile: Create a new file, add a line FROM maven:3.5-jdk-8docker profiles: copy application.properties to application-docker.properties, and change the database connection address in application-docker.properties to jdbc:mysql://mysql:3306/test. nginx configuration 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 docker-compose up.
The above is all the content of this article, I hope to help everyone's study, but also hope that everyone a lot of support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.