Giter Site home page Giter Site logo

book-microservices-v2 / chapter05 Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 13.0 385 KB

Learn Microservices with Spring Boot (2nd edition) - Chapter 5

Home Page: https://tpd.io/book-extra

HTML 5.26% CSS 1.11% JavaScript 37.55% Java 56.07%
spring-boot jpa hibernate-jpa 3-tier-architecture 3-layer query-methods microservice-architecture microservice-example java

chapter05's Introduction

Learn Microservices with Spring Boot - Chapter 5

This repository contains the source code of the practical use case described in the book Learn Microservices with Spring Boot (2nd Edition).

The book follows a pragmatic approach to building a Microservice Architecture. You start with a small monolith and examine the pros and cons that come with a move to microservices.

Chapter 5 version

In Chapter 5, you add a persistence layer to the Multiplication's Spring Boot application, following the 3-layer, 3-tier design patterns. The figure below shows the data model for our application.

Data Model

The main concepts included in this chapter are:

  • Modeling Data Entities
  • Introducing SQL vs NoSQL, Hibernate, and JPA.
  • Covering the main topics about Spring Boot Data JPA, from a practical perspective.
  • Defining JPA Entities for our use case.
  • Reviewing how the 3 layers work together: Controller, Service, Repository.

All these topics are driven by a new feature that you introduce in the evolving app: retrieve the historical data for the attempts of a given user.

You use an H2 embedded database in the implementation, which means you don't need to install anything extra to run the complete system.

The next figure shows the logical view of our application at this stage.

Logical View - Chapter 5

Check the Book's Web Page to see the complete list of chapters.

Running the app

Requirements:

  • JDK 14+
  • Node.js v13.10+
  • npm 6.13.7+
  1. To start the Spring Boot application, you can use the command line with the included Maven wrapper:
    multiplication$ ./mvnw spring-boot:run
  2. The React application can be started with npm. First, you need to download the dependencies with:
    challenges-frontend$ npm install
  3. Then, you start the server with:
    challenges-frontend$ npm start

Once the backend and the fronted are started, you can navigate to http://localhost:3000 in your browser and start resolving multiplication challenges. This time, you'll see a new table that displays your previous attempts.

Application - Screenshot

Additionally, you can access the H2 Console if you want to manage or query the database. The console is located at http://localhost:8080/h2-console.

Questions

  • Do you have questions about how to make this application work?
  • Did you get the book and have questions about any concept explained within this chapter?
  • Have you found issues using updated dependencies?

Don't hesitate to create an issue in this repository and post your question/problem there.

About the book

Are you interested in building a microservice architecture from scratch? You'll face all the challenges of designing and implementing a distributed system one by one, and will be able to evaluate if it's the best choice for your project.

Visit https://tpd.io/book-extra for all the details about the book.

Purchase

You can buy the book online from these stores:

Source code by chapter (all repositories are available on Github)

Extra chapters:

chapter05's People

Contributors

mechero avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

chapter05's Issues

Changing user id and password for H2 database does not work

I tried to overwrite the default user id and password for the h2 database by providing own credentials. To do so I added to the application.properties file these two lines

spring.datasource.user=myuser spring.datasource.password=pw

Via the h2-console I created that user as well. I can log-on to the database with the new user and its password without any problem.

However, when starting the multiplication service, I am getting this error:

org.h2.jdbc.JdbcSQLInvalidAuthorizationSpecException: Falscher Benutzername oder Passwort Wrong user name or password [28000-214]

How can it be if I can log-on to the console with "myuser" and password = "pw" without any problem?

When removing the two lines from the application.properties file it works again as the defaults apply (no user id, no password).

Thanks for your help,
Matthias

javax.persistence missing in classpath

Hi Moisés, when following chapter 5 I came accross the issue, that

import javax.persistence.*;

did not work (javax.persistence cannot be resolved). I added the following dependency in pom.xml which solved the problem:

           `<dependency>
		<groupId>javax.persistence</groupId>
		<artifactId>javax.persistence-api</artifactId>
		<version>2.2</version>
	</dependency>`

Did I miss something when following the book. I am not aware that it is mentioned at some place to add this dependency.

Thanks and best regards,
Matthias

Issue with ChallengeAttemptRepository interface

Hi Moisés, once I added this code

package microservices.book.multiplication.challenge;

import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface ChallengeAttemptRepository extends CrudRepository<ChallengeAttempt, Long> {

    /**
     * @return the last 10 attempts for a given user, identified by their alias.
     */
    List<ChallengeAttempt> findTop10ByUserAliasOrderByIdDesc(String userAlias);
}

This is my repository: https://github.com/wagenbm/LearnMicroservices.git

I cannot run the application anymore. This is the console output I get when running the application:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:3.0.1:run (default-cli) on project multiplication: Process terminated with exit code: 1 -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:3.0.1:run (default-cli) on project multiplication: Process terminated with exit code: 1
at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:375)
at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:351)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:171)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:163)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:294)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:104)
at java.lang.reflect.Method.invoke (Method.java:578)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:104)
at java.lang.reflect.Method.invoke (Method.java:578)
at org.apache.maven.wrapper.BootstrapMainStarter.start (BootstrapMainStarter.java:47)
at org.apache.maven.wrapper.WrapperExecutor.execute (WrapperExecutor.java:156)
at org.apache.maven.wrapper.MavenWrapperMain.main (MavenWrapperMain.java:72)
Caused by: org.apache.maven.plugin.MojoExecutionException: Process terminated with exit code: 1
at org.springframework.boot.maven.JavaProcessExecutor.run (JavaProcessExecutor.java:74)
at org.springframework.boot.maven.RunMojo.run (RunMojo.java:68)
at org.springframework.boot.maven.AbstractRunMojo.run (AbstractRunMojo.java:218)
at org.springframework.boot.maven.AbstractRunMojo.execute (AbstractRunMojo.java:205)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:370)
at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:351)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:171)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:163)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:294)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:104)
at java.lang.reflect.Method.invoke (Method.java:578)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:104)
at java.lang.reflect.Method.invoke (Method.java:578)
at org.apache.maven.wrapper.BootstrapMainStarter.start (BootstrapMainStarter.java:47)
at org.apache.maven.wrapper.WrapperExecutor.execute (WrapperExecutor.java:156)
at org.apache.maven.wrapper.MavenWrapperMain.main (MavenWrapperMain.java:72)
[ERROR]
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

I do not get what exactly the issue with the interface is. When removing the interface, it works again.

Thanks and best regards,
Matthias

/attempts endpoint does not work after introduction of Repositories

Hi Moisés,

After the introduction of the Repositories for the Users and Attempts, the /attempts endpoint does not work anymore. When calling it like this:

http POST :8080/attempts factorA=58 factorB=92 userAlias=matthias guess=5303

I am getting this response:

{"timestamp"}

There is even no http-code.

I also try it via Postman using this payload:

{
    "factorA":20,
    "factorB":50,
    "userAlias":"matthias",
    "guess":1000
}

Here, I am getting the same response as before and can see that there is an internal server error but without any details.

image

I double checked against your code in Git Hub, and cannot find a problem.

Before I added the Json Configuration, I got this error message:

could not prepare statement; SQL [select u1_0.id, u1_0.alias from user u1_0 where u1_0.alias=?]

Any idea what I am doing wrong?

Thanks and best regards,
Matthias

Issue with creating the user table name

Hi-
Hibernate ran into an issue while trying to create the User table. It gave an error message that indicated that User was a reerved word. So following hibernate convention I backticked the table name User and then it worked. I was wondering if anybody else had the same issue ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.