在我的上一篇文章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相关推荐

  1. Spring Cloud(一)服务的注册与发现(Eureka)

    Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布式会话和集 ...

  2. Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】

    Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全 ...

  3. Spring Cloud构建微服务架构(一)服务注册与发现

    Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁 ...

  4. java spring cloud版b2b2c社交电商spring cloud分布式微服务:服务注册与发现(Eureka、Consul)...

    Spring Cloud简介 电子商务社交平台源码请加企鹅求求:一零三八七七四六二六.Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配 ...

  5. spring cloud 实战项目搭建

    spring cloud 实战项目搭建 Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断 ...

  6. Spring Cloud微服务系统架构的一些简单介绍和使用

    Spring Cloud 目录 特征 云原生应用程序 Spring Cloud上下文:应用程序上下文服务 引导应用程序上下文 应用程序上下文层次结构 改变Bootstrap的位置Properties ...

  7. Spring Cloud Dalston.RELEASE中文文档

    Spring Cloud Dalston.RELEASE中文文档 Spring Cloud 目录 特性 云原生应用程序 Spring Cloud上下文:应用程序上下文服务 引导应用程序上下文 应用程序 ...

  8. Spring Cloud 中文文档

    Spring Cloud 官方文档 Spring Cloud为开发人员提供了用于快速构建分布式系统中某些常见模式的工具(例如,配置管理,服务发现,断路器,智能路由,微代理,控制总线).分布式系统的协调 ...

  9. 消息总线(Spring Cloud Bus)

    spring CloudBus 将分布式的节点和轻量的消息代理连接起来.这可以用于广播配置文件的更改或者其他的管理工作.一个关键的思想就是,消息总线可以为微服务做监控,也可以作为应用程序之间相互通讯. ...

最新文章

  1. [UWP]实现一个轻量级的应用内消息通知控件
  2. php中年月日用什么参数,PHP中date()日期函数参数整理
  3. 用C语言解“打印九九乘法表”题
  4. 三种ajax解析模式!
  5. oracle expdp数据到txt,[20130727]ORACLE 12C使用expdp导出view数据.txt
  6. oracle 数据库中执行数据库语句能找到数据,但是程序中却抓取不到
  7. 数码管显示1到8c语言,单片机控制八只数码管滚动显示1~8 附PROTEUS软件仿真图
  8. Hashtable、HashMap、TreeMap总结
  9. 马斯克身价增至392亿美元,但仍低于马化腾马云身价
  10. DPM2012系列之十七:如何将备份文件恢复到网络共享文件夹
  11. MySQL的InnoDB表如何设计主键索引-转自淘宝MySQL经典案例
  12. HDU 6274 Master of Sequence (暴力+下整除)
  13. 机器学习十大经典算法——knn
  14. HUT-XXXX The window of the dazzling 模拟
  15. IMPROVING ADVERSARIAL ROBUSTNESS REQUIRES REVISITING MISCLASSIFIED EXAMPLES
  16. matlab单片机仿真,51单片机proteus仿真实验设计
  17. 离散型特征编码方式:one-hot与哑变量
  18. android 监听home back,Android中监听Home键的4种方法总结
  19. Linux系统信号定义
  20. 布丁浅谈之Linux常用基本命令

热门文章

  1. 使用ueditor实现多图片上传案例——实体类(Shopping.java)
  2. FastDFS(分布式文件系统)
  3. 第4步 tomcat配置中文字符集 启动Tomcat  网页乱码
  4. java 刷新界面_利用java如何实现在删除信息后刷新页面功能
  5. neo4j cypher_Neo4j:Cypher – Neo.ClientError.Statement.TypeError:不知道如何添加Double和String...
  6. java登录界面命令_Java命令行界面(第25部分):JCommando
  7. 弹性架构_实践中的弹性基础架构
  8. spring总结_Spring综合课程总结
  9. jboss eap 7.0_是时候抛弃Java 7 – JBoss EAP 6.4了!
  10. svn: 没有演进历程信息_使用默认方法的接口演进–第二部分:接口