- Mastering Spring Cloud
- Piotr Minkowski
- 521字
- 2021-08-27 18:56:59
Running the application
Let's start MongoDB using the Docker run command:
docker run -d --name mongo -p 27017:27017 mongo
Something that may be useful for us is the Mongo database client. Using this, it is possible to create a new database and add some users with credentials. If you have Docker installed on Windows, the default virtual machine address is 192.168.99.100. The Mongo container has port 27017 exposed as a result of setting the -p parameter inside the run command. Well, in fact, we do not have to create the database because, when we provide the name while defining the client connection, it will automatically be created if it doesn't exist:
Next, we should create a user for the application with sufficient authority:
Finally, we should set the Mongo database connection settings and credentials in the application.yml configuration file:
server:
port: ${port:2222}
spring:
application:
name: first-service
// ...
---
spring:
profiles: production
application:
name: first-service
data:
mongodb:
host: 192.168.99.100
port: 27017
database: microservices
username: micro
password: micro
Spring Boot has good support for multi-profile configuration. A YAML file can be separated into a sequence of documents using --- lines, and each section of the document is parsed independently to a flattened map. The preceding example does exactly the same as a separated configuration file with application-production.yml. If you run the application without any additional options, it uses the default settings, which have no profile name set. If you would like to run it using production properties, you should set the VM argument spring.profiles.active:
java -jar -Dspring.profiles.active=production sample-spring-boot-web-1.0-SNAPSHOT.jar
That's not all. Now, the application with the active production profile failed to start because it tried to initialize the embeddedMongoServer bean. As you might already know, almost all of the additional solutions have auto configuration set in Spring Boot. It is no different in this case. We need to exclude the EmbeddedMongoAutoConfiguration class from auto configuration in the production profile:
spring:
profiles: production
// ...
autoconfigure:
exclude: org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration
We might as well use the configuration class to exclude that artifact:
@Configuration
@Profile("production")
@EnableAutoConfiguration(exclude = EmbeddedMongoAutoConfiguration.class)
public class ApplicationConfig {
// ...
}
Of course, we could have used a more elegant solution, such as Maven profiles, and excluded the whole de.flapdoodle.embed.mongo artifact from the target build package. The presented solution is just one of several possibilities to solve the problem, but it shows the auto configuration and profile mechanisms in Spring Boot. Now, you can run our sample application and perform some tests using, for example, Swagger UI. You can also connect to the database using the Mongo client and check out the changes in the database. Here's our sample project's final file structure:
pl
+- piomin
+- services
+- boot
+- Application.java
|
+- controller
| +- PersonController.java
|
+- data
| +- PersonRepository.java
|
+- model
| +- Person.java
| +- Gender.java
|
+- service
| +- PersonCounterService.java
The example application is complete. These are all Spring Boot features I would like to show you in this chapter. I have focused on those that are especially useful for creating REST-based services.