手把手教你搭建SpringCloud项目

时间:2022-07-22
本文章向大家介绍手把手教你搭建SpringCloud项目,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

IDE开发工具:IntelliJ IDEA 14.0.2

版本管理:Maven

技术栈:SpringCloud

环境:JDK 1.8

一、创建Maven项目

1、File——>New Project ——>Maven 如图所示:

File——>New Project

2、填写模块名称和项目路径

按照以上步骤,就简单的创建了一个Maven项目。

此时项目还不是SpringBoot项目!!

二、把maven项目变成SpringBoot项目

1、pom.xml引入需要的jar包

注意:按照各自项目实际情况;楼主是本项目由自己的maven私库

引入SpringBoot所需jar包

引入SpringCloud所需jar包

引入ereka服务注册发现客户端所需jar包

引入mybatis-SpringCloud依赖jar包

引入kafka 所需jar包

引入redis 所需jar包

引入配置中心Spring config 客户端依赖jar包

等等,按照各自项目所需。

<?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引入 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--spring-cloud监控功能引入,可用于动态刷新配置文件 -->
        <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>
        <!-- 引入feign依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <!-- kafka依赖 -->
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>
        <!-- redis依赖 -->
        <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配置-->
        <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、创建SpringCloud项目主入口类**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 {
  // 启动的时候要注意,由于我们在controller中注入了RestTemplate,所以启动的时候需要实例化该类的一个实例
    @Autowired
    private RestTemplateBuilder builder;
 
        // 使用RestTemplateBuilder来实例化RestTemplate对象,spring默认已经注入了RestTemplateBuilder实例
    @Bean
    public RestTemplate restTemplate() {
        return builder.build();
    }
 
    public static void main(String[] args) {
      SpringApplication springApplication = new SpringApplication(MsgApplication.class);
        SpringContextUtils.setApplicationContext(springApplication.run(args));
    }
}

@SpringBootApplication 这个标注就表示,这个项目是SpringBoot项目,并且此类是项目的主入口类。

由以下main方法启动Application

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

到这里,SpringBoot项目已经完成一大半。接下来就是数据库连接配置以及注册中心config和Ereka等配置文件的配置了

三、注册中心和服务发现配置等

1、Spring Config Center配置项目的配置文件读取

楼主这里是以Spring Config Center作为配置中心来,配置读取项目所需的各种连接和配置信息等(Spring Config Center这里不作详细介绍)

2、GitLab远程托管配置信息

在Spring Config Center配置服务中心配置好了项目的连接和读取权限后,在gitlab上配置本项目的各种所需信息

这里详见另一篇文章

SpringCloud项目采用gitLab作为配置中心

3、项目中加载各环境下对应的配置文件信息(dev、test、prod、uat)

这里以dev开发环境为例

所有SpringCloud项目均是从bootstrap.yml文件开始加载项目所需的各种连接和配置信息的,这是SpringCloud核心内置决定,可以去研究源码,这里不作详述。

4、bootstrap-dev.yml的配置如:

spring:
  application:
    name: msg
  cloud:
    config:
      uri: http://172.20*.4*.80:9000/   # 配置spring cloud config服务端的url
      profile: dev                      # 指定profile
      label: master                     # 指定gitlab仓库的分支

主要是连接spring cloud config服务端,以获取远程gitlab上的配置信息。

5、application-dev.yml的配置如下:

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

主要用来连接eureka服务注册和发现

到这里,SpringCLoud项目基本已经完成。接下来就是各种数据库建表Mybatis配置和撸代码的工作了。