Monday, 20 September 2021

Spring Cloud Config Server with Github

 1. Create a limit-service Client Spring Boot project from https://start.spring.io/


2. Create a spring-cloud-config-server Spring Boot project from https://start.spring.io/



3. Open application.properties in limits-service project and do two things;

spring.application.name=limits-service

server.port=8081


4. Open application.properties file in spring-cloud-config-server project.

spring.application.name=spring-cloud-config-server

server.port=8888


5. Open SpringCloudConfigServerApplication.java file in spring-cloud-config-server and include the below annotation there

@EnableConfigServer


6. Since Spring config server is starting at port 8888, we need to give the server address to limits-service. Add the below line in application.properties in limits-service project

spring.config.import=optional:configserver:http://localhost:8888


7. We can create a Local Git repo or Remote Git repo to add the properties files,

We need to give the path of properties files in application.properties of spring-cloud-config-server.


#Local Git config path

spring.cloud.config.server.git.uri=file:///D:/GitHub-Repo/git-localconfig-repo/


#Remote Git config path

spring.cloud.config.server.git.uri=https://github.com/amir35/git-config-repo


8. Remember the property file name should be same as that of microservice name.

Here microservice name is "limits-service". So properties name present at Git repo should be
"limits-service.properties"


9. Now you can start the limits-service server and you can see it will try to fetch data from spring-cloud-config-server which in turn will fetch data from limits-service.properties file from Git repo.


10. Now suppose you want to change any property from limits-service.properties file, you don't need to start server again for limits-service project.

Just you need to commit the files in Git repo and then execute one refresh endpoint.


11. Open application.properties file in limits-service and add below line:

management.endpoints.web.exposure.include=*

this will enable all the endpoints.

Then after making changes in the property file run this endpoint for changes to reflect on the microservice.

http://localhost:8081/actuator/refresh



Then execute the normal Rest endpoints as before and changes will reflect.


Source Code: Limits-service

Sunday, 12 September 2021

How to Configure GitHub repository with Eclipse Project

First time? I also spent most part of the day today trying to get my Spring Boot project in Eclipse connected to GitHub and I finally got it to work, so I thought I’d share my observations to make it easier for other new learners.


When we work on any project. We want to have it stored online on Github so that it is available to be taken into other places. To make your Local Eclipse project configured with Github, follow the below steps to Configure it.

Step 1: Click on New Button to create a New Repository


Step 2: Enter any Repository name and Click on the "Create repository" button.


Step 3: Copy the URL for the repository, say gitURL.



Step 4: Open Eclipse. Click on the Search icon on the right side and Type Git Repo. You will get the below option. Choose "Git Repositories" under the View section.



Step 5: Paste the gitURL here and Click on "Next".




Step 6: Your repository will start reflecting.



Step 7: Now Create a new Spring Boot(or any other) project.



Step 8: Give the name and other required information for the project.



Step 9: Right Click on Project. Then Select Team -> Share project.



Step 10: Select the Repository that you have created from the dropdown.





Step 11: Click on Finish.


Step 12: Go to Git Staging section. Add all the uncommitted files. Give the proper Commit message and then Click on Commit and Push.



Step 13: Click on Preview. The Github login box will appear.



Step 14: Given username and Token as password.



Step 15: Click on the Push-button.



Step 16: Now push is done. Go to the Github repository and the committed files will reflect there.




How to Configure GIT Token to push changes

 Follow the Below Steps to Configure GIT to push changes to the repository:


Step 1: Log in to your Github account

Step 2: Click on your image at the top right corner for Menu.

Step 3: Select the Setting



Step 4: Click on "Developer Settings"



Step 5: Click on "Personal Access Token" and then "Generate new Token"



Step 6: Give a name for Token and set the Expiration period. 



Step 7: Select all the access you wants to give and then click on the "Generate Token" button




Step 8: A new generated Token will be available. Copy that Token and keep it at someplace. You will not be able to see the token again on this page.

Step 9: After doing any change, click on "Commit and Push" under Git Staging.





Step 10: You will get Login box. Here give "User" as your Git username and "Password" as the Token generated.



Step 11: The changes will be pushed to the Git repository.



Saturday, 11 September 2021

How to Resolve “Failed to Configure a DataSource” Error

How to Resolve Failed to Configure a DataSource” Error:




Suppose we have a Spring Boot project, and we've added the spring-data-starter-jpa dependency and a MySQL JDBC driver to our pom.xml.

Cause:

Spring Boot auto-configuration tries to configure the beans automatically based on the dependencies added to the classpath.

And, since we have the JPA dependency on our classpath, Spring Boot tries to automatically configure a JPA DataSource. The problem is, we haven't given Spring the information it needs to perform the auto-configuration.

For example, we haven't defined any JDBC connection properties, and we'll need to do so when working with external databases like MySQL and MSSQL. On the other hand, we won't face this issue with in-memory databases like H2 since they can create a data source without all this information.

So, If we are not using JPA or JDBC and still we are having this issue. We can do following things to get rid of this error:

The class DataSourceAutoConfiguration is the base class for configuring a data source using the spring.datasource.* properties.

First, we can disable the auto-configuration using the spring.autoconfigure.exclude property in our application.properties file:

application.properties

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration


application.yaml

spring: autoconfigure: exclude: - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration


Or, we can use the exclude attribute on our  @SpringBootApplication or @EnableAutoConfiguration annotation:

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})