javascript
具有代理设置的Spring Cloud AWS
在我的上一篇文章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
總結
以上是生活随笔為你收集整理的具有代理设置的Spring Cloud AWS的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 红魔7pro日常使用怎么样(红魔7pro
- 下一篇: 在Apache Camel中使用Spri