简体   繁体   中英

Reading an external YAML/JSON File from Bitbucket Repository using Spring Cloud Config Server in a Spring Boot Application

There is a Spring boot application deployed on Cloud foundry which needs to connect to a Bitbucket repository to get data from an external Yaml file present in the repository. Currently, I'm using Spring Cloud Config Server and configured bitbucket repository via SSH key in application.yaml. Public key is placed under repository setting on bitbucket server.

application.yaml

spring:
  cloud:
    config:
      server:
        git:
          uri: ssh://git@bitbucket.bip.intl.com/in/myrepo.git
          cloneOnStart: true
          ignoreLocalSshSettings: true
          default-label: feature/RQ4
          privateKey: |
                         -----BEGIN RSA PRIVATE KEY-----
                         MIIEpAIBAAKCAQEAx8yTitlZiJQ8WbQueMBXK3pfBlHM7XQ69i7asDZK14bAjmAH
                         ER3V8rynmvKGpkih7TPyqyr6r+HAEalckLaCqwH6GANA24gdHYl+un5czLQiHY9G
                         PyWBwH3NXv5jsPxwrgWRoQGYwQ8U8lzA+eI6g7+x7qeRK5+Vpi5+GghEHS/x0ujb
                         BXUL2MwieB+yOpOyPGDvj5jDCaFo0wWrUeQ8hyoUVDNdV0+U4LrU1GDFsfNLCmUt
                         udQ5pwmUgYWDRnw4mGsI20cP/wuWeys3ZToUSYLPQFJTXE5ZaJe/XPxPoFbFMS8k
                         9zob5trbuuvMul3G56xnN15CBdo+50T19OG8oQIDAQABAoIBAQC/3EaOXfspOVfe
                         uDSsBd9vQ9p+QgzstOFtyD5+WhRydNbxoWsH18O8jcbQ9zWv9i1wj6LnFaWXQcxj
                         6WOXH3vI5WQYrP8aENIMrB0CzxckB5J/GjdVqhm4E9Yj81sd+LvnkDdxCgx6z5XI
                         yqMRElooa+tchpLT9jIJW7oCG4/1CYNYLlH0gTjhCEQwLwm2C7FlbcaJsADlQhRa
                         +CUbqU5r0fI1ixTlg1VHAVyGtLEM2w0E3Cc0zAioYu/+re6zadTJHU5osyIMYpzj
                         5CYleWBh6/cfu2JrT8Dd5jxdXhG2mirLZrWpbGXjcLTHjp8FSdmDo8pgfcW5aYgR
                         SzuuqamVAoGBAO47UjGFCpph6YbepOvZnt0Uxo+IwOIqvgYt8ox5A2pOQBOjOSQU
                         YClhclhzuJ4zHHOgvv1PzC2E7tNEH4e93Lcfssqqx6WdyFnPlf6fo9tNh+ZZETWd
                         jMByzinK7j788AkOhpDxiULSkc1EMJ3T/D9ADrN6ZxfI9lq4LXVCJ79/AoGBANaz
                         cTaWIyD1CShaMx7oBLxq9Q8h2OnPhzsA9N8z8E6ZczXIUUC7TAZPEJA6bkECusxl
                         vDhb3LxIyHDk03RBybmOBsxqpzIdpx8DiHTwwaWqw8Qo852Rxgr7gqxIY5lC6Ll3
                         +ZF2iTGQJeS04bo9RSU9cuRuz1UAh+dgvgwktJPfAoGBALkLGM/gEBlUfkjFPYL4
                         uV3uGkE5LuE94/X20JRuFsoG19ypBuRQc1URHPk5gahX+PyTWmZ2Chk9TSiKnJca
                         UYtoMr8bIhIxWYLxLAfoRiU8tpekjSqBIugFBMAPoTkJzQSr5LhG9KAT4S54je65
                         zw/uGL0uEU+3SCu1LZIL/cXPAoGAF6clLdJUhGj6XUuynhEJlIhj/wrHjtqiF0n/
                         sCdUrhQRsTckXHFF6twDAyCszNS0J5h9p+fW5ZdPHcDX2tL0mlCfRx3o1YiWxL/q
                         5JcxfkLV1mJ8lDtfCg6zUq+W6WeJpAUnpdffddfcAIM86vudBoFCiw/6H28zibYK
                         uePBURkCgYBtrsPfcqStKzgYMEM+xPtJHYuuBcVNmZfRZzRCmCF5wlgSSdHUkO0K
                         ZTZZNmqeQTHK0AWDLg+W/Fr0HWrY6Y1aj8Fo/II8eMLsCQIX+fVayXgRxM9vqJgb
                         cCfB7Fg0OOA9NofWifXmL7jRyR86jipEo6Ixc+ULhrehnS+FioPcRw==
                         -----END RSA PRIVATE KEY-----

External yaml file which is present in my bitbucket repository is as below. This Yaml file I need to load and read in my Spring boot PaaS via Spring Cloud Config Server. Assume that Out.yaml file is present in a branch feature/RQ4 or a dev-intg branch at root location.

Will it be possible to load complete Yaml file in my Spring boot application in plain text as Java String. Once I get the complete Yaml content, I will use Object mapper and convert to corresponding java object using mapper class written for that Yaml. I'm able to connect to my repository via Config Server using SSH keys but not sure how to read yaml or equivalent Json file from bitbucket repo.

Out.yaml

---      
   
data:  
  - outNumber: 1234
    outName: "name1"
    priority: 2
    clientTypes:
      - clientType: XYZ

  - outNumber: 4567
    outName: "name2"
    identifier: name2
    priority: 1
    clientTypes:
      - clientType: ABC

when reading data on properties files. you can have a Class and mapped accordingly.

@Component
@ConfigurationProperties("customer")
public class CustomerConfiguration {

private final Map<String, List<Customer>> customerProfiles;
 
public CustomerConfiguration() {
customerProfiles = new HashMap<>();   

public static class Customer {
 private String outNumber;
 private String outName;
 private int priority;
 private Map<String, String> clientTypes;
} 

//getters and setters

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM