{"id":134,"date":"2019-12-05T11:55:45","date_gmt":"2019-12-05T11:55:45","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/microservices\/?p=134"},"modified":"2019-12-09T21:49:31","modified_gmt":"2019-12-09T21:49:31","slug":"multiple-database-configuration-for-microservice-in-spring-boot","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/microservices\/multiple-database-configuration-for-microservice-in-spring-boot\/","title":{"rendered":"Multiple Database Configuration for Microservice in Spring Boot"},"content":{"rendered":"<p>Previously in this Tutorial, we learnt <a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/microservices-with-spring-profilesstep-by-step-tutorial\/\">how to split a single structured monolith into microservices. We did this using Spring Profiles.<\/a><\/p>\n<p>In this lesson, we would learn how to configure multiple databases for a structured monolithic application. This is very important if you are testing the performance of different database scenarios for database per service (DBPS) microservice architecture.<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li><a href=\"#t1\">Prerequisites<\/a><\/li>\n<li><a href=\"#t2\">Insert and Query Some Test Data<\/a><\/li>\n<li><a href=\"#t2\">How it Works<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t1\">1. Prerequisites<\/strong><\/h5>\n<ul>\n<li>MySQL Database<\/li>\n<li>PostgreSQL (Optional)<\/li>\n<li>MS SQL Database (Optional)<\/li>\n<\/ul>\n<p>In this demo, we would only use two datasources: MySQL and PostgreSQL. We do this for simplicity and clarity.<\/p>\n<p>You can also use two MySQL databases just to follow this demo.<\/p>\n<p><strong>Before you start:<\/strong> Create a database in MySQL called admissionsdb and another database in PostgreSQL call appointmentsdb.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t2\">2. How It Works<\/strong><\/h5>\n<p>It would be similar to your normal application but with a few changes<\/p>\n<ol>\n<li>The application.properties file would now contain multiple setting each for a different datasource<\/li>\n<li>You need configuration classes, one for each of the datasources. One of the configuration classed must be set as Primary using the @Primary annotation.<\/li>\n<li>The configuration classes would contain mandatory methods:\n<ul>\n<li>DataSource method<\/li>\n<li>LocalContainerEntityManagerFactoryBean method<\/li>\n<li>PlatformTransactionManager method<\/li>\n<\/ul>\n<\/li>\n<li>\u00a0Of course, you need a model class and a repository interface.<\/li>\n<\/ol>\n<p>So, let&#8217;s give it a shot!<\/p>\n<p>We would be using IntelliJ in this demo. However, the complete project can be found <a href=\"https:\/\/github.com\/KindsonTheGenius\/MultipleDBConfig\">here in my GitHub repository<\/a>. Feel free to clone or download it<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 1<\/strong>: Create a spring application and create two packages admissions and appointments<\/p>\n<p><strong>Step 2<\/strong>: Create a the Appointment class in the appointment package. The Appointment Class is shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Entity<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@Data<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@AllArgsConstructor<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@NoArgsConstructor<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@Table<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">\"appointment\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Appointment<\/span>\r\n<span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #555555; font-weight: bold;\">@Id<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> id<span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> String status<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 3<\/strong>: Create the Admission Class in the admission package. The admissions class is shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Entity<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@Data<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@AllArgsConstructor<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@NoArgsConstructor<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@Table<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">\"admission\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Admission<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #555555; font-weight: bold;\">@Id<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> id<span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> String description<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4:<\/strong> Create the AppointmentRepository interface in the appointments package<\/p>\n<p><strong>Step 5<\/strong>: Create the AdmissionRepository interface in the admissions package<\/p>\n<p><strong>Step 6<\/strong>: Open the application.properties file and add the following settings for MySQL and PostgreSQL<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">#Appointments Datasource\r\nspring.appointments.datasource.jdbcUrl=jdbc:postgresql:\/\/localhost:5432\/appointmentsdb\r\nspring.appointments.datasource.username=postgres\r\nspring.appointments.datasource.password=root\r\n\r\n#Admissions Datasource\r\nspring.admissions.datasource.jdbcUrl=jdbc:mysql:\/\/localhost:3301\/admissionsdb?serverTimezone=UTC\r\nspring.admissions.datasource.username=root\r\nspring.admissions.datasource.password=root\r\nspring.admissions.datasource.driver-class-name=com.mysql.cj.jdbc.Driver\r\n\r\nspring.jpa.generate-ddl=true\r\nspring.jpa.show-sql=true\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>You can see the setting for the two datasources. Take note that the datasources are differentiated based on the prefixes: spring.admissions and spring.appointments. Now, if you want to add more data sources, do feel free.<\/p>\n<p>The last two statements applies to both datasources.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 7:<\/strong> Now create the AdmissionsDBConfig class inside the admissions package. Annotate this class with annotations as shown below:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Configuration<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@EnableTransactionManagement<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@EnableJpaRepositories<\/span><span style=\"color: #333333;\">(<\/span>\r\n        entityManagerFactoryRef <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"entityManagerFactory\"<\/span>\r\n<span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">AdmissionsDBConfig<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n\t<span style=\"color: #888888;\">\/\/ Datasource method goes here<\/span>\r\n\t\r\n\t<span style=\"color: #888888;\">\/\/ LocalContainerEntityManagerFactoryBean goes here<\/span>\r\n\t\r\n\t<span style=\"color: #888888;\">\/\/ PlatformTransactionManager goes here<\/span>\r\n\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 8:<\/strong> Write the function that returns the admissions datasource. This is shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Primary<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@Bean<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">\"datasource\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@ConfigurationProperties<\/span><span style=\"color: #333333;\">(<\/span>prefix <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"spring.admissions.datasource\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> DataSource <span style=\"color: #0066bb; font-weight: bold;\">dataSource<\/span><span style=\"color: #333333;\">(){<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> DataSourceBuilder<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">create<\/span><span style=\"color: #333333;\">().<\/span><span style=\"color: #0000cc;\">build<\/span><span style=\"color: #333333;\">();<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Take note that in this listing the function is annotated with the @Primary annotation<\/p>\n<p><strong>Step 9:<\/strong> Write the function that returns a LocalContainerEntityManagerFactory. Also annotated with @Primary annotation as shown below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Primary<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@Bean<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">\"entityManagerFactory\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> LocalContainerEntityManagerFactoryBean <span style=\"color: #0066bb; font-weight: bold;\">entityManagerFactoryBean<\/span><span style=\"color: #333333;\">(<\/span>\r\n        EntityManagerFactoryBuilder builder<span style=\"color: #333333;\">,<\/span>\r\n        <span style=\"color: #555555; font-weight: bold;\">@Qualifier<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"datasource\"<\/span><span style=\"color: #333333;\">)<\/span> DataSource dataSource\r\n<span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #333333;\">{<\/span>\r\n\tMap<span style=\"color: #333333;\">&lt;<\/span>String<span style=\"color: #333333;\">,<\/span> Object<span style=\"color: #333333;\">&gt;<\/span> properties <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> HashMap<span style=\"color: #333333;\">&lt;&gt;();<\/span>\r\n\tproperties<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">put<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"hibernate.hbm2ddl.auto\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"update\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\tproperties<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">put<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"hibernate.dialect\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"org.hibernate.dialect.MySQL5Dialect\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> builder\r\n            <span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">dataSource<\/span><span style=\"color: #333333;\">(<\/span>dataSource<span style=\"color: #333333;\">)<\/span>\r\n            <span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">properties<\/span><span style=\"color: #333333;\">(<\/span>properties<span style=\"color: #333333;\">)<\/span>\r\n            <span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">packages<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"com.kindsonthegenius.multipledb.admissions\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n            <span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">persistenceUnit<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Admission\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n            <span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">build<\/span><span style=\"color: #333333;\">();<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p><strong>Step 10:<\/strong> Write the function that returns the transactionManager object<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Primary<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@Bean<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">\"transactionManager\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> PlatformTransactionManager <span style=\"color: #0066bb; font-weight: bold;\">transactionManager<\/span><span style=\"color: #333333;\">(<\/span>\r\n        <span style=\"color: #555555; font-weight: bold;\">@Qualifier<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"entityManagerFactory\"<\/span><span style=\"color: #333333;\">)<\/span> EntityManagerFactory entityManagerFactory\r\n<span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">JpaTransactionManager<\/span><span style=\"color: #333333;\">(<\/span>entityManagerFactory<span style=\"color: #333333;\">);<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 11<\/strong>: Repeat steps 7 to 10 but this time for the\u00a0 appointment package.<\/p>\n<p>Also remember: Remove the @Primary annotation for the methods in the AppointmentsDBConfig.<\/p>\n<p>The complete content of the AppointmentsDBConfig is given below:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Configuration<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@EnableTransactionManagement<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@EnableJpaRepositories<\/span><span style=\"color: #333333;\">(<\/span>\r\n        entityManagerFactoryRef <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"appointmentsEntityManagerFactory\"<\/span><span style=\"color: #333333;\">,<\/span>\r\n        transactionManagerRef <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"appointmentsTransactionManager\"<\/span>\r\n<span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">AppointmentsDBConfig<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\r\n\t<span style=\"color: #555555; font-weight: bold;\">@Bean<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">\"appointmentsDatasource\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n\t<span style=\"color: #555555; font-weight: bold;\">@ConfigurationProperties<\/span><span style=\"color: #333333;\">(<\/span>prefix <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"spring.appointments.datasource\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> DataSource <span style=\"color: #0066bb; font-weight: bold;\">dataSource<\/span><span style=\"color: #333333;\">(){<\/span>\r\n\t    <span style=\"color: #008800; font-weight: bold;\">return<\/span> DataSourceBuilder<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">create<\/span><span style=\"color: #333333;\">().<\/span><span style=\"color: #0000cc;\">build<\/span><span style=\"color: #333333;\">();<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n\t\t\r\n\t<span style=\"color: #555555; font-weight: bold;\">@Bean<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">\"appointmentsEntityManagerFactory\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> LocalContainerEntityManagerFactoryBean <span style=\"color: #0066bb; font-weight: bold;\">entityManagerFactoryBean<\/span><span style=\"color: #333333;\">(<\/span>\r\n\t        EntityManagerFactoryBuilder builder<span style=\"color: #333333;\">,<\/span>\r\n\t        <span style=\"color: #555555; font-weight: bold;\">@Qualifier<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"appointmentsDatasource\"<\/span><span style=\"color: #333333;\">)<\/span> DataSource dataSource\r\n\t<span style=\"color: #333333;\">)<\/span>\r\n\t<span style=\"color: #333333;\">{<\/span>\r\n\t    <span style=\"color: #008800; font-weight: bold;\">return<\/span> builder\r\n\t            <span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">dataSource<\/span><span style=\"color: #333333;\">(<\/span>dataSource<span style=\"color: #333333;\">)<\/span>\r\n\t            <span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">packages<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"com.kindsonthegenius.multipledb.appointments\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n\t            <span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">persistenceUnit<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Appointments\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n\t            <span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">build<\/span><span style=\"color: #333333;\">();<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\t\r\n\t\r\n\t<span style=\"color: #555555; font-weight: bold;\">@Bean<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">\"appointmentsTransactionManager\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> PlatformTransactionManager <span style=\"color: #0066bb; font-weight: bold;\">transactionManager<\/span><span style=\"color: #333333;\">(<\/span>\r\n\t        <span style=\"color: #555555; font-weight: bold;\">@Qualifier<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"appointmentsEntityManagerFactory\"<\/span><span style=\"color: #333333;\">)<\/span> \r\n\t        EntityManagerFactory entityManagerFactory\r\n\t<span style=\"color: #333333;\">)<\/span>\r\n\t<span style=\"color: #333333;\">{<\/span>\r\n\t    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">JpaTransactionManager<\/span><span style=\"color: #333333;\">(<\/span>entityManagerFactory<span style=\"color: #333333;\">);<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t3\">3. Insert and Query Some Test Data<\/strong><\/h5>\n<p>Follow the steps below.<\/p>\n<p><strong>Step 1<\/strong>: Open the main application class. Annotate this class with the @RestController annotation (we are going to add REST endpoints here) . This is done for simplicity as you could also have create separate controller classes.<\/p>\n<p><strong>Step 2:<\/strong> Autowire both the AppointmentRepository and the AdmissionRepository into this class. The code is shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Autowired<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">private<\/span> AdmissionRepository admissionRepository<span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #555555; font-weight: bold;\">@Autowired<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">private<\/span> AppointmentRepository appointmentRepository<span style=\"color: #333333;\">;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 3<\/strong>: Add a method annotated with @PostConstruct to add some appointments data into the appointments database<\/p>\n<p><strong>Step 4<\/strong>: Add a method annotated with @GetMapping(&#8220;\/getAppointments&#8221;) to retrieve list of appointments. Both methods are shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/******************************************************************************<\/span>\r\n<span style=\"color: #888888;\"> * ENDPOINTS FOR APPOINTMENTS SERVICE<\/span>\r\n<span style=\"color: #888888;\"> ******************************************************************************\/<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@PostConstruct<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">addAppointmentsData<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\tappointmentRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">saveAll<\/span><span style=\"color: #333333;\">(<\/span>Stream<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">of<\/span><span style=\"color: #333333;\">(<\/span>\r\n\t\t\t<span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Appointment<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">101<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Teh first appointment ever\"<\/span><span style=\"color: #333333;\">),<\/span>\r\n\t\t\t<span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Appointment<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">102<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Appointment with a Dentist\"<\/span><span style=\"color: #333333;\">))<\/span>\r\n\t\t\t<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">collect<\/span><span style=\"color: #333333;\">(<\/span>Collectors<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">toList<\/span><span style=\"color: #333333;\">()));<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@GetMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/getAppointments\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> List<span style=\"color: #333333;\">&lt;<\/span>Appointment<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getAppointmens<\/span><span style=\"color: #333333;\">(){<\/span>\r\n\r\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> appointmentRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findAll<\/span><span style=\"color: #333333;\">();<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p><strong>Note<\/strong>: You could also write a simpler code by creating and inserting appointments one by one. You could also use a data.sql file placed inside src\/main\/resources folder.<\/p>\n<p><strong>Step 5:<\/strong> Repeat step 3 and step 4 for the\u00a0 admissions database.<\/p>\n<p>At this point, we are good to go!<\/p>\n<p><strong>Step 7:<\/strong> Run the application and visit http:\/\/localhost:8080\/getAdmissions and do same for Appointment.<\/p>\n<p>&nbsp;<\/p>\n<p>If everything went successfully, then thumbs up to you! Else, clone the repository and check where you got it wrong.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previously in this Tutorial, we learnt how to split a single structured monolith into microservices. We did this using Spring Profiles. In this lesson, we &hellip; <\/p>\n","protected":false},"author":1,"featured_media":136,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[48,46,45,44,47],"class_list":["post-134","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microservices","tag-autowired","tag-entitymanager","tag-mysql","tag-postgresql","tag-transactionmanager"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/comments?post=134"}],"version-history":[{"count":6,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/134\/revisions"}],"predecessor-version":[{"id":142,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/134\/revisions\/142"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/media\/136"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/media?parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/categories?post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/tags?post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}