This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>org.springframework.cloud</groupId> | |
<artifactId>spring-cloud-starter-config</artifactId> | |
</dependency> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spring.application.name=example-service | |
spring.cloud.config.uri=http://localhost:8888 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// it indicates where is the git repository that contains all your configuration files | |
spring.cloud.config.server.git.uri=/Users/gerard/Documents/workspace_gerard/app_configuration | |
server.port=8888 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>org.springframework.cloud</groupId> | |
<artifactId>spring-cloud-config-server</artifactId> | |
</dependency> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController | |
@RequestMapping("/{tenantId}/invoice") | |
public class InvoiceController { | |
@Autowired | |
private InvoiceRepository invoiceRepository; | |
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) | |
public List<Invoice> invoices() { | |
return Lists.newArrayList(invoiceRepository.findAll()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@EnableConfigurationProperties(MultitenancyConfigurationProperties.class) | |
public class MultitenancyConfiguration { | |
@Autowired | |
private MultitenancyConfigurationProperties multitenancyProperties; | |
@Bean(name = "multitenantProvider") | |
public DataSourceBasedMultiTenantConnectionProviderImpl dataSourceBasedMultiTenantConnectionProvider() { | |
HashMap<String, DataSource> dataSources = new HashMap<String, DataSource>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DataSourceBasedMultiTenantConnectionProviderImpl extends AbstractDataSourceBasedMultiTenantConnectionProviderImpl { | |
private static final long serialVersionUID = 1L; | |
private String defaultTenant; | |
private Map<String, DataSource> map; | |
public DataSourceBasedMultiTenantConnectionProviderImpl(String defaultTenant, Map<String, DataSource> map) { | |
super(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RequestContextHolderUtils { | |
private static final String DEFAULT_TENANT_ID = "tenant_1"; | |
public static final String getCurrentTenantIdentifier() { | |
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); | |
if (requestAttributes != null) { | |
String identifier = (String) requestAttributes.getAttribute(CustomRequestAttributes.CURRENT_TENANT_IDENTIFIER,RequestAttributes.SCOPE_REQUEST); | |
if (identifier != null) { | |
return identifier; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
public class CurrentTenantIdentifierResolverImpl implements CurrentTenantIdentifierResolver { | |
@Override | |
public String resolveCurrentTenantIdentifier() { | |
return RequestContextHolderUtils.getCurrentTenantIdentifier(); | |
} | |
@Override | |
public boolean validateExistingCurrentSessions() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// we have to exclude the default configuration, because we want to provide | |
// our multi tenant data source configuration. | |
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) | |
public class App { | |
public static void main(String[] args) { | |
SpringApplication.run(App.class, args); | |
} | |
} |