Skip to content

Lab 16

Steps and Files

  1. Delete YAML
    • application_prod.yml
    • application_qa.yml
  2. Delete Properties
    • application.yml
  3. Add Application Name and Active Profile Properties
    • application.yml
  4. Spring Cloud Client Dependency
    • pom.xml
  5. Config Server Details
    • application.yml
  6. Start Config Server and Accounts Microservice()
  7. Start with a Different Profile
  8. Change Other Microservices

Lab#16 Modifying the microservices to use the SpringCloud Config server

In this lab we will be modify the accounts microservice to read from the values from the configserver.

1. Delete YAML

Step#1 In the accounts microservice, delete the application_prod.yml and the application_qa.yml files.

Delete YAML

Figure 1. Delete application_prod.yml and application_qa.yml files.

2. Delete Properties

Step#2 Also delete everything to do with properties from the application.yml leaving only as shown below.

application.yml delete properties
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
server:
  port: 8080
spring:
  datasource:
    url: jdbc:h2:mem:testdb
    driverClassName: org.h2.Driver
    username: sa
    password: ''
  h2:
    console:
      enabled: true
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: update
    show-sql: true

3. Add Application Name and Active Profile Properties

Step#3 Add properties for application name and active profile

application.yml add application name and active profile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
server:
  port: 8080
spring:
  application:
    name: "accounts"
  profiles:
    active: "prod"
  datasource:
    url: jdbc:h2:mem:testdb
    driverClassName: org.h2.Driver
    username: sa
    password: ''

4. Spring Cloud Client Dependency

Step#4 Add the spring cloud client dependency in the pom file for accounts microservice

pom.xml add spring cloud client dependency
29
30
31
32
33
34
35
36
37
<properties>
        <java.version>17</java.version>
        <spring-cloud.version>2022.0.5</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

This can be copied from config server

pom.xml add spring cloud client dependency
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>

5. Config Server Details

Step#5 Now also add details of the config server to the application.yml (child of spring)

application.yml add config server details
 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
server:
  port: 8081
spring:
  cloud:
    compatibility-verifier:
      enabled: false # disable version check
  application:
    name: "accounts"
  profiles:
    active: "prod"
  datasource:
    url: jdbc:h2:mem:testdb
    driverClassName: org.h2.Driver
    username: sa
    password: ''
  h2:
    console:
      enabled: true
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: update
    show-sql: true
  config:
   import: "configserver:http://localhost:8071/" # config server port 8071

6. Start Config Server and Accounts Microservice

Step#6 Start the config server and the accounts microservice. Test using Postman.

Postman: GET http://localhost:8081/api/contact-info

Test Using Postman config-info

Figure 2. Test /config-info endpoint using Postman.

Test Using Postman build-info

Figure 3. Test /build-info endpoint using Postman.

7. Start with a Different Profile

Step#7 Starting with a different profile. Change the Run As Configuration. Restart the accounts microservice.

Program arguments
--spring.profiles.active=qa

Run As Configuration: QA

Figure 4. Run As Configuration: QA

Postman GET /contact-info

Figure 5. Postman GET /contact-info

8. Change Other Microservices

Step#8 Make similar changes in the cards and loans microservices.

GET cards /contact-info

Figure 6. GET loans /contact-info

GET loans /contact-info

Figure 7. GET cards /contact-info