在AWS上使用ElastiCache和ElasticBeanstalk配置Redis

时间:2015-07-27 21:17:20

标签: tomcat amazon-web-services redis elastic-beanstalk

我在Elastic Beanstalk上运行了Tomcat + Redis应用程序。在我的代码中,我使用

创建了一个Redis客户端
pool = new JedisPool(new JedisPoolConfig(), "localhost");

当然,在制作中,我不能使用“localhost”我已阅读Customizing the Beanstalk Environment以及关于使用代码进行自动发现,但这似乎只适用于Memcached。

是否有人使用Beanstalk配置了Redis ElastiCache群集。你在哪里获得使用的DNS地址而不是“localhost”?

1 个答案:

答案 0 :(得分:3)

我担心您无法轻松获取AWS ElastiCache端点(例如:环境变量)。

Deploying an Express Application with Clustering to Elastic Beanstalk上的AWS文档中,我们可以按照NodeJS + ElastiCache Memcache解决方案进行操作。在根项目中创建以下两个.ebextensions(阅读更多here)脚本:

<强> .ebextenstions / elasticache-IAM与 - script.config

## 
# This sample adds an ElastiCache cluster to the environment.
#  It creates an IAM user with the permisisons required to discover the elasticache nodes.  
#  It provides a cfn-hup responder if the cache changes to rewrite the file
#  It writes a file out to:  /var/www/html/nodelist
#   containing the cache nodes on startup and when the cache changes (through a cfn/eb update)
#
# Customers would generally not edit this file.
# Instead, they would have another file sitting in the same directory (or anywhere) with 
#  an option-settings section such as the following (all of these are showing the default value)
#
# option-settings:
#  "aws:elasticbeanstalk:customoption" :
#     CacheNodeType : cache.m1.small
#     NumCacheNodes : 1
#     Engine : memcached
#     NodeListPath : /var/www/html/nodelist
#
# Issues:
#   Requires ElastiCache CLI latest URL to point to version 1.7
##


Resources:
  MyElastiCache:
    Type: AWS::ElastiCache::CacheCluster
    Properties:
      CacheNodeType: 
         Fn::GetOptionSetting:
             OptionName : CacheNodeType
             DefaultValue: cache.m1.small
      NumCacheNodes: 
           Fn::GetOptionSetting:
             OptionName : NumCacheNodes
             DefaultValue: 1
      Engine: 
           Fn::GetOptionSetting:
             OptionName : Engine
             DefaultValue: memcached
      CacheSecurityGroupNames:
        - Ref: MyCacheSecurityGroup
  MyCacheSecurityGroup:
    Type: AWS::ElastiCache::SecurityGroup
    Properties:
      Description: "Lock cache down to webserver access only"
  MyCacheSecurityGroupIngress:
    Type: AWS::ElastiCache::SecurityGroupIngress
    Properties:
      CacheSecurityGroupName: 
        Ref: MyCacheSecurityGroup
      EC2SecurityGroupName:
        Ref: AWSEBSecurityGroup
  AWSEBAutoScalingGroup :
    Metadata :
      ElastiCacheConfig :
        CacheName :
          Ref : MyElastiCache
        CacheSize :
           Fn::GetOptionSetting:
             OptionName : NumCacheNodes
             DefaultValue: 1
  WebServerUser : 
    Type : AWS::IAM::User
    Properties :
      Path : "/"
      Policies:
        -
          PolicyName: root
          PolicyDocument :
            Statement :
              -
                Effect : Allow
                Action : 
                  - cloudformation:DescribeStackResource
                  - cloudformation:ListStackResources
                  - elasticache:DescribeCacheClusters
                Resource : "*"
  WebServerKeys :
    Type : AWS::IAM::AccessKey
    Properties :
      UserName :
        Ref: WebServerUser

Outputs:
  WebsiteURL:
    Description: sample output only here to show inline string function parsing
    Value: |
      http://`{ "Fn::GetAtt" : [ "AWSEBLoadBalancer", "DNSName" ] }`
  MyElastiCacheName:
    Description: Name of the elasticache
    Value:
      Ref : MyElastiCache
  NumCacheNodes:
    Description: Number of cache nodes in MyElastiCache
    Value:
      Fn::GetOptionSetting:
        OptionName : NumCacheNodes
        DefaultValue: 1

files:
  "/etc/cfn/cfn-credentials" :
    content : |
      AWSAccessKeyId=`{ "Ref" : "WebServerKeys" }`
      AWSSecretKey=`{ "Fn::GetAtt" : ["WebServerKeys", "SecretAccessKey"] }`
    mode : "000400"
    owner : root
    group : root

  "/etc/cfn/get-cache-nodes" :
    content : |
      # Define environment variables for command line tools
      export AWS_ELASTICACHE_HOME="/home/ec2-user/elasticache/$(ls /home/ec2-user/elasticache/)"
      export AWS_CLOUDFORMATION_HOME=/opt/aws/apitools/cfn
      export PATH=$AWS_CLOUDFORMATION_HOME/bin:$AWS_ELASTICACHE_HOME/bin:$PATH
      export AWS_CREDENTIAL_FILE=/etc/cfn/cfn-credentials
      export JAVA_HOME=/usr/lib/jvm/jre

      # Grab the Cache node names and configure the PHP page
      cfn-list-stack-resources `{ "Ref" : "AWS::StackName" }` --region `{ "Ref" : "AWS::Region" }` | grep MyElastiCache | awk '{print $3}' | xargs -I {} elasticache-describe-cache-clusters {} --region `{ "Ref" : "AWS::Region" }` --show-cache-node-info | grep CACHENODE | awk '{print $4 ":" $5}' > `{ "Fn::GetOptionSetting" : { "OptionName" : "NodeListPath", "DefaultValue" : "/var/www/html/nodelist" } }`
    mode : "000500"
    owner : root
    group : root

  "/etc/cfn/hooks.d/cfn-cache-change.conf" :
    "content": |
      [cfn-cache-size-change]
      triggers=post.update
      path=Resources.AWSEBAutoScalingGroup.Metadata.ElastiCacheConfig
      action=/etc/cfn/get-cache-nodes
      runas=root

sources :
  "/home/ec2-user/elasticache" : "https://s3.amazonaws.com/elasticache-downloads/AmazonElastiCacheCli-latest.zip"

commands: 
  make-elasticache-executable:
    command: chmod -R ugo+x /home/ec2-user/elasticache/*/bin/*

packages : 
  "yum" :
    "aws-apitools-cfn"  : []

container_commands:
  initial_cache_nodes:
    command: /etc/cfn/get-cache-nodes

<强> .ebextensions / elasticache-的settings.config

option_settings:
  "aws:elasticbeanstalk:customoption" :
    CacheNodeType : cache.t2.micro
    NumCacheNodes : 1
    Engine : redis
    NodeListPath : /var/nodelist

第一个脚本( elasticache-iam-with-script.config )用于创建附加到Elastic Beanstalk环境的ElastiCache资源。

第二个脚本( elasticache-settings.config )是第一个脚本的自定义配置文件。您可以根据需要更改配置。

第一个脚本将在 / var / nodelist 中生成一个文件。文件内容如下:

aws-my-xxxxxxxxxxxxx.xxxxxx.0001.use1.cache.amazonaws.com:us-east-1b

它是一个端点和AZ对,用冒号(<endpoint>:<AZ>)分隔。您的Java可能会解析 / var / nodelist 并将endpoint带入JedisPool