五、Eureka注册与发现(一)

clipboard

1、Eureka的基础知识

1)什么是服务治理?

在传统的rpc调用框架中,服务与服务之间的依赖关系比较混款,管理比较复杂,所以需要使用服务治理,来管理服务间的依赖关系,实现服务调用,负载均衡,容错等,实现服务的发现和注册

2)什么是服务注册?

Eureka使用CS的设计架构,Eureka Server作为服务注册功能的服务器,它是注册中心,而系统中的

其他微服务服务,使用Eureka的客户端连接到Eureka Server并维持心跳连接。这样系统就可以通过Eureka Server来监控系统中的各个微服务是否能正常运行

在服务注册与发现中,有一个注册中心。当服务器启动的时候,会吧当前服务器的信息,比如服务地址等以别名的方式注册到注册中心上。服务消费者会以该别名去主持中心获取实际的服务提供者的地址。

clipboard

3)eureka的两个组件

① Eureka Server 提供服务注册服务

各个微服务节点通过配置启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可服务节点的信息,服务节点的信息可以在界面中直观看到

② Eureka Client 通过注册中心,进行访问

简而言之,是一个Java客户端,用于简化与Eureka的交互,客户端同时也具备一个内置的,使用轮询负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期是30s),如果Euraka在

多个心跳周期内没有收到某个节点的心跳,Eureka将会从服务注册表中将这个服务节点删除(默认是90s)

2、单机Eureka构建步骤

1)创建 EurekaServer 服务

clipboard

① 引入依赖

<dependencies>
    <!--eureka-server-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
    <dependency>
        <groupId>com.atguigu.springcloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>${project.version}</version>
    </dependency>
    <!--boot web actuator-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--一般通用配置-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
</dependencies>

② 编写application.yml

server:
  port: 7001
eureka:
  instance:
    hostname: localhost  #eureka服务端的实例名称
  client: 
    #false表示不向注册中心注册自己
    register-with-eureka: false 
    # false表示不去eureka server拉取其他服务的配置
    fetch-registry: false
    service-url:
      #eureka server的地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

③ 编写主启动类,并启动服务

@SpringBootApplication
@EnableEurekaServer // 开启eureka服务
public class EurekaMain7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7001.class, args);
    }
}

访问: http://localhost:7001

clipboard

2)支付微服务8001入驻进eurekaServer

① 添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

② 编写application.yml

eureka:
  client:
    register-with-eureka: true #表示是否将自己注册到EurekaServer
    fetch-registry: true #是否从eurekaServer抓取已有的注册信息,默认为true,单点无所谓,集群必须设置weitrue才能配合ribbon使用负载均衡
    service-url:
      defaultZone: http://localhost:7001/eureka

③ 主启动类上添加注解 开启eureka客户端

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

clipboard

3)订单微服务80入驻进eurekaServer(操作和上面步骤相同)

clipboard

原文地址:https://www.cnblogs.com/houchen/p/14798686.html