|
| 1 | +--- |
| 2 | +title: Prepare JDBC connection string |
| 3 | +--- |
| 4 | + |
| 5 | +It's common to use the JDBC connection string to connect to a database. This doc will focus on how to prepare the JDBC connection string |
| 6 | +and how to use it in the application. |
| 7 | + |
| 8 | +## How to generate the JDBC connection string for business component |
| 9 | + |
| 10 | +- Apply WorkflowStep `generate-jdbc-connection` |
| 11 | + |
| 12 | +> WorkflowStep `generate-jdbc-connection` will be automatically applied with KubeVela chart installation. It's not necessary |
| 13 | +> to apply it manually when this feature is released. |
| 14 | +
|
| 15 | +```shell |
| 16 | +$ vela def apply generate-jdbc-connection.cue |
| 17 | +``` |
| 18 | + |
| 19 | +``` |
| 20 | +# generate-jdbc-connection.cue |
| 21 | +import ( |
| 22 | + "vela/op" |
| 23 | + "encoding/base64" |
| 24 | +) |
| 25 | +
|
| 26 | +"generate-jdbc-connection": { |
| 27 | + type: "workflow-step" |
| 28 | + annotations: {} |
| 29 | + labels: { |
| 30 | + "ui-hidden": "true" |
| 31 | + } |
| 32 | + description: "Generate a JDBC connection based on Component of alibaba-rds" |
| 33 | +} |
| 34 | +template: { |
| 35 | + output: op.#Read & { |
| 36 | + value: { |
| 37 | + apiVersion: "v1" |
| 38 | + kind: "Secret" |
| 39 | + metadata: { |
| 40 | + name: parameter.name |
| 41 | + if parameter.namespace != _|_ { |
| 42 | + namespace: parameter.namespace |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | +
|
| 48 | + cluster: parameter.cluster |
| 49 | +
|
| 50 | + dbHost: op.#ConvertString & {bt: base64.Decode(null, output.value.data["DB_HOST"])} |
| 51 | + dbPort: op.#ConvertString & {bt: base64.Decode(null, output.value.data["DB_PORT"])} |
| 52 | + dbName: op.#ConvertString & {bt: base64.Decode(null, output.value.data["DB_NAME"])} |
| 53 | + jdbc: [{name: "jdbc", value: "jdbc://" + dbHost.str + ":" + dbPort.str + "/" + dbName.str}] |
| 54 | +
|
| 55 | + parameter: { |
| 56 | + // +usage=Specify the name of the secret generated by alibaba-rds |
| 57 | + name: string |
| 58 | + // +usage=Specify the namespace of the secret generated by alibaba-rds |
| 59 | + namespace?: string |
| 60 | + // +usage=Specify the cluster of the object |
| 61 | + cluster: *"" | string |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +The property `namespace` and `name` refers to the namespace and name of the secret generated by a database component. |
| 67 | + |
| 68 | +``` |
| 69 | +$ vela show generate-jdbc-connection |
| 70 | +# Properties |
| 71 | ++---------------+--------------------------------------------------------------+--------+----------+---------+ |
| 72 | +| NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT | |
| 73 | ++---------------+--------------------------------------------------------------+--------+----------+---------+ |
| 74 | +| name | Specify the name of the secret generated by alibaba-rds | string | true | | |
| 75 | +| namespace | Specify the namespace of the secret generated by alibaba-rds | string | false | | |
| 76 | +``` |
| 77 | + |
| 78 | +[The keys of the secret]((./terraform/alibaba-rds#outputs)) are as below: |
| 79 | + |
| 80 | +| Name | Description | |
| 81 | +|---------------------|--------------------------------| |
| 82 | +| DB_NAME | RDS Instance Name | |
| 83 | +| DB_USER | RDS Instance User | |
| 84 | +| DB_HOST | RDS Instance Host | |
| 85 | +| DB_PASSWORD | RDS Instance Password | |
| 86 | +| DB_PORT | RDS Instance Port | |
| 87 | +| DATABASE_NAME | RDS Database Name | |
| 88 | + |
| 89 | +> If the keys of the secret generated by a database component are not as above, the workflow could not work. |
| 90 | +
|
| 91 | +- Apply an application which use the JDBC connection string |
| 92 | + |
| 93 | +```shell |
| 94 | +apiVersion: core.oam.dev/v1beta1 |
| 95 | +kind: Application |
| 96 | +metadata: |
| 97 | + name: jdbc |
| 98 | +spec: |
| 99 | + components: |
| 100 | + - name: db |
| 101 | + type: alibaba-rds |
| 102 | + properties: |
| 103 | + instance_name: favorite-links |
| 104 | + database_name: db1 |
| 105 | + account_name: oamtest |
| 106 | + password: U34rfwefwefffaked |
| 107 | + security_ips: [ "0.0.0.0/0" ] |
| 108 | + privilege: ReadWrite |
| 109 | + writeConnectionSecretToRef: |
| 110 | + name: db-conn |
| 111 | + - name: express-server |
| 112 | + type: webservice |
| 113 | + properties: |
| 114 | + image: crccheck/hello-world |
| 115 | + port: 8000 |
| 116 | + |
| 117 | + workflow: |
| 118 | + steps: |
| 119 | + - name: jdbc |
| 120 | + type: generate-jdbc-connection |
| 121 | + outputs: |
| 122 | + - name: jdbc |
| 123 | + valueFrom: jdbc |
| 124 | + properties: |
| 125 | + name: db-conn |
| 126 | + namespace: default |
| 127 | + - name: apply |
| 128 | + type: apply-component |
| 129 | + inputs: |
| 130 | + - from: jdbc |
| 131 | + parameterKey: env |
| 132 | + properties: |
| 133 | + component: express-server |
| 134 | + |
| 135 | +``` |
| 136 | + |
| 137 | +Apply the application above and check whether the jdbc environment is successfully set to component `express-server`. |
| 138 | + |
| 139 | +```shell |
| 140 | +$ kubectl get deploy express-server -o yaml |
| 141 | +apiVersion: apps/v1 |
| 142 | +kind: Deployment |
| 143 | +metadata: |
| 144 | + name: express-server |
| 145 | + namespace: default |
| 146 | + resourceVersion: "67119228" |
| 147 | + uid: 228493b2-7ee7-4147-91f6-b7b23bc78c3b |
| 148 | +spec: |
| 149 | + ... |
| 150 | + template: |
| 151 | + metadata: |
| 152 | + creationTimestamp: null |
| 153 | + labels: |
| 154 | + app.oam.dev/component: express-server |
| 155 | + app.oam.dev/revision: express-server-v2 |
| 156 | + spec: |
| 157 | + containers: |
| 158 | + - env: |
| 159 | + - name: jdbc |
| 160 | + value: jdbc://rm-xxxx.mysql.rds.aliyuncs.com:3306/db1 |
| 161 | + image: crccheck/hello-world |
| 162 | +``` |
| 163 | + |
| 164 | +We can see that the environment variable `jdbc` whose value is a JDBC connection string, is successfully set to the component `express-server`. |
0 commit comments