springCloud-netflix-Eureka注册中心

概要

微服务框架中最为核心和基础的模块就是服务治理,它主要用来实现各个微服务实例的自动化注册与发现。在这个体系结构中有一个“中心点”——服务注册中心,每个服务必须注册到服务注册中心。而各个服务之间进行通讯并不需要知道具体服务的主机名和端口。这种实现的一个缺点是所有客户机必须实现某种逻辑来与这个中心点进行交互,这样在实现服务请求之前将增加一次额外的网络往返。

Spring Cloud 使用 Netflix Eureka
来实现服务注册与发现,它既包含了服务端组件,也包含了客户端组件。Eureka服务端也称为服务注册中心,支持高可用配置。它依托强一致性提供良好的服务实例可用性。Eureka客户端可以同时充当服务器,将其状态复制到一个连接的对等点上。换句话说,客户机检索服务注册中心所有连接的节点的列表,并通过负载平衡算法向所有其他服务发出请求。每个客户机为了声明自己的存活状态,他们必须向注册中心发送一个心跳信号。

在本例中为了体现服务治理功能,实现了三个微服务:

一个服务注册中心 (Eureka Server) 一个在注册中心注册的REST服务(Eureka Client)
一个Web应用程序,服务的消费者(Spring Cloud Netflix Feign Client)

1.注册中心
使用Spring Cloud Netflix Eureka实现一个注册中心非常简单。在pom里增加spring-cloud-starter-eureka-server依赖,在启动类里添加@EnableEurekaServer注解。

@EnableEurekaServer //开启Eureka Server

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/>
    </parent>
    <dependencies>
        <!-- 注册中心 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

        <!-- 用于注册中心访问账号认证 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-parent</artifactId>
                <version>Edgware.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

第二步:创建启动类

package com.springcloud;

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

/**
 * @PACKAGE_NAME PACKAGE_NAME
 * @PROJECT_NAME springcloudeurekaregister
 * @创建人 huang
 * @创建时间 2018/11/26
 */
@SpringBootApplication
@EnableEurekaServer //开启Eureka Server
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class,args);
    }
}

修改全局配置文件application.yaml:

server:
  port: 8888 #服务注册中心端口号

security:
  basic:
    enabled: true #开启认证
  user:  #登录eureka的用户名和密码
    name: user
    password: 123456


eureka:
  instance:
    hostname: localhost #服务注册中心实例的主机名
  client:
    register-with-eureka: false #是否向服务注册中心注册自己
      fetch-registry: false #是否检索服务
      service-url:
        defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

浏览器输入http://localhost:8888/可以看到Eureka的控制台,在那里能看到将来注册后的服务实例和一些状态和健康指标。

在这里插入图片描述

扫描二维码关注公众号,回复: 4260245 查看本文章

猜你喜欢

转载自blog.csdn.net/weixin_36964056/article/details/84546897