Spring Cloud 基础框架搭建

1.架构预览

A01.png

 上面的架构中,主要包括了以下节点:

1.FEBS-Register:微服务注册中心,用于统一各个微服务的注册与发现

2.FEBS-Gateway:  微服务网关,统一处理外部请求,是客户端和众多微服务连接的桥梁

3.FEBS-Auth: 微服务认证服务器,用于令牌(Token)生成和令牌校验,是整个权限系统的核心所在

4.FEBS-Server-System: 微服务提供者(资源服务器)A,对外提供系统模块的CURD服务

5.FEBS-Server-Test: 微服务提供者(资源服务器)B

由于模块比较多,所以对各个微服务的端口做出如下约定:

微服务端口号
FEBS-Register8001
FEBS-Auth8101
FEBS-Server-System8201
FEBS-Server-Test8202
FEBS-Gateway8301

2.Maven父模块搭建

首先我们使用IDEA创建一个名称为FEBS-Cloud的 Maven模块,该模块为整个工程的服务模块,用于聚合各个微服务子系统。

在D盘根目录创建一个名称为febs的文件夹,然后打开IDEA,点击Create New Project新建一个Maven项目,Project SDK选择JDK 1.8:

1.png

 点击Next,如下图所示填写GroupId和ArtifactId:

 

2.png

 点击Next,按照下图所示填写相关内容,路径选择D盘根目录下的febs:

3.png

 因为febs-cloud模块是项目的父模块,仅用于聚合子模块,所以我们可以把src目录下的内容全部删了,保留pom.xml和febs-cloud.iml,然后修改pom.xml,引入Spring Boot和Spring Cloud:

<?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>cc.mrbird</groupId>
    <artifactId>febs-cloud</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>FEBS-Cloud</name>
    <description>FEBS-Cloud:Spring Cloud,Spring Security OAuth2 微服务权限管理系统</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <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>
</project>

3.通用模块搭建

通用模块主要用于定义一些各个微服务通用的实体类,工具类或者第三方依赖等。

点击File -> New -> Module...,新建一个Maven模块,Module SDK选择JDK 1.8:

5.png

6.png

 父模块选择我们上面创建好的febs-cloud,ArtifactId填febs-common,然后点击Next:

7.png

 febs-common模块的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">
    <parent>
        <artifactId>febs-cloud</artifactId>
        <groupId>cc.mrbird</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../febs-cloud/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>febs-common</artifactId>
    <name>FEBS-Common</name>
    <description>FEBS-Common通用模块</description>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.51</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-oauth2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-security</artifactId>
        </dependency>
    </dependencies>
</project>

 4.微服务注册中心模块搭建

微服务注册中心的作用就是用于统一管理微服务实例,微服务间的调用只需要知道对方的服务名,而无需关注具体的IP和端口,便于微服务架构的拓展和维护

我使用的是Eureka构建微服务注册中心,因为Eureka相对简单一点,无须启动第三方服务,只需要引入相关依赖即可

在IDEA的菜单栏中点击File -> New -> Modules...,新建一个Spring Boot项目,模板选择Spring Initializr,Module SDK选择JDK 1.8:

11.png

 点击Next,然后按照下图所示填写相关内容:

12.png

 继续点击Next,在依赖列表中,搜索Eureka Server,然后添加进去:

13.png

 继续点击Next,填写模块名称和路径:

14.png

 使用Spring Initializr构建的是一个Spring Boot应用,其父模块是spring-boot-starter-parent,而我们项目的父模块是上一节中构建的febs-cloud,所以我们调整一下febs-register模块的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>
    <parent>
        <artifactId>febs-cloud</artifactId>
        <groupId>cc.mrbird</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../febs-cloud/pom.xml</relativePath>
    </parent>

    <artifactId>febs-register</artifactId>
    <name>FEBS-Register</name>
    <description>FEBS-Cloud服务注册中心</description>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

接着修改febs-cloud模块的pom,在modules标签里添加上刚刚创建的febs-register模块:

<modules>
    <module>../febs-common</module>
    <module>../febs-register</module>
</modules>

打开febs-register的入口类FebsRegisterApplication,在类上使用@EnableEurekaServer标注,用以开启Eureka服务端功能:

@EnableEurekaServer
@SpringBootApplication
public class FebsRegisterApplication {

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

然后开始编写项目配置文件,由于我个人比较习惯yml格式的配置,所以我将resources目录下的application.properties重命名为application.yml,在该配置文件中编写如下内容:

server:
  port: 8001
  servlet:
    context-path: /register

spring:
  application:
    name: FEBS-Register

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    instance-info-replication-interval-seconds: 30
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}${server.servlet.context-path}/eureka/

 各项配置的具体含义:

  • spring.application.name,定义服务名称为FEBS-Register;

  • eureka.instance.hostname,指定了Eureka服务端的地址,因为我们是在本地搭建的,所以填写为localhost即可;

  • eureka.client.register-with-eureka,表示是否将服务注册到Eureka服务端,由于我们这里是单节点的Eureka服务端,所以这里指定false;

  • eureka.client.fetch-registry,表示是否从Eureka服务端获取服务信息,因为这里是单节点的Eureka服务端,并不需要从别的Eureka服务端同步服务信息,所以这里设置为false;

  • eureka.client.instance-info-replication-interval-seconds,微服务更新实例信息的变化到Eureka服务端的间隔时间,单位为秒,这里指定为30秒(这就是微服务启动后,要过一会才能注册到Eureka服务端的原因)。

  • eureka.client.serviceUrl.defaultZone,指定Eureka服务端的地址,这里为当前项目地址,即http://localhost:8001/register/eureka/

 至此,一个简单的微服务注册中心搭建好了,我们运行入口类FebsRegisterApplicationmain方法启动项目,启动后访问 http://localhost:8001/register/,出现Eureka页面说明微服务注册中心搭建成功,目前还没有微服务实例注册进来,所以列表是空的。

5.微服务认证中心模块搭建

我们一般希望搭建的各个微服务系统是受保护的,只有通过合法的认证信息才能访问相关资源,所以在这一节中,我们将借助Spring Cloud OAuth和Spring Cloud Security搭建一个统一给微服务发放访问令牌的认证服务器febs-auth。

在微服务架构下,我们通常根据不同的业务来构建不同的微服务子系统,各个子系统对外提供相应的服务。客户端除了浏览器外,还可能是手机App,小程序等。在微服务架构出现之前,我们的系统一般为单体模式,客户端只是单一的浏览器,所以通常情况下都是通过Session进行客户端,服务端通信,而随着客户端种类越来越多,这种交互方式变得越来越困难,于是OAuth协议应运而生。

OAuth是一种用来规范令牌(Token)发放的授权机制,目前最新版本为2.0,其主要包含了四种授权模式:授权码模式、简化模式、密码模式和客户端模式。Spring Cloud OAuth对这四种授权模式进行了实现。由于我们的前端系统是通过用户名和密码来登录系统的,所以这里只介绍密码模式。

密码模式简介:

 在密码模式中,用户向客户端提供用户名和密码,客户端通过用户名和密码到认证服务器去获取令牌,流程如下图:

18.png

 

  • Resource Owner,资源所有者,即当前正在使用系统的用户;
  • Client,客户端,比如浏览器,App等;
  • Authorization server,认证服务器,提供认证服务,并发放访问令牌。

 

如上图所示,密码模式包含了三个步骤:

  1. 用户向客户端提供用户名和密码;
  2. 客户端向认证服务器换取令牌;
  3. 认证服务器发放令牌。

其中第2步客户端发出的HTTP请求,包含以下参数:

  • grant_type:授权类型,此处的值固定为password,必选项。
  • username:用户名,必选项。
  • password:密码,必选项。
  • scope:权限范围,可选项。

在了解了OAuth协议和密码模式后,我们开始搭建认证服务器。点击IDEA菜单栏 File -> New -> Module...,因为认证服务器也是一个Spring Boot应用,所以模板选择Spring Initializr,Module SDK选择JDK 1.8:

19.png

 点击Next,按照下图所示填写相关内容:

20.png

 点击Next,在依赖选择过程中,我们先不选择任何依赖,直接继续点击Next:

21.png

 修改febs-auth模块的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>
    <parent>
        <groupId>cc.mrbird</groupId>
        <artifactId>febs-cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../febs-cloud/pom.xml</relativePath>
    </parent>

    <artifactId>febs-auth</artifactId>
    <name>FEBS-Auth</name>
    <description>FEBS-Cloud认证服务器</description>

    <dependencies>
        <dependency>
            <groupId>cc.mrbird</groupId>
            <artifactId>febs-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

修改febs-cloud模块的pom,在modules标签里引入febs-auth:

<modules>
    <module>../febs-common</module>
    <module>../febs-register</module>
    <module>../febs-auth</module>
</modules>

接着编写配置文件application.yml,内容如下所示:

server:
  port: 8101

spring:
  application:
    name: FEBS-Auth

eureka:
  instance:
    lease-renewal-interval-in-seconds: 20
  client:
    register-with-eureka: true
    fetch-registry: true
    instance-info-replication-interval-seconds: 30
    registry-fetch-interval-seconds: 3
    serviceUrl:
      defaultZone: http://febs:123456@localhost:8001/register/eureka/

各项配置含义如下:

  • eureka.instance.lease-renewal-interval-in-seconds,向Eureka 服务端发送心跳的间隔时间,单位为秒,用于服务续约。这里配置为20秒,即每隔20秒向febs-register发送心跳,表明当前服务没有宕机;
  • eureka.client.register-with-eureka,为true时表示将当前服务注册到Eureak服务端;
  • eureka.client.fetch-registry,为true时表示从Eureka 服务端获取注册的服务信息;
  • eureka.client.instance-info-replication-interval-seconds,新实例信息的变化到Eureka服务端的间隔时间,单位为秒;
  • eureka.client.registry-fetch-interval-seconds,默认值为30秒,即每30秒去Eureka服务端上获取服务并缓存,这里指定为3秒的原因是方便开发时测试,实际可以指定为默认值即可;
  • eureka.client.serviceUrl.defaultZone,指定Eureka服务端地址。

6.微服务网关模块搭建:

 在微服务的架构中,服务网关就是一个介于客户端与服务端之间的中间层。在这种情况下,客户端只需要跟服务网关交互,无需调用具体的微服务接口。这样的好处在于,客户端可以降低复杂性,无需关注具体是哪个微服务在提供服务。我将使用Spring Cloud Zuul搭建微服务网关febs-gateway。

点击IDEA菜单栏 File -> New -> Module...,模板选择Spring Initializr,Module SDK选择JDK 1.8:

35.png

 点击Next,按照下图所示填写相关内容:

36.png

 点击Next,在依赖列表里选择Zuul:

37.png

 点击Next:

38.png

 修改febs-gateway模块的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>
    <parent>
        <groupId>cc.mrbird</groupId>
        <artifactId>febs-cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../febs-cloud/pom.xml</relativePath>
    </parent>

    <artifactId>febs-gateway</artifactId>
    <name>FEBS-Gateway</name>
    <description>FEBS-Gateway微服务网关</description>

    <dependencies>
        <dependency>
            <groupId>cc.mrbird</groupId>
            <artifactId>febs-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

修改febs-cloud模块的pom,在modules标签里引入febs-gateway:

<modules>
    <module>../febs-common</module>
    <module>../febs-register</module>
    <module>../febs-auth</module>
    <module>../febs-gateway</module>
</modules>

在febs-gateway的入口类FebsGatewayApplication上添加@EnableDiscoveryClient注解,开启服务注册与发现,添加@EnableZuulProxy注解,开启Zuul服务网关功能:

@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
public class FebsGatewayApplication {

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

接着编写配置文件application.yml,先添加如下内容:

server:
  port: 8301

spring:
  application:
    name: FEBS-Gateway

eureka:
  instance:
    lease-renewal-interval-in-seconds: 20
  client:
    register-with-eureka: true
    fetch-registry: true
    instance-info-replication-interval-seconds: 30
    registry-fetch-interval-seconds: 3
    serviceUrl:
      defaultZone: http://febs:123456@localhost:8001/register/eureka/

接着在application.yml继续添加和Zuul有关的配置,内容如下所示:

zuul:
  routes:
    auth:
      path: /auth/**
      serviceId: FEBS-Auth
      sensitiveHeaders: "*"
  retryable: true
  ignored-services: "*"
  ribbon:
    eager-load:
      enabled: true

ribbon:
  ReadTimeout: 3000

这一段配置意思是,所有以/auth开头的请求都会被转发到名称为FEBS-Auth的服务上,由于我们需要在请求头中携带令牌,所以sensitiveHeaders设置为*,表示不过滤请求头信息,即请求的请求头信息将原封不动的转发出去。此外,因为Zuul已经包含了ribbon和hystrix依赖,所以我们在使用Zuul的同时,可以添加ribbon和hystrix相关配置。

  • zuul.retryable,设置为true时,表示开启重试机制;
  • zuul.ignored-services,Zuul配合Eureka后会有一套默认的配置规则,这里我们只想请求根据我们显示配置的路由规则走,所以设置为*,表示关闭所有默认路由配置规则;
  • zuul.ribbon.eager-load.enabled,Zuul内部通过Ribbon按照一定的负载均衡算法来获取服务,Ribbon进行客户端负载均衡的Client并不是在服务启动的时候就初始化好的,而是在调用的时候才会去创建相应的Client,所以第一次调用的耗时不仅仅包含发送HTTP请求的时间,还包含了创建RibbonClient的时间,这样一来如果创建时间速度较慢,同时设置的超时时间又比较短的话,第一次请求很容易出现超时的情况。设置为true的时候表示开启Ribbon的饥饿加载模式,即在应用启动的时候就去获取相应的Client备用。
  • ribbon.ReadTimeout,设置请求超时时间,单位为毫秒;

7. 资源服务器模块搭建

我在此只阐述一个资源服务器的搭建,多个资源服务器搭建过程类似

因为存在多个微服务提供者,所以我们先新增一个febs-server作为这些微服务的父项目,统一进行管理。

点击IDEA菜单栏 File -> New -> Module...新增一个Maven模块:

43.png

点击Next,父模块选择FEBS-Cloud,ArtifactId为febs-server:

44.png

 点击Next,模块名称和路径按照下图所示填写:

45.png

 修改febs-server的pom,引入febs-common模块:

<?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>

    <parent>
        <groupId>cc.mrbird</groupId>
        <artifactId>febs-cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../febs-cloud/pom.xml</relativePath>
    </parent>

    <artifactId>febs-server</artifactId>
    <packaging>pom</packaging>
    <name>FEBS-Server</name>
    <description>FEBS-Server服务提供模块</description>

    <dependencies>
        <dependency>
            <groupId>cc.mrbird</groupId>
            <artifactId>febs-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

febs-cloud模块的pom文件修改:

<modules>
    <module>../febs-common</module>
    <module>../febs-register</module>
    <module>../febs-auth</module>
    <module>../febs-gateway</module>
    <module>../febs-server</module>
</modules>

创建好febs-server模块后,开始创建febs-server-system模块。点击IDEA菜单栏 File -> New -> Modules...,选择Spring Initialzr作为模板,Module SDK选择JDK 1.8:

47.png

点击Next,按照下图所示填写相关内容:

48.png

 点击Next,暂时无需添加依赖,所以继续点击Next:

49.png

 修改febs-server-system模块的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>

    <parent>
        <groupId>cc.mrbird</groupId>
        <artifactId>febs-server</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>febs-server-system</artifactId>
    <name>FEBS-Server-System</name>
    <description>FEBS-Server-System微服务系统模块</description>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 在该pom中,指定了父模块为febs-server,在febs-server模块的pom的modules标签里也许引入febs-server-system:

<modules>
    <module>febs-server-system</module>
</modules>

在febs-server-system模块的入口类FebsServerSystemApplication上添加@EnableDiscoveryClient注解,开启服务注册与发现:

@EnableDiscoveryClient
@SpringBootApplication
public class FebsServerSystemApplication {

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

然后编写项目配置文件application.yml,内容如下所示:

server:
  port: 8201

spring:
  application:
    name: FEBS-Server-System

eureka:
  instance:
    lease-renewal-interval-in-seconds: 20
  client:
    register-with-eureka: true
    fetch-registry: true
    instance-info-replication-interval-seconds: 30
    serviceUrl:
      defaultZone: http://febs:123456@localhost:8001/register/eureka/

注:这里只是简单介绍最基础的搭建过程,其中涉及的很多依赖以及配置未列出,比如redis配置、web配置以及oauth2的具体拦截配置等等,想学习的同学可以自行去学习完善哈~

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值