Walking through the generated files

Let's start with the app.yml file under src/main/docker, which can be found inside your gateway application.

As we saw at the beginning of this chapter, the file starts with the Docker version that it supports:

version: '2'

This is followed by the services section, where various services, applications, or components that we will kick start with this Docker Compose file are defined. 

Under the services section, we have a name for the service  in our case, store-app  followed by the image that is used as the container. This image is generated with the help of JIB from our Gradle build.

This is followed by a series of environment variables that our application will depend on, including the following:

  • SPRING_PROFILES_ACTIVE: This tells the application to run in production mode and expose Swagger endpoints.
  • EUREKA_CLIENT_SERVICE_URL_DEFAULTZONE: This tells the application where to check for the JHipster Registry (which is the Eureka client that we are using. If we have chosen Consul here, then the application will point to the Consul URL).
  • SPRING_CLOUD_CONFIG_URI: This tells the application where to look for the config service for the application.
  • SPRING_DATASOURCE_URL: This tells the application where to look for the data source.
  • JHIPSTER_SLEEP: This is the custom property that we have used to make sure that the JHipster Registry starts before the application starts up.

Finally, the port that the application should run and be exposed on is specified: 

  store-app:
image: store
environment:
- _JAVA_OPTIONS=-Xmx512m -Xms256m
- SPRING_PROFILES_ACTIVE=prod,swagger
- MANAGEMENT_METRICS_EXPORT_PROMETHEUS_ENABLED=true
- EUREKA_CLIENT_SERVICE_URL_DEFAULTZONE=...
- SPRING_CLOUD_CONFIG_URI=...
- SPRING_DATASOURCE_URL=..
- JHIPSTER_SLEEP=30 # gives time for other services to boot before the application
ports:
- 8080:8080

We have seen how the service is defined with the Docker Compose file. Now, let's look at two other services that are needed for our application to run. These are the database and JHipster Registry.

So, we have another service called store-mysql, which creates and starts the MySQL server. Since we already have MySQL as a separate Docker Compose file, it is linked here with an extends keyword, followed by the Docker Compose file and the service that we have to start from the specified file:

  store-mysql:
extends:
file: mysql.yml
service: store-mysql

The compose file, mysql.yml, contains the following code:

version: '2'
services:
store-mysql:
image: mysql:8.0.18
# volumes:
# - ~/volumes/jhipster/store/mysql/:/var/lib/mysql/
environment:
- MYSQL_USER=root
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_DATABASE=store
ports:
- 3306:3306
command: mysqld --lower_case_table_names=1 --skip-ssl --character_set_server=utf8mb4 --explicit_defaults_for_timestamp

This started with the version of Docker Compose, followed by the services keyword, and then specified the service name, store-mysql, that is used in the app.yml file.

If you want to specify a volume for the persistent data storage, you can uncomment the commented volumes segment. This basically maps the local file location to Docker's internal location so that the data is persistent, even if the Docker image itself is replaced or updated. This is recommended.

This is followed by a set of environment variables, such as the username and the password (it is set to empty here, but for a real production application, it is recommended to set a more complex password), and then the database schema name.

The command that needs to run in order to start the MySQL server is defined next. Now, we need to go back to the app.yml file and look at the JHipster Registry service. This is again extending the jhipster-registry.yml file. One more thing to note here is that even though we extend the services from another Docker Compose file, we can override the environment variables that we specified in the original Docker Compose file. This comes in handy in certain cases where we have to kickstart our application with different or customized values. In this case, the Spring Cloud Config server file location is overridden:

  jhipster-registry:
extends:
file: jhipster-registry.yml
service: jhipster-registry
environment:
- SPRING_CLOUD_CONFIG_SERVER_COMPOSITE_0_TYPE=native
- SPRING_CLOUD_CONFIG_SERVER_COMPOSITE_0_SEARCH_LOCATIONS=file:
./central-config/docker-config/

The Jhipster-registry.yml file is as follows:

version: '2'
services:
jhipster-registry:
image: jhipster/jhipster-registry:v5.0.2
volumes:
- ./central-server-config:/central-config
environment:
- _JAVA_OPTIONS=-Xmx512m -Xms256m
- SPRING_PROFILES_ACTIVE=dev,swagger
- SPRING_SECURITY_USER_PASSWORD=admin
- JHIPSTER_REGISTRY_PASSWORD=admin
- SPRING_CLOUD_CONFIG_SERVER_COMPOSITE_0_TYPE=native
- SPRING_CLOUD_CONFIG_SERVER_COMPOSITE_0_SEARCH_LOCATIONS=file:
./central-config/localhost-config/
# - SPRING_CLOUD_CONFIG_SERVER_COMPOSITE_0_TYPE=git
# - SPRING_CLOUD_CONFIG_SERVER_COMPOSITE_0_URI=https://github.com/
jhipster/jhipster-registry/
# - SPRING_CLOUD_CONFIG_SERVER_COMPOSITE_0_SEARCH_PATHS=central-config
ports:
- 8761:8761

The central-config file for JHipster Registry can be seen in the following code block. The JWT secret and Eureka client's URL are configured here. The JWT token that's specified allows services to authorize and communicate between themselves and the registry:

# Common configuration shared between all applications
configserver:
name: Docker JHipster Registry
status: Connected to the JHipster Registry running in Docker
jhipster:
security:
authentication:
jwt:
secret: my-secret-token-to-change-in-production
eureka:
client:
service-url:
defaultZone: http://admin:${jhipster.registry.
password}@localhost:8761/eureka/

Apart from these, JHipster also generated the sonar.ymlhazelcast-management-center.yml, and monitoring.yml files (these files are not important for deploying your application).

Similarly, in the microservices, that is, in our invoice and the notification applications, similar files will be generated. They are the same except for the change in the service name.

MongoDB can also run as a cluster with different nodes and configurations and, hence, it has a slightly more complex configuration here. Here, we have two Docker Compose files:

  •  mongodb.yml is for starting MongoDB with a single node.
  • mongodb-cluster.yml is used to start MongoDB as a cluster.

Please check the database port number between the gateway and the microservice application that contains the MySQL database. Since they use the same database, there may be a clash in the port number since JHipster generates the same port number for both. Change it to any other unused port; otherwise, Docker Compose will throw an error. In our case, I have changed the exposed port mapping to 3307:3306 in the invoice service's mysql.yml file. This port is only used if you want to connect to the DB from outside the docker network; the invoice application can still access it via port 3306, which is internal to the service.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.145.114