2. Build the basic framework of springCloud-alibaba (producer/consumer)

development environment

idea2021.2
jdk 11
springcloud Hoxton.SR9
springboot 2.3.2.RELEASE

nacos注册中心部署可参考上一篇文章

Portal jump

1. Start building the environment cloudAlibaba

Build the parent project/common module

1. Use idea to create a maven parent project (used to manage version dependencies and unify version information)
pom.xml configuration information

    <properties>
        <spring.cloud.alibaba.version>2.2.6.RELEASE</spring.cloud.alibaba.version>
        <spring.boot.version>2.3.2.RELEASE</spring.boot.version>
        <spring.cloud.version>Hoxton.SR9</spring.cloud.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--Spring Cloud Alibaba 版本管理器-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring.cloud.alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--Spring Boot 版本管理器-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--Spring Cloud 版本管理器-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-openfeign-dependencies</artifactId>
                <version>2.2.6.RELEASE</version>
                <type>pom</type>
                <scope>pom</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

If necessary, you can also create a public function module, import the dependency packages that need to be used in the public module, and create a public tool class.
Such as lombok, mysql connection package, fastjson, commonly used general tool classes (submodules only need to introduce public modules)

nacos integrated producer

1. Create a maven producer sub-project under the parent module.
1. Maven dependency management sets the parent project as the parent project we started to create, depending on the coordinates of the parent project created by the individual

    <parent>
        <artifactId>alibabaCloud</artifactId>
        <groupId>com.xjt</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

2. Introduce nacos and springboot related dependencies in the producer sub-project.

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.xjt</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

The common package is a public module created by the author

  • Now build the project as a basic springboot project.
  • Add nacos configuration The
    configuration information is as follows
server:
  port: 8082
spring:
  application:
    name: provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8845

Corresponding to the port number, the service name registered to the nacos center (must be unique ( 服务调用会用到)), and the address of the nacos service registration center

  • After the configuration is complete, add an annotation to the main startup class @EnableDiscoveryClientto enable the service to register for discovery
    insert image description here

Since it is a service provider, it needs to provide some interfaces or services for consumers to use.
We can create a test interface and enable the project, and we can see the service we just registered in the nacos service management platform.
insert image description here

here. The service producer is built!

nacos integration service consumer

The service producer project can be copied directly

Add an additional dependency (for restful-style calls to the producer interface)

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
  1. Modify the configuration file, service application name.
  2. Create a configuration class, inject RestTemplate in the configuration class/or use @beanannotation injection directly in the main startup class, and use annotations @LoadBalanced, and add annotations to the main startup class to @EnableFeignClientsenable feign

insert image description here

  • Create an interface and add annotations to the interface class@FeignClient
  • This method is to use the feign method to call the producer service insert image description here
    using the restTemplate method
  • Create a class to add @Componentinjection to the spring container
  • Inject RestTemplateinsert image description here

The address can also be modified to the corresponding IP address port number plus the interface path.

The method to be called already exists, we will create an externally provided interface for testing, and the two interfaces corresponding to the two calling methods will be tested.
insert image description here

Consumer integration complete!

Producer and Consumer Testing

  1. Start the service producer and service consumer respectively.
  2. We visit the two interfaces provided by the consumer to see if we can call the method provided by the producer.
    insert image description here
    insert image description here

Called successfully!
The basic microservice project is built!

Guess you like

Origin blog.csdn.net/languageStudent/article/details/121192339