sidd
sidd

Reputation: 305

Spring Boot microservices dependency

In my project I have a lot of microservices. Some of the microservices are dependent on each other. for e.g. microservice X is dependent on microservice Y and currently I have that dependency in microservice X's pom.xml as below:

(microservice Y)

<dependency>
      <groupId>com.myproject</groupId>
      <artifactId>integration-framework</artifactId>
      <version>0.0.1</version>
 </dependency>

I do not want to have my dependency in pom.xml because for e.g. if I change the version of Spring Boot in microservice X from 1.5.x to 2.x then it's having a ripple effect in mircoservice Y. I have to change the Spring Boot version in microservice Y as well.

Do I have any other option for e.g. service discovery?

Please kindly advise and help me.

Upvotes: 0

Views: 1664

Answers (1)

gdaly
gdaly

Reputation: 114

You can achieve this, if I understand correctly, by playing with pom inheritance.

Assume you have a main project with a main .pom, which is called super pom (let's call it Z), packaged as a pom. All your microservices will be in separate modules in the project, inheriting this super pom.

Put everything in common between every of you microservices in Z, like version properties, common dependencies, etc... and define a module section containing all your subprojects X and Y

Z super pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <!-- if you have a parent pom on top of it -->
    </parent>

    <groupId>my.awesome.project</groupId>
    <artifactId>z</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Z</name>
    <description>Z</description>
    <packaging>pom</packaging> <!-- THIS IS IMPORTANT -->

    <properties>
        <springbootVersion>2.3.2.RELEASE</springbootVersion>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- your COMMON dependencies -->
    </dependencies>

    <modules>
        <module>x</module>
        <module>y</module>
    </modules>
</project>

X or Y children pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent> <!-- MATCHING SUPER POM !! -->
        <groupId>my.awesome.project</groupId>
        <artifactId>z</artifactId>
        <version>0.0.1-SHAPSHOT</version>
    </parent>

    <artifactId>x</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>X</name>
    <description>X</description>
    <packaging>jar</packaging>

    <!-- You can only put one parent pom, and it is taken by our super pom, so we add spring-boot dependencies using dependencyManagement to solve this issue -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${springbootVersion}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- YOUR DEPS (but specific to X/Y) -->
    </dependencies>

    <build>
        <plugins>
           <!-- YOUR PLUGINS -->
        </plugins>
    </build>

</project>

Upvotes: 2

Related Questions