51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2560|回复: 0
打印 上一主题 下一主题

[原创] 初探Spring Cloud

[复制链接]
  • TA的每日心情

    前天 09:25
  • 签到天数: 76 天

    连续签到: 1 天

    [LV.6]测试旅长

    跳转到指定楼层
    1#
    发表于 2019-3-8 16:25:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

    1.  什么是Spring Cloud?

    Spring提供了一系列工具,可以帮助开发人员迅速搭建分布式系统中的公共组件(比如:配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁,主节点选举, 分布式session, 集群状态)。协调分布式环境中各个系统,为各类服务提供模板性配置。使用Spring Cloud, 开发人员可以搭建实现了这些样板的应用,并且在任何分布式环境下都能工作得非常好,小到笔记本电脑, 大到数据中心和云平台。

    Spring Cloud官网的定义比较抽象,我们可以从简单的东西开始。Spring Cloud是基于Spring Boot的, 最适合用于管理Spring Boot创建的各个微服务应用。要管理分布式环境下的各个Spring Boot微服务,必然存在服务的注册问题。所以我们先从服务的注册谈起。既然是注册,必然有个管理注册中心的服务器,各个在Spring Cloud管理下的Spring Boot应用就是需要注册的client

    Spring Cloud使用erureka server,  然后所有需要访问配置文件的应用都作为一个erureka client注册上去。eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳,在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。

    2.  创建Eureka Server

    1).创建一个Maven工程helloworld.eureka.server, pom.xml内容如下:

    1. <project xmlns="http://maven.apache.org/POM/4.0.0"
    2.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3.         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.         <modelVersion>4.0.0</modelVersion>
    5.         <groupId>com.cloud</groupId>
    6.         <artifactId>springcloud.helloworld.eureka.server</artifactId>
    7.         <version>0.0.1-SNAPSHOT</version>
    8.         <name>springcloud.helloworld.Eureka.server</name>
    9.         <description>Demo Spring Eureka Server</description>

    10.         <parent>
    11.                 <groupId>org.springframework.boot</groupId>
    12.                 <artifactId>spring-boot-starter-parent</artifactId>
    13.                 <version>1.5.3.RELEASE</version>
    14.                 <relativePath /> <!-- lookup parent from repository -->
    15.         </parent>
    16.        
    17.         <properties>
    18.          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    19.          <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    20.          <java.version>1.8</java.version>
    21.      </properties>

    22.      <dependencies>
    23.          <!--eureka server -->
    24.          <dependency>
    25.              <groupId>org.springframework.cloud</groupId>
    26.              <artifactId>spring-cloud-starter-eureka</artifactId>
    27.          </dependency>
    28.          <dependency>
    29.              <groupId>org.springframework.cloud</groupId>
    30.              <artifactId>spring-cloud-starter-eureka-server</artifactId>
    31.          </dependency>
    32.          <dependency>
    33.              <groupId>org.springframework.cloud</groupId>
    34.              <artifactId>spring-cloud-starter-config</artifactId>
    35.          </dependency>
    36.          <!-- spring boot test-->
    37.          <dependency>
    38.              <groupId>org.springframework.boot</groupId>
    39.              <artifactId>spring-boot-starter-test</artifactId>
    40.              <scope>test</scope>
    41.          </dependency>
    42.      </dependencies>

    43.      <dependencyManagement>
    44.          <dependencies>
    45.              <dependency>
    46.                  <groupId>org.springframework.cloud</groupId>
    47.                  <artifactId>spring-cloud-dependencies</artifactId>
    48.                  <version>Dalston.RC1</version>
    49.                  <type>pom</type>
    50.                  <scope>import</scope>
    51.              </dependency>
    52.          </dependencies>
    53.      </dependencyManagement>

    54.      <build>
    55.          <plugins>
    56.              <plugin>
    57.                  <groupId>org.springframework.boot</groupId>
    58.                  <artifactId>spring-boot-maven-plugin</artifactId>
    59.              </plugin>
    60.          </plugins>
    61.      </build>

    62.      <repositories>
    63.          <repository>
    64.              <id>spring-milestones</id>
    65.              <name>Spring Milestones</name>
    66.              <url>https://repo.spring.io/milestone</url>
    67.              <snapshots>
    68.                  <enabled>false</enabled>
    69.              </snapshots>
    70.          </repository>
    71.      </repositories>
    72. </project>
    复制代码

    2). 用Spring Boot创建一个服务类EurekaServerApplication,需要一个注解@EnableEurekaServer加在springboot工程的启动类上

    1. package springcloud.helloworld.eureka.server;

    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

    5. @EnableEurekaServer
    6. @SpringBootApplication
    7. public class EurekaServerApplication {

    8.         public static void main(String[] args) {
    9.                 SpringApplication.run(EurekaServerApplication.class, args);
    10.         }
    11. }
    复制代码

    3).eureka server的配置文件bootstrap.yml,其中registerWithEureka:false和fetchRegistry:false表明自己是一个eureka server

    server:
       port: 8761

    eureka:
       instance:
           hostname: 127.0.0.1
       client:
           registerWithEureka: false
           fetchRegistry: false
           serviceUrl:
               defaultZone: http://127.0.0.1:8761/eureka/

    4) eureka server的工程结构如下

    server:
       port: 8761

    eureka:
       instance:
           hostname: 127.0.0.1
       client:
           registerWithEureka: false
           fetchRegistry: false
           serviceUrl:
               defaultZone: http://127.0.0.1:8761/eureka/

    5)启动eureka server,然后访问http://localhost:8761, 界面如下, "No instances available" 表示无client注册

    3.  创建Eureka Client

    1). 创建一个Maven工程helloworld.eureka.client, pom.xml内容如下:

    1. <project xmlns="http://maven.apache.org/POM/4.0.0"
    2.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3.         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.         <modelVersion>4.0.0</modelVersion>
    5.         <groupId>com.cloud</groupId>
    6.         <artifactId>springcloud.helloworld.eureka.client</artifactId>
    7.         <version>0.0.1-SNAPSHOT</version>
    8.         <name>springcloud.helloworld.eureka.client</name>
    9.         <description>Demo Spring Boot Client</description>

    10.         <parent>
    11.                 <groupId>org.springframework.boot</groupId>
    12.                 <artifactId>spring-boot-starter-parent</artifactId>
    13.                 <version>1.5.3.RELEASE</version>
    14.                 <relativePath /> <!-- lookup parent from repository -->
    15.         </parent>

    16.         <properties>
    17.                 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    18.                 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    19.                 <java.version>1.8</java.version>
    20.         </properties>

    21.         <dependencies>
    22.                 <dependency>
    23.                         <groupId>org.springframework.cloud</groupId>
    24.                         <artifactId>spring-cloud-starter-eureka</artifactId>
    25.                 </dependency>
    26.                 <dependency>
    27.                         <groupId>org.springframework.boot</groupId>
    28.                         <artifactId>spring-boot-starter-web</artifactId>
    29.                 </dependency>

    30.                 <dependency>
    31.                         <groupId>org.springframework.boot</groupId>
    32.                         <artifactId>spring-boot-starter-test</artifactId>
    33.                         <scope>test</scope>
    34.                 </dependency>
    35.         </dependencies>

    36.         <dependencyManagement>
    37.                 <dependencies>
    38.                         <dependency>
    39.                                 <groupId>org.springframework.cloud</groupId>
    40.                                 <artifactId>spring-cloud-dependencies</artifactId>
    41.                                 <version>Dalston.RC1</version>
    42.                                 <type>pom</type>
    43.                                 <scope>import</scope>
    44.                         </dependency>
    45.                 </dependencies>
    46.         </dependencyManagement>

    47.         <build>
    48.                 <plugins>
    49.                         <plugin>
    50.                                 <groupId>org.springframework.boot</groupId>
    51.                                 <artifactId>spring-boot-maven-plugin</artifactId>
    52.                         </plugin>
    53.                 </plugins>
    54.         </build>

    55.         <repositories>
    56.                 <repository>
    57.                         <id>spring-milestones</id>
    58.                         <name>Spring Milestones</name>
    59.                         <url>https://repo.spring.io/milestone</url>
    60.                         <snapshots>
    61.                                 <enabled>false</enabled>
    62.                         </snapshots>
    63.                 </repository>
    64.         </repositories>
    65. </project>
    复制代码

    2).  创建主类EurekaClientApplication

    1. package springcloud.helloworld.eureka.client;

    2. import org.springframework.beans.factory.annotation.Value;
    3. import org.springframework.boot.SpringApplication;
    4. import org.springframework.boot.autoconfigure.SpringBootApplication;
    5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    6. import org.springframework.web.bind.annotation.RequestMapping;
    7. import org.springframework.web.bind.annotation.RestController;

    8. @RestController
    9. @SpringBootApplication
    10. @EnableEurekaClient
    11. public class EurekaClientApplication {

    12.         public static void main(String[] args) {
    13.                 SpringApplication.run(EurekaClientApplication.class, args);
    14.         }

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

    17.         @RequestMapping("/")
    18.         public String home() {
    19.                 return "hello world from port " + port;
    20.         }
    21. }
    复制代码

    3) eureka client的配置文件bootstrap.yml

    eureka:
        client:
            serviceUrl:
                defaultZone: http://localhost:8761/eureka/
    server:
        port: 8762
    spring:
        application:
            name: service-helloworld

    4). Client启动后, 可以访问http://localhost:8762

    5). 再次访问服务器端口, 可以看到Service Helloworld已经自动注册到之前的server中


    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-9-20 07:48 , Processed in 0.073254 second(s), 24 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表