SpringCloud学习-第一节-服务注册与发现

一、Eureka服务注册与发现

1.Eureka基础知识

  • 服务治理:用于管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。
  • Eureka包含两个组件:Eureka Server和Eureka Client
    Eureka Server:提供服务注册服务,存储所有可用服务节点的信息
    Eureka Client:EurekaClient通过注册中心进行访问

2.单机Eureka构建步骤

(1)pom文件:

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</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>

(2)配置文件

server:
  port: 7001
eureka:
  instance:
    hostname: localhost #eureka服务端的实例名称
  client:
    #false表示不向注册中心注册自己。
    register-with-eureka: false
    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
      #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

(3)启动类

package com.zj.springcloud;

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

/**
 * 概述:
 * 作者:zhujie
 * 创建时间:2021/12/15 10:32
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerAppication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerAppication.class,args);
    }
}

(4)测试成功的页面
在这里插入图片描述

3.集群Eureka构建步骤

(1)按照单机Eureka构建步骤建立两个节点
(2)修改两个节点配置文件分别为(区别为defaultZone配置项,单节点构建时,为节点本身地址,集群构建时,为集群中除节点本身外其它节点的地址)
节点一:

server:
  port: 7001
eureka:
  instance:
    hostname: eureka7001.com #eureka服务端的实例名称
  client:
    #false表示不向注册中心注册自己。
    register-with-eureka: false
    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
      #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
      defaultZone: http://eureka7002.com:7002/eureka/

节点二:

server:
  port: 7002
eureka:
  instance:
    hostname: eureka7002.com #eureka服务端的实例名称
  client:
    #false表示不向注册中心注册自己。
    register-with-eureka: false
    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
      #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
      defaultZone: http://eureka7001.com:7001/eureka/

4.actuator微服务信息完善

(1)引入jar

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

(2)主机名称修改与显示ip地址:

eureka:
  instance:
    instance-id: payment1
    prefer-ip-address: true

在这里插入图片描述

二、zookeeper服务注册与发现

(1)引入jar包

 <!-- SpringBoot整合zookeeper客户端 -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>

(2)配置文件

spring:
  application:
    name: springcloud-order
  cloud:
    zookeeper:
      connect-string: 127.0.0.1:2181

(3)启动类

package com.zj.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * 概述:
 * 作者:zhujie
 * 创建时间:2021/12/15 9:24
 */
@SpringBootApplication
@EnableDiscoveryClient
public class OrderAppication {
    public static void main(String[] args) {
        SpringApplication.run(OrderAppication.class,args);
    }
}

三、Consul服务注册与发现

(1)引入jar包

 <!-- SpringBoot整合Consul客户端 -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

(2)配置文件

spring:
  application:
    name: springcloud-order
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        #hostname: 127.0.0.1
        service-name: ${spring.application.name}

(3)启动类

package com.zj.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * 概述:
 * 作者:zhujie
 * 创建时间:2021/12/15 9:24
 */
@SpringBootApplication
@EnableDiscoveryClient
public class OrderAppication {
    public static void main(String[] args) {
        SpringApplication.run(OrderAppication.class,args);
    }
}

四、三者区别

组件名编写语言CAP服务健康检查对外暴露的接口springcloud集成
eurekaJavaAP可配支持HTTP已集成
consulGoCP支持HTTP/DNS已集成
zookeeperJavaCP支持客户端已集成

注:CAP定理: 指的是在一个分布式系统中,Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性),三者不可同时获得。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值