SpringCloud 学习(2) --- Eureka(一)注册中心

原创
2019/12/05 22:05
阅读数 278

[TOC]

1.1、什么是Eureka

当开发大型项目时,服务的提供方和服务的调用方,将会非常庞大,管理服务的成本将几何倍的增长。Eureka将负责服务的注册、发现、状态监控。

Eureka职责:

  • 服务注册:服务提供方将服务注册到注册中心
  • 服务发现:服务调用方法,从注册中心中,获得需要的服务
  • 服务检测:注册中心与服务之间采用心跳检测服务状态

1、注册:Eureka负责管理、记录服务提供者的信息

就是在服务启动的时候,调用Eureka Server的REST API的Register方法,注册应用实例的信息。

  • Java应用,使用Netflix的Eureka Client封装API调用
  • Spring Cloud引用,可以使用spring-cloud-starter-netflix-eureka-client基于Spring Boot自动配置,自动注册服务。

2、发现:服务调用者无需自己寻找服务,而是把自己的需求告诉Eureka,然后Eureka会把符合你需求的服务告诉你

3、监控:服务提供方与Eureka之间通过“心跳”机制进行监控,当某个服务提供方出现问题,Eureka自然会把它从服务列表中剔除

1.2原理图

基本架构:

  • Eureka:就是服务注册中心(可以是一个集群),对外暴露自己的地址
  • 提供者:启动后向Eureka注册自己信息(地址,提供什么服务)
  • 消费者:向Eureka订阅服务,Eureka会将对于服务的所有提供者地址列表发送给消费者,并且定期更新
  • 心跳(续约):提供者定期通过http方式向Eureka刷新自己的状态

2.1、入门案例

1、编写注册中心:eureka_demo

2、 将服务注册到注册中心:eureka_service

3、调用者从注册中心获得服务:eureka_client

父项目pom文件

  <!--1 确定spring boot的版本-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>

    <!--2  确定版本-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <spring-cloud-release.version>Greenwich.RELEASE</spring-cloud-release.version>
    </properties>

    <!-- 3 锁定sprig cloud版本-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud-release.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- 4 确定spring cloud私有仓库-->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

2.1.1、编写注册中心

核心步骤:

1.添加web启动器和eureka service 依赖

2.yml文件中配置eureka

3.启动类添加开发服务注解 @EnableEurekaServer

步骤一:创建eureka_demo模块

步骤二:修改pom.xml文件,添加依赖

<dependencies>
    <!--web起步依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Eureka服务端 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>

步骤三:创建application.yml,配置注册中心

server:
  port: 10086
#服务名称
spring:
  application:
    name: eureka_demo2
#注册中心地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:${server.port}/eureka   #eureka服务注册地址
    register-with-eureka: false     #关闭将自己注册到注册中心中
    fetch-registry: false           #关闭从注册中心获得列表(不拉去其他服务信息)

步骤四:编写启动类,添加注解

package com.czxy;

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

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

2.1.2、eurekaservic(8080)注册到Eureka注册中心

核心步骤

1.添加eureka.client客户端依赖

2.yml文件中确定注册中心的位置

3.启动类添加开启eureka client客户端注解@EnableEurekaClient

步骤一:创建 eureka_service模块

步骤二:修改pom.xml文件,添加 eureka client依赖

<dependencies>
    <!--web起步依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Eureka客户端 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!--spring boot监控-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

步骤三:创建yml文件,确定eureka注册中心的位置

server:
  port: 8080
spring:
  application:
    name: service
eureka:
  client:
    service-url: #注册中心位置
      defaultZone: http://localhost:10086/eureka/
  instance: #web页面显示效果和访问路径
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true #注册中心可视化中显示IP地址

在Eureka监控页面,查看服务注册信息:

在status一列中,显示以下信息:

• UP(1):代表现在是启动了1个示例,没有集群

• DESKTOP-2MVEC12:user-service:8081:是示例的名称(instance-id),

– 默认格式是:${hostname} + ${spring.application.name} + ${server.port}

– instance-id是区分同一服务的不同实例的唯一标准,因此不能重复。

我们可以通过instance-id属性来修改它的构成:

eureka:
  instance: *#web页面显示效果和访问路径*    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

步骤四:编写启动类

package com.czxy;

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


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

测试:

步骤六:提供测试内容

@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping
    public ResponseEntity<String> test(){
        return ResponseEntity.ok("测试数据");
    }
}

2.1.3、消费者eureka_client(9090)从Eureka获取服务

修改consumer-demo,尝试从EurekaServer获取服务。

步骤一:创建eureka_client模块

步骤二:修改pom.xml文件, (和eureka_service一样)

<dependencies>
    <!--web起步依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Eureka客户端 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!--spring boot监控-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

步骤三:创建yml文件

server:
  port: 9090
spring:
  application:
    name: client
eureka:
  client:
    service-url: #注册中心位置
      defaultZone: http://localhost:10086/eureka/
  instance: #web页面显示效果和访问路径
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true

步骤四:编写启动类

package com.czxy;

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

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

测试

步骤六:编写测试程序,通过RestTemplate通过IP地址直接访问service

package com.czxy.controller;

import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;


@RestController
@RequestMapping("/data")
public class DataController {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    @Resource
    private RestTemplate restTemplate;

    @GetMapping
    public ResponseEntity<String> data(){
        return restTemplate.getForEntity("http://localhost:8080/test",String.class);
    }
}

2.1.4 配置eureka instance

  • yml文件配置
    • instance-id : 用于配置可视化页面中,显示的服务名称
      • 默认服务名称:计算机名称:服务名:端口号
      • 自定义服务名称:
        • ${spring.application.name} 获得服务名
        • ${spring.cloud.client.ip-address} 获得ip地址
        • ${server.port} 获得端口号
    • prefer-ip-address:用于配置可视化页面中,访问时是否显示ip地址
      • 默认显示的是:计算机名称:端口号/
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
  instance:
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true   #注册中心可视化中显示IP地址

  • properties文件配置(不建议),参考学习
eureka.client.service-url.defaultZone=http://localhost:10086/eureka
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
eureka.instance.prefer-ip-address=true
展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
3
分享
返回顶部
顶部