Microservices with Springboot

Asked

Viewed 455 times

2

I started to develop an application using the concept of microservices and I still have a lot of doubts. I have repositories and their respective entities, requests submissions work correctly but when I finish the application and start again I lose the data.
For example, I send one post that saves the data in the bank, I give get and get correctly however if I restart the application my get does not return nothingness.
Why does this happen? 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=/
  • Is using the spring-boot-starter-data-mongodb is? Would you be able to update with your application.properties, if using? Something else, has some custom configuration?

  • Wow, I just noticed there’s a configuration of Fongo, mongoDB in memory.

  • 1

    Exactly, by default it is always in memory, when you do not configure your server, there is in the boot something Embedded for everything =D See if you can configure, if not, update with your questions :)

  • Taking advantage, there is use of Spring REST and any post, get, delete etc fall into these repositories. Is it possible to switch to the API and then call the repository? Or is this not the purpose?

  • 1

    This is why you’re wearing spring-data-rest, then HTTP resources are published for each repository. To use as you said, use normal repository (@Repository) and controllers in the same way as in Spring MVC (@Controller or @RestController)

  • Perfect, thank you! If you can publish in the form of a reply

  • Okay, then I’ll respond including references =)

Show 2 more comments

1 answer

2


When we use the spring-boot, by default there is something Embedded/in-memory for almost everything from containers, messaging services and databases, which is this case. Due to this you can include and recover data in one run, but when restarting the database is removed (by default it will always be create-drop in-memory databases, regardless of whether SQL or Nosql).

For Mongodb there is not even if you generate by http://start.spring.io/ or use the default auto configure, but as you well noted, you have configured the Fongo to be his server Mongo, that is, is using a gang in-memory. I don’t know if you are following some tutorial or some application generated for reference, but it must have included configuration to generate an instance Mongo or MongoClient

For example in case you are using Mongodb and spring-boot-starter-data-mongodb will enable the MongoDataAutoConfiguration, since the 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 set up a server, so that your database is not in memory, you can set the property spring.data.mongodb.uri Or those that are specific to host and door, spring.data.mongodb.host and spring.data.mongodb.port, respectively, in your file application.properties/application.yml. Here you can see all the properties you can use on boot, not only those related to Mongo.

As for your doubt 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 spring-data (@Repository or nothing, but continue to inherit MongoRepository) and controllers like in Spring MVC (@Controller or @RestController), from the controllers retrieves an instance from the repository(s) you need.

  • You are one of the few I found here at Sopt who understand about Spring, if you can help me http://answall.com/questions/74631/requisi%C3%A7%C3%B5es-para-api-Rest

Browser other questions tagged

You are not signed in. Login or sign up in order to post.