Hand in hand to teach you how to build a spring cloud project

IDE development tool: IntelliJ IDEA 14.0.2

Version management: Maven

Technology stack: spring cloud

Environment: JDK 1.8

 

1, Create Maven project


1. File - > new project - > maven, as shown in the figure:


File->New Project

2. Fill in module name and project path

Follow these steps to create a Maven project.

At this time, the project is not a spring boot project!!

2, Turn maven project into spring boot project


1. The jar package needed for pom.xml introduction

Note: according to the actual situation of each project, the owner of the building is the private warehouse of maven

  • Introducing the jar package required by SpringBoot
  • jar package required to introduce spring cloud
  • Introducing ereka service to register and discover the jar package needed by the client
  • Introducing mybatis spring cloud dependent jar package
  • Introducing the jar package required by kafka
  • The jar package required for the introduction of redis
  • The introduction of configuration center Spring config client depends on jar package and so on, according to the needs of each project.
<?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>
 
    <groupId>com.**inks.e**s</groupId>
    <artifactId>msg</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
 
    <name>msg</name>
    <description>Demo project for Spring Boot</description>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <tomcat.version>8.5.30</tomcat.version>
    </properties>
 
    <dependencies>
        <!--spring-cloud-config introduce -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--spring-cloud Monitoring function is introduced, which can be used to refresh configuration files dynamically -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!-- introduce feign rely on -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <!-- kafka rely on -->
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>
        <!-- redis rely on -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!-- Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--mybatis to configure-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.epaylinks.efps</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.epaylinks.efps</groupId>
            <artifactId>logtracer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
 
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-math</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpasyncclient</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>3.2.2</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>3.2.2</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>src/main/resources/generatorConfig-mch.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>com.oracle</groupId>
                        <artifactId>ojdbc6</artifactId>
                        <version>11.1.0.7.0</version>
                    </dependency>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.2</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>


2. Create the main entry class of spring cloud project * * Application

 

package com.**s.**s;
 
import com.**ks.e**.common.util.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;
 
@RefreshScope
@EnableEurekaClient
@EnableFeignClients
@EnableKafka
@EnableScheduling
@EnableAspectJAutoProxy(proxyTargetClass=true , exposeProxy=true)
@SpringBootApplication
public class MsgApplication {
    // Note that since we inject RestTemplate into the controller, we need to instantiate an instance of this class at startup
    @Autowired
    private RestTemplateBuilder builder;
 
        // Use RestTemplateBuilder to instantiate the RestTemplate object. spring has injected the RestTemplateBuilder instance by default
    @Bean
    public RestTemplate restTemplate() {
        return builder.build();
    }
 
    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(MsgApplication.class);
        SpringContextUtils.setApplicationContext(springApplication.run(args));
    }
}


@The annotation of SpringBootApplication indicates that this project is a SpringBoot project, and this class is the main entry class of the project.

Start Application with the following main method

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(MsgApplication.class);
        SpringContextUtils.setApplicationContext(springApplication.run(args));
    }


By now, the spring boot project has been completed by more than half. Next is the configuration of database connection and configuration files such as registry config and Ereka

3, Registry and service discovery configuration
1. Reading configuration file of Spring Config Center Configuration Project

The building owner uses the Spring Config Center as the configuration center to configure various connections and configuration information needed to read the project (Spring Config Center will not be introduced in detail here)

2. GitLab remote hosting configuration information

After the Spring Config Center configures the connection and read permissions of the project, it configures all kinds of required information of the project on gitlab

See another article here

The spring cloud project uses gitLab as the configuration center

3. Load the corresponding configuration file information (dev, test, prod, uat) of each environment in the project

Take dev development environment as an example


Load in order of icons
 

All spring cloud projects start from the bootstrap.yml file to load various connection and configuration information required by the project. This is a built-in decision of spring cloud core. You can study the source code, which is not detailed here.

4. The configuration of bootstrap-dev.yml is as follows:

spring:
  application:
    name: msg
  cloud:
    config:
      uri: http://172.20 *. 4 *. 80:9000 / ා configure the url of the spring cloud config server
      profile: dev                      # Specify profile
      label: master                     # Specify the branch of the gitlab repository


It is mainly to connect the spring cloud config server to obtain the configuration information on the remote gitlab.

5. The configuration of application-dev.yml is as follows:

eureka:
  client:
    registerWithEureka: true
    service-url:
      defaultZone: http://172.20.4.80:8000/eureka/
swagger:
  enable: true


Mainly used to connect to eureka service registration and discovery

By now, the spring close project has been basically completed. The next step is to create tables for various databases, configure Mybatis, and update the code.

 


Pay attention to official account: the big vision of procedure

Feel helpful to you, pay attention to blog and official account. From time to time, we will share the latest cutting-edge technology framework and common technologies of bat plant, and from time to time, we will share live lectures of Daniel in the industry and obtain video courseware materials.

Tags: Programming Spring Maven Mybatis Apache

Posted on Sun, 17 May 2020 05:21:29 -0400 by kellz