How to create Maven aggregation project (build microservice project)

Keywords: Maven Spring Cloud Microservices

1. Build an aggregation project in idea. Both the parent project and the child project are module s. In the pom file of the parent project, do the following:

In the 1.1 POM file, unified version management is configured

<dependencyManagement>
        <dependencies>
            <!--spring boot Core dependent version definition(spring Official definition)-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--Spring Cloud Microservice specification(from spring Official definition)-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR9</version>
                <type>pom</type><!--If scope yes import,type Must be pom-->
                <scope>import</scope><!--Introducing third-party dependent version design-->
            </dependency>

            <!--Spring Cloud alibaba Dependent version management (Refer to official instructions)-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

1.2 add public dependencies required by all subprojects in the POM file to avoid repeated introduction of dependencies in subprojects, mainly including lombok and test dependencies

<!--Step 2: Add the required public dependencies of the subproject-->
    <dependencies>
        <!--lombok rely on,If necessary in the subproject lombok,No need to import-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope><!--provided Indicates that this dependency is only valid at compile time-->
        </dependency>
        <!--Unit test dependency,When unit test is required in subproject,There is no need to introduce this dependency again-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope><!--test Indicates that only test Use this dependency under the directory-->
            <exclusions>
                <exclusion><!--Eliminate unnecessary dependencies-->
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--Other dependencies...-->
    </dependencies>

1.3 compile and run versions of all subprojects of the specification

<!--Step 3: Define the unified compilation and running version of the current project module and subproject-->
    <build><!--Project build configuration,We are based on maven Complete the compilation of the project,test,Packaging and other operations,
    All based on pom.xml Complete the operation of this column,However, the compiled and packaged configurations are written to build element
    Internal,The specific compilation and packaging configuration,Need again plugin To achieve,plugin Element is not required,maven
    There is a default plugin to configure,Common plug-ins can be viewed in the local library-->
        <plugins>
            <!--adopt maven-compiler-plugin Plug in settings item
            Unified jdk Compile and run versions-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <!--If the local library does not have this version,A red font error will appear here-->
                <version>3.8.1</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

1.3.1 why configure jdk to compile a unified version

Then why should we configure a unified compiled and running version? Because the jdk version is downward compatible, we can use the higher version to compile the lower version of. java files, but if the function higher than the compiled version is used in a sub project, it cannot be compiled into. class files, and it cannot run

2. Test whether each service request is normal

Dependency needs to be introduced in advance

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

Then write a simple controller to test the web request

@RestController
public class HelloController {
    @GetMapping("/hello/{msg}")
    public String sayHello(@PathVariable String msg) {
        return "sayHello:" + msg;
    }
}

Successful, how to realize the communication between various modules?

2.1 introducing the dependency of tool engineering on the caller

<dependency>
            <groupId>com.jt</groupId>
            <artifactId>sca-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
import common.MyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @GetMapping("/hello/{msg}")
    public String sayHello(@PathVariable String msg) {
        return MyUtils.sayHello(msg);
    }
}

OK, the call succeeded! However, in the future, if we want to realize such an evil demand: there are two services, namely, order service and payment service. In the payment process of an order, we need to call the interface of the payment service instead of a simple call method. This is the next step to realize the service call between sub projects. At this time, we need to use nacos. Let's take a look at the definition of nacos, Look what he's used for

3.Nacos micro service configuration center

3.1 attach a note on the requirements of noces version first, because the version of nacos depends on the version of spring cloud, which corresponds to each other

Version Description · Alibaba / spring cloud Alibaba wiki · GitHub

3.2 modify the default sql, add this paragraph at the beginning, and then import the current sql database

DROP DATABASE IF EXISTS `nacos_config`;
CREATE DATABASE `nacos_config` DEFAULT character set utf8mb4;
SET names utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
USE `nacos_config`;

3.3 start nacos

Introduction: now that we have these subprojects, we need a unified configuration management center to manage the communication between subprojects (services)

3.3.1 we need to define a @ Bean in startup to register a restTemplate globally, which can be used to access interfaces of other services. At the same time, if our callee has multiple service instances, you can add the @ Balanced annotation to the restTemplate and use the following methods to achieve load balancing

@GetMapping("/consumer/doRestEcho3")
public String doRestEcho03(){
    String url=String.format("http://%s/provider/echo/%s","sca-provider",appName);
    //Send an http request to the service provider to obtain the response data
    return loadBalancedRestTemplate.getForObject(
            url,//The address of the service to request
            String.class);//String.class is the response result type of the requested service
}

Posted by arkleyjoe on Fri, 22 Oct 2021 20:15:30 -0700