• spring cloud 学习(一)初学SpringCloud


    初学SpringCloud

    前言

    在SpringBoot的坑还没填完的情况下,我又迫不及待地开新坑了。主要是寒假即将结束了,到时又得忙于各种各样的事情……留个坑给自己应该就会惦记着它,再慢慢地补上…………………………

    附录

    个人博客地址 : https://zggdczfr.cn
    个人参考项目 : https://github.com/FunriLy/springcloud-study/tree/master/%E6%A1%88%E4%BE%8B1

    SpringCloud 介绍

    Spring Cloud是一个基于Spring Boot实现的云应用开发工具(就是说,接触Spring Cloud之前需要了解一下Spring Boot)。归结起来,Spring Cloud 是一个微服务工具包,为开发者提供了在分布式系统的配置管理、服务发现、断路器、智能路由、微代理、控制总线等许多开发工具包。
    按照官方的说法就是,Spring Cloud 为开发者提供了在分布式系统操作的工具。而且容易上手,可以进行快速开发。

    微服务架构

    微服务架构概念

    “微服务架构”,就是将一个完整的应用从数据存储开始拆分成多个不同的服务,每个服务都能独立部署、独立维护、独立扩展。系统中的各个微服务可被独立部署,各个微服务之间是松耦合的。每个微服务仅关注于完成一件任务并很好地完成该任务。服务与服务间通过诸如RESTful API的方式互相调用。

    关于微服务架构的扯淡

    “微服务架构”,其实这个概念好几年前就已经出现了,而且国外也出现了较为成熟的产品:netflix、dubbo等。听说目前 Spring 开发团队的精力主要集中于 spring boot 和 spring cloud 相关框架的开发。一般情况下,作为一个学习 java 的屌丝,基本上跟上 spring 屌丝的步伐,也就跟上了主流技术。

    微服务架构的选择

    附上大神 程序猿DD(翟永超) 的一篇博客,可以参考一下:微服务架构的基础框架选择:Spring Cloud还是Dubbo?

    服务注册与发现(Eureka)

    Eureka Server

      • 创建一个 Spring Boot 项目。只要引入了Log4j2,便于日志记录。pom.xml依赖文件如下:
      <!-- SpringBoot 框架 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!--log4j2-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
            </dependency>
            <!-- eureka-服务 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
            </dependency>
    

     版本参数:

      <!-- 还是比较喜欢稳定的 Brixton 版本 -->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Brixton.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <!--spring boot 的 maven 插件-->
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    • 通过@EnableEurekaServer注解启动一个服务注册中心。直接在SpringBoot启动类加上注解@EnableEurekaServer即可。

    • application.properties配置:

    server.port=8761
    eureka.client.register-with-eureka=false
    eureka.client.fetch-registry=false
    eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
    

    同时,简要说明一下这些配置项:
    eureka.client.registerWithEureka :表示是否将自己注册到Eureka Server,默认为true。由于当前这个应用就是Eureka Server,故而设为false。
    eureka.client.fetchRegistry :表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设为false。
    eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。

    如图所示:

    这里写图片描述

    Eureka Client

    • 创建一个 Spring Boot 项目。基本上与上一个工程无异,pom.xml需要修改了一个依赖。
         <!-- eureka-服务 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
    
    • 通过@EnableDiscoveryClient注解启动一个服务注册中心。直接在SpringBoot启动类加上注解@EnableDiscoveryClient即可。同时,Eureka Client 提供的服务与一般 Spring Boot 项目是一样的。

    • application.properties配置:

    # eureka client 配置
    spring.application.name=service0
    server.port=1111
    eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
    

    如图所示:
    这里写图片描述

  • 相关阅读:
    iOS堆栈-内存-代码在据算机中的运行
    iOS self和super的区别
    php代码优化
    缓存雪崩现象解决方案
    缓存失效
    分布式memcache
    Linux下编译安装Memcache
    windows 下安装 php-memcached 扩展
    Linux下安装 php-memcache 扩展
    缓存之文件缓存
  • 原文地址:https://www.cnblogs.com/MaxElephant/p/8109123.html
Copyright © 2020-2023  润新知