Skip to content

Instantly share code, notes, and snippets.

@unclebean
Last active October 9, 2024 08:08
Show Gist options
  • Save unclebean/092b58ae18eacf5e84a1c504c541ab3a to your computer and use it in GitHub Desktop.
Save unclebean/092b58ae18eacf5e84a1c504c541ab3a to your computer and use it in GitHub Desktop.
Azure config
<!-- Spring Cloud Azure Starter -->
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-starter</artifactId>
</dependency>
<!-- Azure Blob Storage -->
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-starter-storage-blob</artifactId>
</dependency>
<!-- Azure Cognitive Search -->
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-starter-cognitive-search</artifactId>
</dependency>
<!-- Azure PostgreSQL (if needed) -->
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-starter-data-jdbc</artifactId>
</dependency>
spring:
cloud:
azure:
storage:
account-name: your-storage-account-name
account-key: your-storage-account-key # If not using managed identity
endpoint: https://your-storage-account-name.blob.core.windows.net
spring:
cloud:
azure:
cognitive-search:
service-name: your-cognitive-search-service-name
api-key: your-cognitive-search-api-key # If not using managed identity
endpoint: https://your-cognitive-search-service-name.search.windows.net
spring:
datasource:
url: jdbc:postgresql://your-postgres-server.postgres.database.azure.com:5432/your-database-name
username: your-database-username@your-postgres-server
password: your-database-password
driver-class-name: org.postgresql.Driver
@Bean
public BlobServiceClient blobServiceClient() {
// Build a credential using User-Assigned Managed Identity
ManagedIdentityCredential managedIdentityCredential = new ManagedIdentityCredentialBuilder()
.clientId(clientId)
.build();
// Build the BlobServiceClient using the Managed Identity Credential
return new BlobServiceClientBuilder()
.endpoint(String.format("https://%s.blob.core.windows.net", accountName))
.credential(managedIdentityCredential)
.buildClient();
}
@Bean
public SearchClient searchClient() {
// Create a credential using the User-Assigned Managed Identity
ManagedIdentityCredential managedIdentityCredential = new ManagedIdentityCredentialBuilder()
.clientId(clientId)
.build();
// Build the SearchClient using Managed Identity Credential
return new SearchClientBuilder()
.credential(managedIdentityCredential)
.endpoint(searchEndpoint)
.indexName(indexName)
.buildClient();
}
spring:
cloud:
azure:
identity:
client-id: your-managed-identity-client-id # Only needed for User-Assigned Managed Identity
<!-- Spring Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- JDBI for SQL queries -->
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-core</artifactId>
</dependency>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-sqlobject</artifactId>
</dependency>
<!-- PostgreSQL driver -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
spring:
datasource:
url: jdbc:postgresql://your-postgres-server.postgres.database.azure.com:5432/your-database-name
username: your-database-username@your-postgres-server
password: your-database-password
driver-class-name: org.postgresql.Driver
hikari:
maximum-pool-size: 10
jpa:
hibernate:
ddl-auto: update # Can be 'none', 'update', 'validate', etc.
show-sql: true
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
import org.jdbi.v3.core.Jdbi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class JdbiConfig {
@Bean
public Jdbi jdbi(DataSource dataSource) {
return Jdbi.create(dataSource);
}
}
@unclebean
Copy link
Author

java -jar app.jar --spring.profiles.active=dev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment