Openshift:使用Java 8在Wildfly 8.2.0上构建Spring Boot应用程序

OpenShift DIY墨盒是在OpenShift上测试不受支持的语言的好方法。 但是它不具有可伸缩性(您可以在此处为可伸缩DIY墨盒投票),这使得它很难与生产级Spring Boot应用程序一起使用。 但是,如果我们将Spring Boot应用程序部署到WildFly Application Server,该怎么办? Spring Boot可以与诸如Tomcat之类的嵌入式servlet容器一起运行,也可以与速度更快的Undertow一起运行,但是它也可以部署到独立的应用服务器上。 这意味着它也可以部署到OpenShift支持的WildFly应用服务器。 让我们来看看从头开始创建Spring Boot应用程序并将其部署到OpenShift上的WildFly 8.2多么容易。

注意 :在浏览OpenShift 文档时,您可能会认为WildFly 8.1和Java 7均受支持(截至撰写本文时)。 幸运的是,这不再是真的:WildFly 8.2和Java 8可以正常工作,并且实际上是默认设置! 这是我第一次对文档过时感到高兴。

更新 :如果您正在寻找快速入门 ,而没有逐步介绍,请查看: 快速入门:OpenShift上的Spring Boot和WildfFly 8.2

先决条件

在开始构建应用程序之前,您需要安装一个OpenShift免费帐户和客户端工具( rhc )。

创建WildFly应用程序

要使用客户端工具创建WildFly应用程序,请键入以下命令:

rhc create-app boot jboss-wildfly-8 --scaling

jboss-wildfly-8盒带描述为WildFly Application Server 8.2.0.Final。 使用缩放选项,因为以后将无法设置它( 在此处投票)

创建应用程序后,您应该看到为您创建的管理用户的用户名和密码。 请存储这些凭据,以便能够登录WildFly管理控制台。

模板应用程序源代码

OpenShift创建一个模板项目。 该项目是标准的Maven项目。 您可以浏览pom.xml并看到该项目默认使用Java 8。 此外,还有创建了两个非标准文件夹: deployments ,用来把生成的存档进去, .openshift与OpenShift特定文件。 请注意.opensift/config 。 这是WildFly配置存储的地方。

Spring Boot依赖项

作为依赖项管理,将使用Spring IO Platform。 使用Spring IO Platform的主要优势在于,它通过提供Spring项目的版本以及经过测试并已知可以协同工作的依赖项,简化了依赖项管理。 通过添加以下内容来修改pom.xml

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.spring.platform</groupId>
            <artifactId>platform-bom</artifactId>
            <version>1.1.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

现在,可以添加Spring Boot依赖项。 请注意,由于该应用程序将部署到WildFly,因此我们需要显式删除对Tomcat的依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

配置应用程序

初始化Spring Boot应用程序

具有所有依赖性,我们可以添加应用程序代码。 在demo包中创建Application.javaApplication类的工作是启动Spring Boot应用程序,因此它必须从SpringBootServletInitializer扩展并使用@SpringBootApplication进行注释。

package demo;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

}

@ Entity,@ Repository,@ Controller

Spring Data JPA是较大的Spring Data系列的一部分,可轻松实现基于JPA的存储库。 对于那些不熟悉该项目的人,请访问: http : //projects.spring.io/spring-data-jpa/

此示例项目的域模型只是具有一些基本字段的Person

@Entity
@Table(name = "people")
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Integer id;

    @Column(name = "first_name")
    @NotEmpty
    protected String firstName;

    @Column(name = "last_name")
    @NotEmpty
    protected String lastName;

    @Column(name = "address")
    @NotEmpty
    private String address;

    @Column(name = "city")
    @NotEmpty
    private String city;

    @Column(name = "telephone")
    @NotEmpty
    @Digits(fraction = 0, integer = 10)
    private String telephone;

}

Person需要@Repository ,所以我们可以createa使用Spring的数据仓库基础之一。 通过简单的接口定义,Spring Data存储库减少了许多样板代码:

@Repository
public interface PeopleRepository extends PagingAndSortingRepository<Person, Integer> {
    List<Person> findByLastName(@Param("lastName") String lastName);
}

使用域模型后,可以方便地使用一些测试数据。 最简单的方法是为data.sql文件提供要在应用程序启动时执行的SQL脚本。

创建src/main/resources/data.sql其中包含people表的初始数据(请参见下文)。 Spring Boot将选择该文件并针对配置的数据源运行。 由于使用的数据源正在连接到H2数据库,因此必须使用正确的SQL语法:

INSERT INTO people VALUES (1, 'George', 'Franklin', '110 W. Liberty St.', 'Madison', '6085551023');

有了Spring Data JPA存储库后,我们可以创建一个简单的控制器来通过REST公开数据:

@RestController
@RequestMapping("people")
public class PeopleController {

    private final PeopleRepository peopleRepository;

    @Inject
    public PeopleController(PeopleRepository peopleRepository) {
        this.peopleRepository = peopleRepository;
    }

    @RequestMapping
    public Iterable<Person> findAll(@RequestParam Optional<String> lastName) {
        if (lastName.isPresent()) {
            return peopleRepository.findByLastName(lastName.get());
        }
        return peopleRepository.findAll();
    }
}

findAll方法接受可选的lastName参数,该参数绑定到Java的8 java.util.Optional

首页

OpenShift在项目设置过程中生成的项目包含带有一些静态文件的webapp文件夹。 可以删除这些文件,并可以修改index.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>OpenShift</title>
</head>
<body>
<form role="form" action="people">
    <fieldset>
        <legend>People search</legend>
        <label for="lastName">Last name:</label>
        <input id="lastName" type="text" name="lastName" value="McFarland"/>
        <input type="submit" value="Search"/>
    </fieldset>
</form>
<p>
    ... or: <a href="people">Find all ...</a>
</p>
</body>
</html>

它只是一个静态页面,但是我注意到,如果没有默认映射( / )或返回的代码不同于200 ,则应用程序将无法启动。 通常,将始终存在默认映射。

组态

创建src/main/resources/application.properties并输入以下值:

  • management.context-path=/manage :执行器默认管理上下文路径为/ 。 更改为/manage ,因为OpenShift公开了/health端点本身,该端点涵盖了Actuator的/health端点。
  • spring.datasource.jndi-name=java:jboss/datasources/ExampleDS :由于应用程序使用Spring Data JPA,因此我们希望通过JNDI绑定到服务器的数据源。 请查看.openshift/config/standalone.xml以获取其他数据源。 如果您希望配置MySql或PostgreSQL与您的应用程序一起使用,则这一点很重要。 在此处阅读有关在Spring Boot中连接到JNDI数据源的更多信息: http : //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-connecting-to-a-jndi-datasource
  • spring.jpa.hibernate.ddl-auto=create-drop :基于提供的实体创建数据库的结构。

部署到OpenShift

准备将应用程序推送到存储库。 提交本地更改,然后将其推送到远程:

git push

初始部署(构建和应用程序启动)将花费一些时间(最多几分钟)。 后续部署要快一些。 现在,您可以浏览到: http : //appname-yournamespace.rhcloud.com/ ,您应该看到以下形式:

单击具有默认值的搜索将获得ID = 3的记录:

[
    {
        "id": 3,
        "firstName": "2693 Commerce St.",
        "lastName": "McFarland",
        "address": "Eduardo",
        "city": "Rodriquez",
        "telephone": "6085558763"
    }
]

导航到http://appname-yournamespace.rhcloud.com/people将返回数据库中的所有记录。

走向Java 7

如果要在项目中使用Java 7,而不是默认的Java 8, .openshift/markers/java8重命名为.openshift/markers/java7并相应地更改pom.xml

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <maven.compiler.fork>true</maven.compiler.fork>
</properties>

请注意, maven.compiler.executable已删除。 不要忘记更改@Controller的代码并使它与Java 7兼容。

摘要

在此博客文章中,您学习了如何配置基本的Spring Boot应用程序以及如何使用WildfFly 8.2和Java 8在OpenShift上运行它。OpenShift使用Web代理HAProxy扩展应用程序。 OpenShift负责自动添加或删除应用程序副本以根据需要处理请求。

资源资源

翻译自: https://www.javacodegeeks.com/2015/02/openshift-build-spring-boot-application-on-wildfly-8-2-0-with-java-8.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值