updating README.md and adding docker-compose.yml
This commit is contained in:
19
README.md
19
README.md
@@ -15,6 +15,12 @@ Or Select the following share link:
|
||||
Spring Initializr
|
||||
Initializr generates spring boot project with just what you need to start quickly!start.spring.io
|
||||
|
||||
for simplicity just run:
|
||||
```docker-compose up --force-recreate```
|
||||
|
||||
the docker-compose.yml is already in the project which contains 2 PostgresSql in 2 different ports, with ```demo``` DataBase
|
||||
|
||||
Note: you can always uninstall it as: ```docker-compose down``` if you needed to.
|
||||
|
||||
Once you Generate and download the zip file, you should have similar POM file as:
|
||||
```xml
|
||||
@@ -219,4 +225,17 @@ public class CustomerServiceImpl implements CustomerService {
|
||||
}
|
||||
}
|
||||
```
|
||||
Now if run this line you create customer in DB1:
|
||||
```
|
||||
curl -H "Content-Type: application/json" --request POST --data '{"name":"Jay"}' http://localhost:8080/customer
|
||||
```
|
||||
OR
|
||||
```
|
||||
curl -H "Content-Type: application/json" --request PUT --data '{"id":1 , "name":"Jay ehsaniara"}' http://localhost:8080/customer
|
||||
```
|
||||
|
||||
But if you run this line you getting data from DB2:
|
||||
```
|
||||
curl --request GET http://localhost:8080/customer/1
|
||||
```
|
||||
Note: you need to insert customer manually in DB2 since it has no pre customer. and we haven't setup Postgres Replication yet
|
||||
24
docker-compose.yml
Normal file
24
docker-compose.yml
Normal file
@@ -0,0 +1,24 @@
|
||||
version: '3.1'
|
||||
services:
|
||||
db1:
|
||||
image: postgres
|
||||
container_name: postgres1
|
||||
volumes:
|
||||
- ./postgres-data1:/var/lib/postgresql/data
|
||||
ports:
|
||||
- "5432:5432"
|
||||
environment:
|
||||
POSTGRES_PASSWORD: postgres_user_for_db_write
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_DB: demo
|
||||
db2:
|
||||
image: postgres
|
||||
container_name: postgres2
|
||||
volumes:
|
||||
- ./postgres-data2:/var/lib/postgresql/data
|
||||
ports:
|
||||
- "5433:5432"
|
||||
environment:
|
||||
POSTGRES_PASSWORD: postgres_user_for_db_read
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_DB: demo
|
||||
2
pom.xml
2
pom.xml
@@ -11,7 +11,7 @@
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>demo</name>
|
||||
<name>Spring Boot Multiple DataSource</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<properties>
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.ehsaniara.multidatasource.repository;
|
||||
|
||||
import com.ehsaniara.multidatasource.model.Customer;
|
||||
import com.ehsaniara.multidatasource.repository.readRepository.CustomerReadRepository;
|
||||
import com.ehsaniara.multidatasource.repository.writeRepository.CustomerWriteRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class CustomerRepository implements CustomerRepositoryCombo {
|
||||
|
||||
private final CustomerReadRepository readRepository;
|
||||
private final CustomerWriteRepository writeRepository;
|
||||
|
||||
public CustomerRepository(CustomerReadRepository customerReadRepository, CustomerWriteRepository customerWriteRepository) {
|
||||
this.readRepository = customerReadRepository;
|
||||
this.writeRepository = customerWriteRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends Customer> S save(S s) {
|
||||
return writeRepository.save(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends Customer> Iterable<S> saveAll(Iterable<S> iterable) {
|
||||
return writeRepository.saveAll(iterable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Customer> findById(Long aLong) {
|
||||
return readRepository.findById(aLong);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean existsById(Long aLong) {
|
||||
return readRepository.existsById(aLong);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Customer> findAll() {
|
||||
return readRepository.findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Customer> findAllById(Iterable<Long> iterable) {
|
||||
return readRepository.findAllById(iterable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long count() {
|
||||
return readRepository.count();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(Long aLong) {
|
||||
writeRepository.deleteById(aLong);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Customer customer) {
|
||||
writeRepository.delete(customer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAll(Iterable<? extends Customer> iterable) {
|
||||
writeRepository.deleteAll(iterable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAll() {
|
||||
writeRepository.deleteAll();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.ehsaniara.multidatasource.repository;
|
||||
|
||||
import com.ehsaniara.multidatasource.repository.readRepository.CustomerReadRepository;
|
||||
import com.ehsaniara.multidatasource.repository.writeRepository.CustomerWriteRepository;
|
||||
|
||||
public interface CustomerRepositoryCombo extends CustomerReadRepository, CustomerWriteRepository {
|
||||
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
package com.ehsaniara.multidatasource.service;
|
||||
|
||||
import com.ehsaniara.multidatasource.model.Customer;
|
||||
import com.ehsaniara.multidatasource.repository.readRepository.CustomerReadRepository;
|
||||
import com.ehsaniara.multidatasource.repository.writeRepository.CustomerWriteRepository;
|
||||
import com.ehsaniara.multidatasource.repository.CustomerRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -14,16 +13,14 @@ import java.util.Optional;
|
||||
@Service
|
||||
public class CustomerServiceImpl implements CustomerService {
|
||||
|
||||
private final CustomerReadRepository customerReadRepository;
|
||||
private final CustomerWriteRepository customerWriteRepository;
|
||||
private final CustomerRepository customerRepository;
|
||||
|
||||
public CustomerServiceImpl(CustomerReadRepository customerReadRepository, CustomerWriteRepository customerWriteRepository) {
|
||||
this.customerReadRepository = customerReadRepository;
|
||||
this.customerWriteRepository = customerWriteRepository;
|
||||
public CustomerServiceImpl(CustomerRepository customerRepository) {
|
||||
this.customerRepository = customerRepository;
|
||||
}
|
||||
|
||||
public Optional<Customer> getCustomer(Long id) {
|
||||
return customerReadRepository.findById(id);
|
||||
return customerRepository.findById(id);
|
||||
}
|
||||
|
||||
public Customer createCustomer(Customer customer) {
|
||||
@@ -32,7 +29,7 @@ public class CustomerServiceImpl implements CustomerService {
|
||||
Assert.isNull(customer.getId(), "customer id should be null");
|
||||
Assert.notNull(customer.getName(), "Invalid customer name");
|
||||
|
||||
return customerWriteRepository.save(customer);
|
||||
return customerRepository.save(customer);
|
||||
}
|
||||
|
||||
public Customer updateCustomer(Customer customer) {
|
||||
@@ -40,6 +37,6 @@ public class CustomerServiceImpl implements CustomerService {
|
||||
Assert.notNull(customer, "Invalid customer");
|
||||
Assert.notNull(customer.getId(), "Invalid customer id");
|
||||
|
||||
return customerWriteRepository.save(customer);
|
||||
return customerRepository.save(customer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,3 +23,4 @@ spring:
|
||||
minimum-idle: 5
|
||||
pool-name: ReadHikariPool
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user