Spring Cloud 服务注册中心的创建与简介(Greenwich SR3版)

1. 简介

    Spring Cloud 是一套分布式服务管理框架,本身不会提供具体功能性的操作,专注于服务之间的通讯、熔断、监控等。因此就需要很多的组件来支持一套功能。微服务是可以独立部署、水平扩展、独立访问(或者有独立的数据库)的服务单元,Spring Cloud 可以很好的管理多个独立的服务,相近模式的架构是Dubbo。

1.1 Spring Cloud 官方资源

1.2 Spring Cloud 特性

Distributed/versioned configuration(分布式/版本化配置)
Service registration and discovery(服务注册和发现)
Routing(路由)
Service-to-service calls(服务到服务的通话)
Load balancing(负载均衡)
Circuit Breakers(断路器)
Global locks(全局锁)
Leadership election and cluster state(领导竞选和集群状态)
Distributed messaging(分布式消息)

1.3 Spring Cloud 与 Dubbo 区别

Dubbo Spring Cloud
服务注册中心 Zookeeper Spring Cloud Netflix Eureka
服务调用方式 RPC RESTAPI
服务监控 Dubbo-monitor Spring Boot Admin
断路器 不完善 Spring Cloud Netflix Hystrix
服务网关 Spring Cloud Netflix Zuul
分布式配置 Spring Cloud Config
服务跟踪 Spring Cloud Sleuth
消息总线 Spring Cloud Bus
数据流 Spring Cloud Stream
批量任务 Spring Cloud Task

    简单来说Spring Cloud 体系架构更广,包含了Dubbo 的功能,相对的可以说在某些地方Dubbo 做的更深入,还是推荐使用Spring Cloud。

1.4 Spring Cloud 版本

    Greenwich 只支持 Spring Boot 2.1.x 分支。错误使用版本会存在兼容性问题,版本对应关系,可以在官方文档主页面找到。

Boot Version 2.2.x 2.1.x 2.0.x 1.5.x
Release Train Hoxton Greenwich Finchley Edgware/Dalston

1.5 演示示例环境说明

系统 工具 Spring Cloud 版本 Spring Boot 版本
Win10 IDEA Greenwich SR3 2.1.9

2. Spring Cloud 项目创建

2.1 程序示例

    Spring Cloud 和Spring Boot 同样方便,使用Spring Cloud 的注解就可以拥有很多功能。如下服务发现程序示例,可以使用注解 @EnableDiscoveryClient 声明程序启动类,使得项目支持服务发现。使用注解 @EnableEurekaServer 使得项目支持服务注册。

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

2.2 创建 Spring Initializr Project

    2.2.1 新建项目
在这里插入图片描述
    2.2.2 可以在创建项目时选中Eureka Server 依赖,当然你也可以不选,创建项目后手动配置。
在这里插入图片描述
    2.2.3 创建完成的项目如下
在这里插入图片描述

2.3 服务注册中心配置

    2.3.1 依赖管理配置pom.xml 没有改动

<?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.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>pers.niaonao</groupId>
    <artifactId>spring-cloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR3</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>

    2.3.2 应用程序入口启动类添加注解 @EnableEurekaServer

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

@SpringBootApplication
@EnableEurekaServer
public class SpringCloudApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudApplication.class, args);
    }
}

    2.3.3 配置文件application.yml; 建议使用yaml 格式文档,此处替代application.properties 文件

# server.port 服务发现端口,默认为8761
server:
  port: 8761
# eureka.instance.hostname 主机IP,访问实例时需要通过主机IP+端口号访问,主机名+端口号访问不到
# fetch-registry 检索服务选项,当设置为True(默认值),会进行服务检索,注册中心不负责检索服务。
# register-with-eureka 服务注册中心也会将自己作为客户端来尝试注册自己,true(默认)时自动生效
# eureka.client.serviceUrl.defaultZone 是一个默认的注册中心地址。配置该选项后,可以在服务中心进行注册。
# 为什么不建议使用default-zone 替代defaultZone
#   service-url 的Value 支持Map 键值对;参考博客 https://blog.csdn.net/u011531425/article/details/81713441
#   zone 属性会被Eureka Client 解析当成客户端默认当成zone 处理;
#   default-zone 被解析时会被认为客户端没有有效的配置service-url;
#   defaultZone 会正常处理为默认的service-url 为http://192.168.15.1:8761/eureka/
eureka:
  instance:
    hostname: 192.168.15.1
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

    2.3.4 配置完成后通过程序入口类SpringCloudApplication 启动项目,访问http://192.168.15.1:8761/ 或 http://localhost:8761/ 可以看到Eureka 注册中心,此时Spring Cloud 服务注册中心项目创建成功。只是目前还没有注册实例,DS Replicas 面板上显示No instances available。
在这里插入图片描述

THE END

发布了132 篇原创文章 · 获赞 312 · 访问量 100万+

猜你喜欢

转载自blog.csdn.net/niaonao/article/details/102467755
今日推荐