Menu Close
4.4.2. 部署 Spring PetClinic 示例应用程序
要在 OpenShift Container Platform 集群上部署 Spring PetClinic 示例应用程序,您必须使用部署配置并配置本地环境才能测试应用程序。
流程
使用
PostgresCluster
自定义资源 (CR) 在 shell 中运行以下命令来部署spring-petclinic-rest
应用程序:$ oc apply -f - << EOD --- apiVersion: apps/v1 kind: Deployment metadata: name: spring-petclinic-rest namespace: my-postgresql labels: app: spring-petclinic-rest spec: replicas: 1 selector: matchLabels: app: spring-petclinic-rest template: metadata: labels: app: spring-petclinic-rest spec: containers: - name: application image: quay.io/baijum/spring-petclinic-rest:latest env: - name: SPRING_PROFILES_ACTIVE value: postgresql,spring-data-jpa ports: - name: http containerPort: 9966 --- apiVersion: v1 kind: Service metadata: name: spring-petclinic-rest namespace: my-postgresql spec: ports: - port: 80 targetPort: 9966 selector: app: spring-petclinic-rest EOD
输出会验证 Spring PetClinic 示例应用是否已创建并部署:
输出示例
deployment.apps/spring-petclinic-rest created service/spring-petclinic-rest created
从应用程序端口设置端口转发,以便从本地环境访问示例应用程序:
$ oc port-forward --address 0.0.0.0 svc/spring-petclinic-rest 9966:80 -n my-postgresql
访问 http://localhost:9966/petclinic。
现在,您可以在 localhost:9966 远程访问 Spring PetClinic 示例应用程序。
注意应用尚未连接到数据库服务。如果您尝试与应用交互,它将返回错误。
例如,如果尝试使用
curl
访问所有 pets 列表,您可以看到类似以下示例消息的错误消息:$ curl -X GET "http://localhost:9966/petclinic/api/pets" -H "accept: application/json" {"className":"org.springframework.transaction.CannotCreateTransactionException","exMessage":"Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection"}
现在,您可以使用 Service Binding Operator 将应用程序连接到数据库服务。