• OpenShift 4 - 用 OpenShift DevSpaces 在线开发 Quarkus 云原生应用


    OpenShift / RHEL / DevSecOps 汇总目录
    本文在 OpenShift 4.11 中已验证。

    请先根据《OpenShift 4 - 用 OpenShift DevSpaces 构建多租户的在线云原生应用开发环境》一文完成 OpenShift Dev Spaces 环境安装。

    了解 Workspace 运行模式和 Devfile 文件

    OpenShift DevSpaces 提供的 WebIDE 需要提供针对应用代码的界面交互功能和后台处理功能。在 OpenShift DevSpaces 中所有运行的界面、后台组件、处理功能都是运行在 Pod 中。例如要编译应用代码,就需要使用带有编译命令的 Image 然后使用编译命令。
    Devfile 是在 OpenShift DevSpaces 中运行的 Workspace 的定义文件,它包含以下各方面的配置:包括应用代码源,IDE 可以使用的插件,Build、Run、Debug 应用代码的操作命令和运行环境等。
    在这里插入图片描述
    在 OpenShift DevSpaces 中提供了部分常用的 Devfile 模板,我们也可以在 https://registry.devfile.io/viewer 上获取其他 Devfile 使用。
    在这里插入图片描述

    使用定制 Devfile 配置 Quarkus 应用开发环境

    1. 在 OpenShift Dev Spacs 控制台的 Create Workspace 页面中为 Git Repo URL 提供 https://github.com/liuxiaoyu-git/hello-world-quarkus,然后点击 Create & Open 按钮。
      在这里插入图片描述
    2. 可以查看 https://github.com/liuxiaoyu-git/hello-world-quarkus 中的 .devfile.yaml 文件的内容。说明:由于下文演示了将 Java 应用编译为 Native 应用的场景,因此 memoryLimit 最小需要 4Gi 内存。
    schemaVersion: 2.1.0
    metadata:
      name: hello-world-quarkus
    components:
      - name: tools
        container:
          image: quay.io/devfile/universal-developer-image:ubi8-latest
          endpoints:
            - name: webapp
              targetPort: 8080
          memoryLimit: 4Gi
          memoryRequest: 256Mi
    commands:
      - exec:
          commandLine: './mvnw quarkus:dev'
          component: tools
          group:
            kind: build
          workingDir: '${PROJECTS_ROOT}/hello-world-quarkus'
        id: 1-build-dev
      - exec:
          commandLine: './mvnw test'
          component: tools
          group:
            kind: test
          workingDir: '${PROJECTS_ROOT}/hello-world-quarkus'
        id: 2-test
      - exec:
          commandLine: './mvnw package'
          component: tools
          group:
            kind: run
          workingDir: '${PROJECTS_ROOT}/hello-world-quarkus'
        id: 3-package
      - exec:
          commandLine: 'java -jar target/quarkus-app/quarkus-run.jar'
          component: tools
          group:
            kind: run
          workingDir: '${PROJECTS_ROOT}/hello-world-quarkus'
        id: 4-run-jar
      - exec:
          commandLine: './mvnw install -Dnative -DskipTests -Dquarkus.native.native-image-xmx=4g'
          component: tools
          group:
            kind: build
          workingDir: '${PROJECTS_ROOT}/hello-world-quarkus'
        id: 5-native
      - exec:
          commandLine: './target/getting-started-1.0.0-SNAPSHOT-runner'
          component: tools
          group:
            kind: run
          workingDir: '${PROJECTS_ROOT}/hello-world-quarkus'
        id: 6-run-native
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    1. 名为 hello-world-quarkus 的 Workspace 创建完后会自动打开与其关联的 Web IDE 界面。
      在这里插入图片描述
    2. 在界面右侧 Workspace 的 User Runtime 中是快捷操。点击 tools 的 New terminal,然后在终端中运行修改 mvnw 的文件执行权限。
    $ chmod +x mvnw
    
    • 1

    在这里插入图片描述

    1. 点击 tools 中的 1-build-dev,根据前面 Devfile.yaml 文件,可知会在终端中运行 ./mvnw quarkus:dev 命令,它代表应用运行在开发模式中。
      在这里插入图片描述
    2. 完成后会自动在新窗口显示 Quarkus 应用的默认调试页面。
      在这里插入图片描述
    3. 在 tools terminal 0 窗口中执行以下命令,确认可以正常返回结果。
    $ curl -w "\n" http://localhost:8080/hello
    
    • 1

    在这里插入图片描述

    1. 关闭上图 1-build-dev 窗口。然后先点击右侧 tools 中的 3-package,再点击 4-run,此时应用再次运行起来。
    2. 另外也可在 tools terminal 0 中执行以下命令将应用编译转换成 Linux 可执行程序。
    ./mvnw install -Dnative -DskipTests -Dquarkus.native.native-image-xmx=4g
    
    • 1
    1. 随后在 tools terminal 0 中执行命令运行可执行程序。
    ./target/getting-started-1.0.0-SNAPSHOT-runner
    
    • 1
    1. 点击 tools 中的 New terminal,然后在新打开的窗口中执行命令测试可正常访问微服务。
    $ curl -w "\n" http://localhost:8080/hello
    
    • 1

    查看 DevWorkspace 实例对应的 Pod 资源

    用 user1 用户进入 DevWorkspace Operator 的 DevWorkspace 实例界面,有名为 hello-world-quarkus 的实例对应以上 Quarkus 应用。
    在这里插入图片描述
    进入 hello-world-quarkus 实例可以看到 Deployment、ReplicaSet、Pod 等相关资源,其中 Pod 显示 Running 的状态。
    在这里插入图片描述
    进入上图运行的 Pod,可以看到其中运行的 Container 和 对应的 Image,以及使用到的 Volume 配置。 注意:其中名为 tools 的容器既是由前面 Devfile 文件的 commponents 组件定义的。
    在这里插入图片描述
    以上 4 个运行的容器正是在 Web IDE 中 WORKSPACE 显示的 4 个 User Runtimes 项目,可以通过打开 New terminal 进入运行的容器。
    在这里插入图片描述

    演示视频

    视频

    参考

    https://che.eclipseprojects.io/2021/10/12/@mario.loriedo-devfile-v2-and-the-devworkspace-operator-p1.html
    https://che.eclipseprojects.io/2021/06/23/@florent.benoit-devfile-v2-and-ide-plug-ins.html
    https://che.eclipseprojects.io/2019/10/02/@florent.benoit-discover-che-7-devfile.html
    https://github.com/che-samples/java-spring-petclinic/blob/devfilev2/devfile.yaml
    https://github.com/devfile-samples
    https://developers.redhat.com/articles/2022/04/01/codeready-workspaces-scales-now-red-hat-openshift-dev-spaces#changes_to_the_workspace_engine

  • 相关阅读:
    微信小程序瀑布流布局
    安装DBD-Oracle报错处理
    vue考试系统后台管理项目-接口封装调用
    什么是类加载器,类加载器如何分类
    web前端-javascript-自增++和自减--(a++和++a,都在自身基础上+1,a--和--a都在自增基础上-1,自增和自减的练习)
    一文了解tcp/ip协议的运行原理
    【紫光同创盘古PGX-Lite 7K教程】——(盘古PGX-Lite 7K开发板/PGC7KD-6IMBG256第七章)数字钟实验例程
    C++设计模式_11_builder 构建器(小模式,不太常用)
    基于像素预测和位平面压缩的加密图像可逆数据隐藏附matlab代码(论文复现)
    C#联合halcon应用——大华相机采集类
  • 原文地址:https://blog.csdn.net/weixin_43902588/article/details/127430079