在我的上一篇文章Spring和Amazon Web Services中 ,我简要介绍了Spring Cloud AWS模块以及开发人员现在对它的期望。 从官方文档中看不出来的一件事是,当您的Internet连接受到代理服务器的限制时,如何使用此模块。 在本文中,我将概述如何为基于Java和基于XML的配置传递代理配置。 在将来的发行版中可能会解决此配置方面的问题,但是如果您使用此模块并需要您的应用程序与公司代理一起工作,它现在可能会对您有所帮助。
Spring Cloud AWS代理配置
Java配置
让我们从更流行的配置Spring应用程序的方式开始-Java配置。 在这种情况下,事情相对简单,因为您可以自己在代码中手动提供所需的代理配置。 考虑以下声明两个bean的配置类-S3客户端和代理配置(如果未从属性文件/属性解析这些设置,则将使用默认的无代理连接)。
来自ApplicationConfiguration类的S3客户端配置示例
@Configuration
@EnableContextInstanceData
public final class ApplicationConfiguration {@Value("${proxy.host}")private String proxyHost;@Value("${proxy.port}")private int proxyPort;@Value("${proxy.username}")private String proxyUsername;@Value("${proxy.password}")private String proxyPassword;@Beanpublic AmazonS3Client amazonS3Client() {return new AmazonS3Client(clientConfiguration());}@Beanpublic ClientConfiguration clientConfiguration() {final ClientConfiguration clientConfiguration = new ClientConfiguration();clientConfiguration.setProxyHost(proxyHost);clientConfiguration.setProxyPort(proxyPort);clientConfiguration.setProxyUsername(proxyUsername);clientConfiguration.setProxyPassword(proxyPassword);return clientConfiguration;}
}
考虑到此类代码的含义,请考虑使用用于在开发人员计算机上运行应用程序的配置文件标记此类,例如@Profile("local")
。
XML配置
在使用XML配置进行代理配置时,需要一定程度的Spring配置知识。 为了使此简单的配置AmazonS3Client
,我们需要使用存储在客户端配置Bean中的代理设置创建AmazonS3Client
实例。 以下XML文件显示了整个配置,因此让我们将其分为几个部分。
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aws-context="http://www.springframework.org/schema/cloud/aws/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/cloud/aws/context http://www.springframework.org/schema/cloud/aws/context/spring-cloud-aws-context.xsd"><context:component-scan base-package="com.jakubstas.s3downloader"/><!-- Bunch of simple configuration switches allowing access to instance metadata, integration of S3into ResourceLoader and region auto detection. Some of these are not essential for the examplehowever it is always nice to have the information they provide at hand when needed. --><aws-context:context-instance-data/><aws-context:context-resource-loader/><aws-context:context-region auto-detect="true"/><!-- Configuration of Amazons credentials provider chain to allow execution on developers machineas well as in the Beanstalk environment. --><aws-context:context-credentials><aws-context:instance-profile-credentials/><aws-context:simple-credentials access-key="#{systemProperties['AWS_ACCESS_KEY_ID']}" key="#{systemProperties['AWS_SECRET_KEY']}"/></aws-context:context-credentials><!-- Bean with client configuration with passed proxy settings (if these settings are not resolvedfrom property files / properties default no-proxy connection will be used) --><!-- The client instance created by hand with proxy configuration --><bean id="amazonS3" class="com.amazonaws.services.s3.AmazonS3Client" autowire-candidate="true" autowire="constructor"/><!-- Proxy configuration for any AWS related client code - currently used for S3 (but might as well be used for DynamoDB access, ...) --><bean id="clientConfiguration" class="com.amazonaws.ClientConfiguration"><property name="proxyHost" value="${proxy.host}"/><property name="proxyPort" value="${proxy.port}"/><property name="proxyUsername" value="${proxy.username}"/><property name="proxyPassword" value="${proxy.password}"/></bean>
</beans>
考虑到此类代码的含义,请考虑使用用于在开发人员机器上运行应用程序的配置文件标记这些bean,例如profile="local"
。
超越S3
到目前为止,该示例几乎仅限于S3。 但是,由于Amazon SDK的设计方式,可以在任何适用的情况下使用此配置。 让我们看一下DynomoDB客户端的示例。 各种Amazon AWS服务的多个客户端可以利用上述方法。
来自ApplicationConfiguration类的DynamoDB客户端配置示例
@Configuration
@EnableContextInstanceData
public final class ApplicationConfiguration {@Value("${proxy.host}")private String proxyHost;@Value("${proxy.port}")private int proxyPort;@Value("${proxy.username}")private String proxyUsername;@Value("${proxy.password}")private String proxyPassword;@Beanpublic AmazonS3 amazonS3Client() {return new AmazonS3Client(clientConfiguration());}@Beanpublic AmazonDynamoDBClient amazonDynamoDBClient() {return new AmazonDynamoDBClient(clientConfiguration());}@Beanpublic ClientConfiguration clientConfiguration() {final ClientConfiguration clientConfiguration = new ClientConfiguration();clientConfiguration.setProxyHost(proxyHost);clientConfiguration.setProxyPort(proxyPort);clientConfiguration.setProxyUsername(proxyUsername);clientConfiguration.setProxyPassword(proxyPassword);return clientConfiguration;}
}
结论
将应用程序配置传递给您的bean是非常标准的任务,不会对有经验的Spring开发人员造成很多麻烦。 但是,鉴于开发人员的经验水平和日常生活水平各不相同,这可能会引起麻烦。 这就是为什么我鼓励任何人自己尝试这些示例的原因,因为此处建模的情况使用了Spring配置设计的基本方法之一。 继续练习并保持该公司代理人身份。 :)
翻译自: https://www.javacodegeeks.com/2016/01/spring-cloud-aws-proxy-settings.html