第04课:初识 Spring Cloud

Spring Cloud 基于 Spring Boot,因此在前几篇,我们系统地学习了 Spring Boot 的基础知识,为深入研究Spring Cloud打下扎实的基础。

从本章开始,我们将正式进入探索Spring Cloud秘密的旅程中。学习完本课程后,读者将从中学习到如何搭建一个完整的分布式架构,从而向架构师方向靠近。

微服务概述

根据百度百科的描述,微服务架构是一项在云中部署应用和服务的新技术。大部分围绕微服务的争论都集中在容器或其他技术是否能很好的实施微服务,而红帽说 API 应该是重点。

微服务可以在“自己的程序”中运行,并通过“轻量级设备与 HTTP 型 API 进行沟通”。关键在于该服务可以在自己的程序中运行。通过这一点我们就可以将服务公开与微服务架构(在现有系统中分布一个 API)区分开来。在服务公开中,许多服务都可以被内部独立进程所限制。如果其中任何一个服务需要增加某种功能,那么就必须缩小进程范围。在微服务架构中,只需要在特定的某种服务中增加所需功能,而不影响整体进程。

微服务的核心是 API,在一个大型系统中,我们可以将其拆分为一个个的子模块,每一个模块就可以是一个服务,各服务之间通过 API 进行通信。

什么是 Spring Cloud

Spring Cloud是微服务架构思想的一个具体实现,它为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理、服务发、断路器,智能路由、微代理、控制总线等)。

Spring Cloud 基于 Spring Boot 框架,它不重复造轮子,而是将第三方实现的微服务应用的一些模块集成进去。准确的说,Spring Cloud 是一个容器。

最简单的 Spring Cloud 项目

学习任何一门语言和框架,从 Hello World 入门是最合适的,Spring Cloud 也不例外,接下来,我们就来实现一个最简单的 Spring Cloud 项目。

最简单的 Spring Cloud 微服务架构包括服务发现和服务提供者(即一个大型系统拆分出来的子模块),最极端的微服务可以做到一个方法就是一个服务,一个方法就是一个项目。在一个系统中,服务怎么拆分,要具体问题具体分析,也取决于系统的并发性、高可用性等因素。

闲话少说,请看代码。

首先是服务发现,这里我们采用 Eureka。

pom.xml 文件添加如下内容:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </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>Dalston.SR5</version>
                <type>pom</type>
                <scope>import</scope>
                <exclusions>
                </exclusions>
            </dependency>
        </dependencies>
    </dependencyManagement>

增加 application.yml 文件,添加如下内容:

server:
  port: 8761
spring:
  profiles:
    active: dev
eureka:
  server:
    enable-self-preservation: false
  instance:
    preferIpAddress: true
    hostname: ${spring.cloud.client.ipAddress}
    instanceId: ${spring.cloud.client.ipAddress}:${server.port}
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

添加一个启动类 Application.java:

@SpringBootApplication
@EnableEurekaServer
public class Application {

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

然后再创建一个项目,实现服务提供者,在 pom.xml 添加如下内容:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </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>Dalston.SR5</version>
                <type>pom</type>
                <scope>import</scope>
                <exclusions>
                </exclusions>
            </dependency>
        </dependencies>
    </dependencyManagement>

增加 application.yml,并增加如下内容:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8762
spring:
  application:
    name: hello

增加一个启动类:

@SpringBootApplication
@EnableEurekaClient
@RestController
public class HelloController {

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

    @Value("${server.port}")
    String port;
    @RequestMapping("/hello")
    public String home(String name) {
        return "hi "+name+",i am from port:" +port;
    }

}

这时,分别启动服务发现和服务提供者,浏览器输入:http://localhost:8761,即服务发现的地址:
《第04课:初识 Spring Cloud》

可以发现,服务提供者 Hello 已经注册到服务发现中了,然后我们请求 hello 接口地址:http://localhost:8762/hello?name=lynn,即可以看到下面返回数据:
《第04课:初识 Spring Cloud》

以上只是 Spring Cloud 的入门实例,是为了给大家展示什么是 Spring Cloud

    原文作者:taojin
    原文地址: https://segmentfault.com/a/1190000016077755
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞