二:Spring Cloud 之Eureka服务注册中心(单机版)

版权声明:本文为博主原创文章,觉得稍有帮助,可点赞、转载注明出处。 https://blog.csdn.net/chenghuaying/article/details/82391805

1. Eureka简介

Eureka is the Netflix Service Discovery Server and Client. The server can be configured and deployed to be highly available, with each server replicating state about the registered services to the others.

  • Eureka基于REST实现了服务注册、服务发现的Netfix组件
  • 可通过相互注册实现HA(高可用)
  • 微服务中服务注册发现功能组件

2. 代码实现

2.1涉及的模块

  • spring-cloud-demo
  • eureka-server-singleton

2.2 源代码

这里只说明一次如何切换到对应的Revision number,后续只会列出Github地址与Revision number

2.2.1 Github地址

https://github.com/andyChenHuaYing/spring-cloud-demo

2.2.2 切换

Commit:Finished eureka singleton server example
Revision Number:2eb0802b94e41b4a5e25a60c3bfb9a1484166aa3
  • 切换到本篇博文对应的版本可使用命令或者图形化形式切换

    • 图形化切换:打开Intellij 的Version control(ctrl+9)–> 查看提交日志(log),找到Finished eureka singleton server example注释–> 右键:checkout revision
      切换导指定Revision

    • 命令行切换:alt+f12打开 Intellij 集成的 Terminal –>输入 git log查看注释为Finished eureka singleton server example的 revision number:2eb0802b94e41b4a5e25a60c3bfb9a1484166aa3 –> 切换过来:git checkout 2eb0802b94e41b4a5e25a60c3bfb9a1484166aa3
      这里写图片描述

2.3 spring-cloud-demo

spring-cloud-demo 是parent,定义Project级别的一些属性与依赖。
后续所有的module都继承于此,后续变动的仅为modules标签中会多增加module。

2.3.1 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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <modules>
        <module>eureka-server-singleton</module>
    </modules>

    <groupId>org.oscar.scd</groupId>
    <artifactId>spring-cloud-finchley-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</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.4 eureka-server-singleton

单机版eureka实现,常规创建maven类型的module即可。

2.4.1 pom.xml

主要引入 eureka-server依赖,因为继承于spring-cloud-demo,不必指定版本号。
<?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>org.oscar.scd</groupId>
        <artifactId>spring-cloud-finchley-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>eureka-server-singleton</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-server-singleton</name>
    <description>Eureka singleton server</description>

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

2.4.2 application.yml

指定eureka-server相关设置,因为当前module是eureka-server,所以需要指定不把当前module注册到eureka注册中心,并且指定注册中心的serviceUrl。
spring:
  application:
    name: eureka-server

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

2.4.3 EurekaServerSingletonApplication

启动eureka-server很简单,只需要在启动类上添加`@EnableEurekaServer`即可
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerSingletonApplication {

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

3. 验证

3.1 启动模块

可新增SpringBoot类型的启动类来启动,后续随着模块的增加,启动类会不断增加,还有一个启动类需要根据不同的profile来指定不同的端口,模拟多服务提供。如果都是SpringBoot类型的启动类,则可以统一由Run Dashboard窗口来进行各种操作,比较方便。

这里写图片描述

3.2 访问注册中心

访问地址:http://localhost:8761/  可以查看eureka-server已经启动。

这里写图片描述

4. 思考

目前阶段的一些疑惑或者想法,暂时这里没有答案,自己的总结一定会有,不过可能会在后续的文章中,或者原理、源码的理解篇幅中。
  • 注册中心是非常重要的一个环节,如何实现高可用、突发状况下注册信息不丢失
  • 是否支持zookeeper
  • 服务的自动注册与注销由哪些配置可控,如检测时间
  • 如何保持心跳连接

5. 补充

5.1 资料

http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi_spring-cloud-eureka-server.html

5.2 项目结构

这里写图片描述

猜你喜欢

转载自blog.csdn.net/chenghuaying/article/details/82391805
今日推荐