"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Spring API Gateway Date : 05/02/2025 (Session - 119) _____________________________________________________________________________________________________________________________ Config Server ============= * Generally We have several Microservices in project development and each microservice requires some of set of configurations such as database configuration,email configurations,security configuration,application configurations etc., * Suppose if We have 10 microservices in the project definetly we need to duplicate the above configuration for each microservice. * If we are duplicating the configuration of microservices the major problem is that whenever any configuration got changed we need to make necessary changes in all microservices and need to restart or redeployment required. * Inorder to overcome the above problem we got solution as "Config Server". * Config Server is an another simple microservice which will maintains common configurations for all microservices. * Config Server maintains external configuration means common configurations will be stored in Github/Gitlab/Bitbucket. * Inorder to Create the Config Server using spring boot project by adding starter called "config-server". Architecture ============ (Connecting) (Spring Boot) (Connecting) UserService/CustomerService >>>>>>>>>>>>>>>>>> Config Server >>>>>>>>>>>>>>>>>>>>>Github/Gitlab/Bitbucket * Generally Spring cloud config Server collect the configurations in two ways 1) External Configuration i.e., Collecting the configuration from github,gitlab,bitbucket etc., * This kind of configuration mostly will be used for production environment. 2) Native Configuration i.e., Collecting the configuration from local system * This kind of configuration mostly will be used of Development environment. Steps For Developing Config Server Application ============================================== 1) Create the Spring boot Project with below Starter * Config Server 2) Add the below annotation on top of our Spring Boot Main Class @EnableConfigServer 3) Below entries are required in application.properties #github repoistory link spring.cloud.config.server.git.uri=https://github.com/maheshashokit/MicroServices-Configuration #github repository cloning during application startup spring.cloud.config.server.git.clone-on-start=true #Default PortNo for ConfigServer is 8888 server.port=8888 #Setting the active Profile spring.profiles.active=prod 4) After Starting the Spring Boot Application hit below url in your favourite browser http://localhost:8888/application/prod >>>>>>>>>>>>>>>>> active profile OUTPUT ====== // 20231017070826 // http://localhost:8888/application/prod { "name": "application", "profiles": [ "prod" ], "label": null, "version": "35b67ad34e5159a55f85b999666617d070ebe5eb", "state": null, "propertySources": [ { "name": "https://github.com/maheshashokit/MicroServices-Configuration/application-prod.properties", "source": { "spring.datasource.driver-class-name": "oracle.jdbc.driver.OracleDriver", "spring.datasource.url": "jdbc:oracle:thin:@localhost:1521:xe", "spring.datasource.username": "system", "spring.datasource.password": "manager", "spring.jpa.hibernate.ddl-auto": "update", "spring.jpa.show-sql": "true", "welcome.message": "Welcome To AshokIT Production Branch...." } }, { "name": "https://github.com/maheshashokit/MicroServices-Configuration/application.properties", "source": { "spring.datasource.driver-class-name": "oracle.jdbc.driver.OracleDriver", "spring.datasource.url": "jdbc:oracle:thin:@localhost:1521:xe", "spring.datasource.username": "system", "spring.datasource.password": "manager", "spring.jpa.hibernate.ddl-auto": "update", "spring.jpa.show-sql": "true", "welcome.message": "Welcome To AshokIT...." } } ] } Steps for Developing the Config Client Applications =================================================== 1) Take the Existing the Microservices either Customer-Service (or) Address-Service 2) Add the below changes in application.properties #Database Configuration #spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver #spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe #spring.datasource.username=system #spring.datasource.password=manager #Hibernate Configuration #spring.jpa.hibernate.ddl-auto=update #spring.jpa.show-sql=true #To get connected with config server MS from current microservices #Always gets the details from config server which is running on port of 8888 spring.config.import=optional:configserver: 3) Start the Service-Discovery Application & Customer-Service Application and observe the logs whether configuration is collected from config server or not. 4) Test Some API Resources from Customer-Service in PostMan tool to feel that configuration are inherited or not. NOTE ==== * By default spring cloud config server runs on 8888 port number only. * We can change the port number as per programmer conviences, so we changed to 8572 to cloud config server * When we change the port number make sure config client application need to specify config server url as below in customer-service #Always gets the details from config server which is running on port of 8572 spring.config.import=optional:configserver:http://localhost:8572 Native Configuration ==================== Config Server Application Changes ================================= 1) Create one folder under src/main/resources i.e.,config folder to be created. 2) After creating config folder, We need to copy and paste yesterday developed files below * application.properties * application-dev.properties * application-prod.properties * application-test.properties 3) Inorder to collect the configuration details from local file system by the config server, we need to activate one profile i.e.,native to be configured in application.properties application.properties(Config Server Application related properties file) ========================================================================= #Default PortNo for ConfigServer is 8888 server.port=8572 #Activating the native configuration spring.profile.active=native spring.cloud.config.server.native.search-locations=classpath:config/ 4) No Changes in Config Client Application i.e.,Customer-Service 5) After starting the Config Server Application test the below application url in browser window to ensure whether our configurations are pulling for local file system or github repoistory. http://localhost:8572/application/name >>>>>>>>>>>> Favourite Browser OUTPUT ====== // 20231018063122 // http://localhost:8572/application/name { "name": "application", "profiles": [ "name" ], "label": null, "version": null, "state": null, "propertySources": [ { "name": "classpath:/config/application.properties", (*********************************************) "source": { "spring.datasource.driver-class-name": "oracle.jdbc.driver.OracleDriver", "spring.datasource.url": "jdbc:oracle:thin:@localhost:1521:xe", "spring.datasource.username": "system", "spring.datasource.password": "manager", "spring.jpa.hibernate.ddl-auto": "update", "spring.jpa.show-sql": "true", "welcome.message": "Welcome To AshokIT...." } } ] } Three Repos ------------ 1) Main Repo i.e, Customer-Service 2) Config Repo i.e.,Customer-Service-config >>> ${username}, ${password} 3) Book Repo i.e, Customer-Service-Book >>> username, password How to get changes from config Server with out restart the Microservices ======================================================================== 1) We need to keep ready of Config Server Application which has been connected to our github or gitlab account 2) We need to develop the Config Client(Customer-Service) Application with below starters 1) Spring Cloud Config >>>>>>>>> Config Client 2) actuator >>>>>>>> /refresh(POST API) * Actuator are nothing but an providing Production Ready Features as predefined Rest API's and by default all actuators will not be activated in our spring boot application. * Inorder to activate the all actuators in the spring boot application we need to add the below configuration in application.properties of Config Client management.endpoints.web.exposure.include=* * We need to add @RefreshScope annotation on top of controller class. * Make sure every actuator will have based URL Mapping i.e.,/actuator /actuator/info /actuator/threaddump /actuator/health /actuator/refresh(****************) 3) We need to make some changes in our github repoistory simply by changing username & password with respective environment properties file and do the commit operation application-dev.properties ========================== welcome.message<--------> Welcome To AshokIT Development Branch.... <---> Welcome To AshokIT Dev Branch.... 4) After completion of our commit in our repoistory we need to execute below Actuator API Endpoint in postman tool POST <---------------> http://localhost:9577/actuator/refresh OUTPUT ====== [ "config.client.version", "welcome.message" ] 5) Just Simple refresh our API endpoint to see the latest changes done in our github repo http://localhost:9977/api/customers/welcome