一、概述

Spring Cloud基于Spring Boot,提供了多个组件来帮助企业进行微服务系统建设;它提供的主要功能有:服务注册中心/服务注册/服务调用/负载均衡/断路器等;一般情况下它都是使用已有的开源软件,在其基础上按Spring Boot的理念来进行封装,简化各种个组件的调用以及各个组件之间的交互。

二、常用组件

Spring Cloud主要包含以下常用组件:

1、 Eureka

分成两类,一是注册中心及EurekaServer,用于提供服务注册/服务申请等功能;一是被注册者及服务提供者EurekaClient,用于向EurekaServer注册服务并可从EurekaServer获取需要调用的服务地址信息;需要向外提供服务的应用,需要使用EurekaClient来向Server注册服务。

2、 Ribbon

负责进行客户端负载均衡的组件;一般与RestTemplate结合,在访问EurekaClient提供的服务时进行负载均衡处理。也就是说,Ribbon用于服务调用者向被调用者进行服务调用,并且如果服务者有多个节点时,会进行客户端的负载均衡处理;

3 、Feign

与Ribbon功能类型,用于调用方与被调用方的服务调用,同时进行客户端负载均衡的处理;不过它能提供类似本地调用的方式调用远程的EurekaClient提供的服务;它实际上是在Ribbon基础上进行了进一步的封装来提高调用服务的简便性。

三、项目目录结构

1、创建eureka-center(注册中心)

  • 步骤:在项目右键 new → module,选择Spring initializr然后next:

springcloud创建项目 springcloud 组建_java


springcloud创建项目 springcloud 组建_intellij-idea_02


springcloud创建项目 springcloud 组建_java_03

  • 配置文件更改(application.yml):
server:
  port: 8090

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

spring:
  application:
    name: eureka-center
  • 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eureka-center</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-center</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2021.0.3</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>

</project>
  • 启动类添加注解@EnableEurekaServer,启动代码验证是否成功

2、创建eureka-client(子模块)

  • 与创建center模块操作相同,不同点为选择的不再是Eureka Server,而是Eureka Discovery Client

springcloud创建项目 springcloud 组建_springcloud创建项目_04

  • 配置文件相关信息
server:
  port: 9090

eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8090/eureka/

spring:
  application:
    name: eureka-client
  • 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-client</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2021.0.3</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

</project>
  • 启动类添加注解@EnableEurekaClient,启动代码验证是否成功
  • 创建测试类,测试
package com.eurekaclient.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class TestController {
    @RequestMapping("/hello")
    public String hello() {
        return "hello world!!!";
    }
}

springcloud创建项目 springcloud 组建_java_05

3、创建eureka-gateway(网关)

  • 与创建eureka-client模块操作相同,不同点为在pom中添加maven依赖
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
  • 配置文件相关信息
server:
  port: 9091

eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8090/eureka/

spring:
  application:
    name: eureka-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true # 开启从注册中心动态创建路由的功能,利用微服务名称进行路由
      routes:
        - id: eureka-client
          uri: lb://eureka-client
          order: 8001
          predicates:
            - Path=/api/client/**
          filters:
            - StripPrefix=2
  • 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com</groupId>
    <artifactId>eureka-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-gateway</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2021.0.3</spring-cloud.version>
    </properties>
    <dependencies>
        <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-gateway</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>

</project>
  • 启动测试eureka-client创建的测试类,是否经过网关进行转发,通过/api/client转发到client模块中

4、项目目录整体结构

springcloud创建项目 springcloud 组建_spring_06

  • 父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>org.example</groupId>
    <artifactId>SpringCloudDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>eureka-center</module>
        <module>eureka-client</module>
        <module>eureka-gateway</module>
    </modules>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • modules中为各个模块的服务名

springcloud创建项目 springcloud 组建_spring_07