从零搭建自己的SpringCloud项目,及SpringBoot升级到SpringCloud填坑

从零搭建自己的SpringCloud项目,及SpringBoot升级到SpringCloud填坑

首先我们的项目构成是这个样子的,一个单体的Springboot项目。迫于需求越来越多,准备升级到SpringCloud,目前用到了SpringCloud的三个组件。eureka-注册中心,admin-监控面板,gateway-api网关。

因为我们以前是springboot项目,升级到springcloud涉及到springboot的版本,spring的版本、mybatis的版本等等,中间遇到了N多的坑,后边一一道来。特殊说明:

因为我们用到了springcloud的网关是gateway而非zuul,所以springboot的版本必须的再2.0.x以上。而gateway比zuul的优势我只了解到每秒接收的请求gateway是zuul的1.6倍之上,欢迎大家补充

1、首先搭建注册中心:eureka

启动类如图:

从零搭建自己的SpringCloud项目,及SpringBoot升级到SpringCloud填坑

pom文件:

Springcloud的版本我用的是:

Finchley,没有用Greenwich,也没有用edage。为什么没用?呵呵因为gateway,并且springboot的版本只能是2.0.6,2.1.0版本的springboot搭建eureka和admin没问题,gateway一直不能运行 后来又降到2.0.6。

此处想特殊说明,我查阅了好多springcloud的东西,感觉好多公司没采用springcloud可能也有这点原因,版本管理太乱了,比如springcloud的组件用什么版本对应的springboot用那个版本,spring用那个版本没有一个明确的说明,我在升级的过程中耽误了好多时间 感觉像一个一个试出来的,欢迎大家给提意见有没有好的渠道来区分。

<?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>
    <artifactId>hht-operations-gateway</artifactId>
    <parent>
        <groupId>honghe</groupId>
        <artifactId>hht-operations-parent</artifactId>
        <version>1.0.0</version>
    </parent>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
        <!--<spring-cloud.version>Greenwich.M3</spring-cloud.version>-->
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.springframework.cloud</groupId>-->
            <!--<artifactId>spring-cloud-starter-eureka-server</artifactId>-->
            <!--<version>2.1.0.RELEASE</version>-->
        <!--</dependency>-->

        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-test</artifactId>-->
            <!--<version>2.0.6.RELEASE</version>-->
            <!--<scope>test</scope>-->
        <!--</dependency>-->

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

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>*.properties</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>

运行效果:

从零搭建自己的SpringCloud项目,及SpringBoot升级到SpringCloud填坑

2、搭建监控面板-admin

启动类:

从零搭建自己的SpringCloud项目,及SpringBoot升级到SpringCloud填坑

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>
    <artifactId>hht-operations-admin</artifactId>
    <parent>
        <groupId>honghe</groupId>
        <artifactId>hht-operations-parent</artifactId>
        <version>1.0.0</version>
    </parent>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.0.5</version>
        </dependency>

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server-ui</artifactId>
            <version>2.0.5</version>
        </dependency>

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

        <!--添加spring-cloud依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <resources>
            <!--整体打包时注释-->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>application.properties</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.honghe.base.AdminApplication</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>
                            jar-with-dependencies
                        </descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <compilerArguments>
                        <bootclasspath>${java.home}\lib\rt.jar;${java.home}\lib\jce.jar</bootclasspath>
                    </compilerArguments>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

运行效果:

从零搭建自己的SpringCloud项目,及SpringBoot升级到SpringCloud填坑

3、网关-gateway

启动类:

从零搭建自己的SpringCloud项目,及SpringBoot升级到SpringCloud填坑

配置文件:重点重点重点。网上大部分的配置文件都是yml,我的是application.properties。里边的配置有一些却别

从零搭建自己的SpringCloud项目,及SpringBoot升级到SpringCloud填坑

说明:

至此,我们所有的模块都由8433进行转发,前端请求后端只有这一个端口。我们根据lb:xxx从注册中心获取服务,根据与前端约定的唯一标识进行接口的转发。

如:

从零搭建自己的SpringCloud项目,及SpringBoot升级到SpringCloud填坑

注册中心有gateway-server的服务,现在前端通过8433(网关的端口请求)从零搭建自己的SpringCloud项目,及SpringBoot升级到SpringCloud填坑,就会转发到gateway-server服务上的hello接口。

总结:目前我只搭建了springcloud的三个组件,将我们的项目从springboot升级到了springcloud。中间遇到的坑:

springcloud的版本采用了Finchley。

springboot的版本从1.5.7升级到了2.0.6

spring的版本升级到了5.1.0,

后续再增加别的组件再更新本文。

未完,待续。。。。。。