Microservices with Springboot

2

I started developing an application using the microservices concept and I still have many doubts. I have repositories and their respective entities, the submissions of the requests work correctly however when I finish the application and start again I lose the data. For example, I send a post that saves the data in the database, dou get e I get it correctly however if I restart the application my get does not return nothing .
Why does this occur? How do I get all the data?

Repository

@RepositoryRestResource(collectionResourceRel = "veiculos", path = "veiculos")
public interface VeiculoRepository extends MongoRepository<Veiculo, String> {
    Veiculo save(Veiculo veiculo);

    List<Veiculo> findAll();

}

Application.properties

spring.application.name=VeiculoService

spring.data.mongodb.uri=mongodb://localhost:27017/db

spring.data.rest.baseUri=/api

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.http.gzip.enabled=true


management.context-path=/actuator

info.app.name=Db Admin
info.app.description=DB
info.app.version=${project.version}

server.port=${port:8181}
server.servletPath=/
    
asked by anonymous 07.07.2015 / 21:24

1 answer

2

When we use spring-boot , by default there is something embedded / in-memory for almost anything, from containers, messaging services and databases, which is this case . Because of this you are able to include and retrieve data in an execution, but when the database is restarted it is removed (by default it will always be create-drop in memory bases, regardless of whether SQL or NoSQL).

For MongoDB there is not even if you generate by link or use default auto configure, but as you have noted, it is configured the Fongo to be your server Mongo, that is, is using a in-memory . I do not know if you are following some tutorial or some application generated for reference, but it should have included configuration to generate an instance Mongo or MongoClient

For example in the case of using MongoDB and spring-boot-starter-data-mongodb will enable MongoDataAutoConfiguration , since spring-boot-starter-data-mongodb will include MongoDB dependencies ( Mongo and MongoTemplate ). This is due to @ConditionalOnClass existing in the configuration ( @ConditionalOnClass({ Mongo.class, MongoTemplate.class}) ).

To configure a server, so that its base is not in memory, you can set the spring.data.mongodb.uri property or those that are specific to host and port, spring.data.mongodb.host , and spring.data.mongodb.port , respectively, in its application.properties / application.yml file. Here you can see all the properties you can use in the boot , not just just those related to Mongo.

Regarding your question about the HTTP requests that were handled in the repository, this is due to the fact that you are using spring-data-rest , see that your repository is annotated with @RepositoryRestResource , then RepositoryRestConfiguration will act and expose HTTP resources through the repository.

To use as you need, separating responsibilities between controller and repository , you can use normal repository of spring-data ( @Repository or nothing, but keep inheriting of MongoRepository ) and the controllers as in Spring MVC (% with% or @Controller ), then the controllers retrieve an instance of the repository (s) you need.

    
08.07.2015 / 16:40