最近项目要用spring boot ,spring cloud 还有docker,在spring cloud这卡了很久,这篇博文做个笔记

1、因为项目是多模块的,这里需要建立一个maven工程,它的pom作为项目的父pom

1.1、建立一个空的maven工程

jenkinsfile springcloud多模块 springcloud多模块搭建_ci

jenkinsfile springcloud多模块 springcloud多模块搭建_maven_02

jenkinsfile springcloud多模块 springcloud多模块搭建_后端_03

jenkinsfile springcloud多模块 springcloud多模块搭建_maven_04

建立好以后就是上面这个样子,因为我们只需要pom文件做为父pom,规范子模块,因此应当删除src目录

jenkinsfile springcloud多模块 springcloud多模块搭建_maven_05

1.2、修改pom文件

但是怎么修改这个pom文件呢,我们第一次创建这个可能不太清楚,以为springcloud是基于springboot的,应当特别注意他两版本的兼容性,笔者第一次建立的时候就因为这个版本兼容性报错,浪费了好长时间

jenkinsfile springcloud多模块 springcloud多模块搭建_spring_06

为了解决这个问题,我们可以有更方便地方法,那就是用idea自带的spring initialzr工具建立一个springcloud工程,然后趴下它的pom文件

如下

jenkinsfile springcloud多模块 springcloud多模块搭建_maven_07

jenkinsfile springcloud多模块 springcloud多模块搭建_后端_08

jenkinsfile springcloud多模块 springcloud多模块搭建_ci_09

题外话 注意一点

jenkinsfile springcloud多模块 springcloud多模块搭建_spring_10

实际上刚入门的时候好多都是jar包的问题,找依赖的时候都可以用我这个方法

比如spring-cloud-starter-feign依赖已改为spring-cloud-starter-openfeign,用我这个方法很容易知道这一点

接着上面的步骤继续

jenkinsfile springcloud多模块 springcloud多模块搭建_ci_11

下面就是他的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>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.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>
        <spring-cloud.version>Greenwich.M3</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>


</project>

然后我们再试着编写我们的pom文件

以为是父pom,规范子模块用的所以它的jar因改为pom

还有他的springboot版本为2.1.0.RELEASE

springcloud版本为Greenwich.M3

ok

下面就是我们最终的父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>

    <groupId>com.whale.demo</groupId>
    <artifactId>springCloud</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.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>
        <spring-cloud.version>Greenwich.M3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

这个是一定要写的,禁止获取快照组件,

否则后面我们需要依赖的cloud的包会报unkonwn错误

2、然后创建2个model工程:
一个model工程作为服务注册中心,即Eureka Server,
另一个作为Eureka Client。
下面以创建server为例子,详细说明创建过程:

2.1、Eureka Server的创建

jenkinsfile springcloud多模块 springcloud多模块搭建_后端_12

jenkinsfile springcloud多模块 springcloud多模块搭建_后端_13

jenkinsfile springcloud多模块 springcloud多模块搭建_spring_14

jenkinsfile springcloud多模块 springcloud多模块搭建_spring_15

也就是上面我们为了获取父pom的操作

贴出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>

    <groupId>com.example</groupId>
    <artifactId>eurekaserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eurekaserver</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.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>
        <spring-cloud.version>Greenwich.M3</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>


</project>

因为我们新建spring moudel时,他没有自动继承我们的父pom,所以关键还是要修改它的 pom,让他继承我们的父pom然后删掉父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>

    <groupId>com.example</groupId>
    <artifactId>eurekaserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eurekaserver</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.whale.demo</groupId>
        <artifactId>springCloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
    
</project>

相当于这个子pom文件只是引入了spring-cloud-starter-netflix-eureka-server的依赖

最后我们的父pom下应该多一个元素

<modules>
    <module>eurekaserver</module>
</modules>

2.2、启动一个服务注册中心

只需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动application类上加

@SpringBootApplication
@EnableEurekaServer
public class EurekaserverApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaserverApplication.class, args);
    }
}

2.3、eureka server的配置文件appication.yml

eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),
在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。
eureka server的配置文件appication.yml如下

我们还徐要改下配置文件的后缀为yml

jenkinsfile springcloud多模块 springcloud多模块搭建_maven_16

server:
  port: 8765

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: eurka-server

通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.

2.4、运行

然后运行启动类EurekaserverApplication

jenkinsfile springcloud多模块 springcloud多模块搭建_spring_17

eureka server 是有界面的,启动工程,打开浏览器访问:
http://localhost:8765 ,界面如下

jenkinsfile springcloud多模块 springcloud多模块搭建_ci_18

No application available 没有服务被发现 ……_

因为没有注册服务当然不可能有服务被发现了。

3、创建一个服务提供者 (eureka client)

当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。

创建过程同server类似,创建完pom.xml如下:

<?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.example</groupId>
    <artifactId>service-hi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>service-hi</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.whale.demo</groupId>
        <artifactId>springCloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
    
</project>

父pom引入module

<modules>
    <module>eurekaserver</module>
    <module>service-hi</module>
</modules>

通过注解@EnableEurekaClient 表明自己是一个eurekaclient.

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceHiApplication.class, args);
    }

    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }

}

仅仅@EnableEurekaClient是不够的,还需要在配置文件中注明自己的服务注册中心的地址,application.yml配置文件如下:

server:
? port: 8768

spring:
? application:
? ? name: service-hi

eureka:
? client:
? ? serviceUrl:
? ? ? defaultZone: http://localhost:8765/eureka/

需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。
启动工程,打开http://localhost:8765,即eureka server 的网址:

jenkinsfile springcloud多模块 springcloud多模块搭建_后端_19

你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI

这时打开http://localhost:8768/hi?name=whale,你会在浏览器上看到 :

jenkinsfile springcloud多模块 springcloud多模块搭建_后端_20