https://docs.microsoft.com/en-us/java/azure/spring-framework/configure-spring-cloud-stream-binder-java-app-azure-event-hub?view=azure-java-stable
https://github.com/microsoft/spring-cloud-azure/tree/master/spring-cloud-azure-samples/eventhubs-operation-sample
以上链接为SpringCloud集成eventhub的文档和代码,由于默认是针对Azure Globle的Eventhub服务,如果要使用Azure China的相关资源,需要稍作修改
- maven依赖
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-spring-boot-starter</artifactId>
<version>${azure.version}</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>spring-cloud-starter-azure-eventhub</artifactId>
<version>1.1.0.RC1</version>
</dependency>
- application.properties配置
azure.storage.connection-string=xxx
# Fill event hub namespace connection string copied from portal
spring.cloud.azure.eventhub.connection-string=xxx
spring.cloud.azure.eventhub.checkpoint-access-key=xxx
spring.cloud.azure.eventhub.checkpoint-storage-account=xxx
由于默认是使用国际版的资源,官方文档建议通过配置application.properties的spring.cloud.azure.environment=AZURE_CHINA
来改变环境变量,但是通过实践,该配置参数并没有生效;通过查看源码,发现可以通过增加以下的Bean来改变默认运行环境,并通过实践验证该方案可行
@Configuration
public class ChinaEnvironmentProvider implements EnvironmentProvider {
private Environment environment = Environment.CHINA;
@Override
public Environment getEnvironment() {
return this.environment;
}
}