You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ubuntu 14.04 LTS环境下迁移Play Framework 1.2.4至2.1?

Alright, let's walk through migrating your Play 1.2.4 microservices to Play 2.1 on Ubuntu 14.04 LTS. I’ve tackled similar migrations before, so here’s a practical, step-by-step plan tailored to your setup:

1. Pre-Migration Setup on Ubuntu 14.04

First, get your environment ready to support Play 2.1:

  • Install Java 7: Play 2.1 requires Java 7 (it won’t work properly with newer versions). Run these commands to set it up:
    sudo apt-get update && sudo apt-get install openjdk-7-jdk
    export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
    echo "export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64" >> ~/.bashrc
    
    Verify with java -version to ensure you’re running Java 7.
  • Set up Play 2.1: Grab the latest stable Play 2.1 release (2.1.5) as a zip, extract it to /usr/local/play-2.1.5, then add it to your PATH:
    export PATH=$PATH:/usr/local/play-2.1.5
    echo "export PATH=$PATH:/usr/local/play-2.1.5" >> ~/.bashrc
    
    Test it with play help—you should see Play’s command line help.
  • Backup every microservice: Since you’re working with independent services, back each one up individually (e.g., tar -czf user-service-backup.tar.gz /path/to/user-service) to avoid data loss during migration.
2. Core Migration for Each Microservice

Play 1.x to 2.x is a full rewrite, so you’ll need to refactor each service incrementally:

  • Restructure the project:
    Play 1.x uses app/ (models/views/controllers), conf/, public/; Play 2.1 keeps these but adds a project/ folder for build configs. Start by creating a new Play 2.1 Java project:
    play new user-service --java
    
    Then move your old code into the new structure gradually.
  • Rewrite controllers:
    Play 1.x controllers extend Controller and use render(); Play 2.1 Java controllers extend play.mvc.Controller and return Result objects. For example:
    Old Play 1 code:
    public static void profile(Long userId) {
        User user = User.findById(userId);
        render(user);
    }
    
    New Play 2.1 code:
    public static Result profile(Long userId) {
        User user = User.find.byId(userId);
        return ok(views.html.profile.render(user));
    }
    
    Replace params.get() with request().getQueryString() or use form binding with form().bindFromRequest().
  • Convert view templates:
    Play 1.x uses Groovy templates (.gsp); Play 2.1 uses Twirl (.scala.html) with different syntax. Key changes:
    • Variable output: ${name} → @name
    • Loops: #{list items:users, as:'user'}...#{/list} → @for(user <- users) { ... }
    • URL generation: Hardcoded paths → @routes.UserController.profile(user.id)
  • Adjust ORM configurations:
    If you used Play 1.x’s Ebean, Play 2.1 supports it but requires updated config. Add ebean.default="models.*" to conf/application.conf, and update query syntax:
    Old: User.find("byEmail", email).first() → New: User.find.where().eq("email", email).findUnique()
    For other ORMs like Hibernate, add dependencies in project/Build.scala.
  • Update config files:
    Play 1.x’s .properties-style application.conf becomes HOCON format in Play 2.1. Example changes:
    # Old Play 1
    db.url=jdbc:mysql://localhost/mydb
    db.user=root
    
    # New Play 2.1
    db.default.url="jdbc:mysql://localhost/mydb"
    db.default.user="root"
    
    Rewrite conf/routes too: GET /profile/:userId controllers.UserController.profile(userId: Long)
3. Microservice-Specific Best Practices

Since your project is split into microservices, use this to your advantage:

  • Migrate one service at a time: Start with services that have the fewest dependencies, test thoroughly, then move to dependent services.
  • Run old and new services in parallel: Use a load balancer or API gateway to gradually shift traffic to the new Play 2.1 services. This lets you catch issues without downtime.
  • Update inter-service communication: Replace Play 1.x’s WS client with Play 2.1’s play.libs.WS for HTTP calls between services. Example:
    WS.url("http://payment-service/api/charge").post(formData).map(response -> {
        // Handle response
    });
    
4. Testing & Deployment on Ubuntu 14.04
  • Test locally: For each migrated service, run play run and validate all endpoints, database interactions, and user flows match the old service.
  • Build and deploy:
    • Generate a standalone distribution with play dist—this creates a zip file you can extract on your server.
    • Use Ubuntu’s Upstart to manage the service. Create /etc/init/user-service.conf:
      description "User Microservice"
      start on runlevel [2345]
      stop on runlevel [016]
      exec /path/to/user-service/bin/user-service -Dhttp.port=9001
      respawn
      
      Start it with sudo start user-service.
  • Database migrations: Use tools like Flyway or Liquibase to handle schema changes—integrate them into your Play 2.1 project to ensure data is migrated smoothly.
5. Troubleshooting Tips
  • Java version conflicts: Use sudo update-alternatives --config java to switch to Java 7 if Ubuntu defaults to a newer version.
  • Dependency issues: Clean up old Play 1.x JARs from your project—Play 2.1 manages dependencies via Build.scala, so avoid mixing old libraries.
  • Template errors: Twirl’s error messages are detailed—follow them to fix syntax issues like missing closing brackets or incorrect variable references.

内容的提问来源于stack exchange,提问作者user10389600

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:43:11