Eureka注册中心_Spring_cloud_1

本文详细介绍了Eureka作为Spring Cloud的服务注册中心,包括注册中心概述、搭建步骤、高可用配置以及高可用过程中遇到的细节问题解决,如服务控制台IP显示、服务剔除机制和自我保护策略的调整。
摘要由CSDN通过智能技术生成

一. 注册中心概述

本文仅供单个模块代码的提示,如想知整套demo,请留言!

cap原理图解:
在这里插入图片描述

Eureka注册中心的客户端消费者会缓存地址信息到本地,这就造成了信息不一致,也就是cap原理的ap;
在这里插入图片描述

二.使用Eureka的步骤

Eureka是服务注册中心,只做服务注册;自身并不提供服务也不消费服务。可以搭建web工程使用Eureka,可以使用Spring Boot方式搭建。

1.搭建Eureka

步骤:

  1. 创建一个maven工程;

  2. 导入坐标

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
  1. 配置application.yml文件
spring:
  application:
    name: eureka-server
server:
  port: 9000 #端口
#配置eureka server
eureka:
    instance:
      hostname: localhost #主机地址名称
    client:
      register-with-eureka: false  # 是否将自己注册到注册中心
      fetch-registry: false   # 是否从eureka中获取注册信息
      service-url: #配置暴露给Eureka Client(客户端)的请求地址
        defaultZone: http://127.0.0.1:9000/eureka/  # eureka 服务地址,如果是集群的话;需要指定其它集群eureka地址,使用逗号隔开
      #或者 http://${
   eureka.client.instance.hostname}:${
   server.port}/eureka/



  1. 配置启动类:一个注解@EnableEurekaServer
package cn.itcast.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaServer //激活EurekaServer
class EurekaServerApplication {
   
    public static void main(String[] args) {
   

        SpringApplication.run(EurekaServerApplication.class,args);
    }
}

2.将服务提供者注册到EurekaServer上

1. 引入EurekaClient的坐标
  <!--引入eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
2. 修改application.yml 添加EurekaServer的相关信息
server:
  port: 9001 #端口
spring:
  application:
    name: service-product #服务名称
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8
    username: root
    password: root
  jpa:
    database: MySQL
    show-sql: true
    open-in-view: true
#配置Eureka
eureka:
    client:
      service-url:
        defaultZone: http://127.0.0.1:9000/eureka/ #EurekaService的路径
    instance:
     prefer-ip-address: true #使用ip地址注册
3.修改启动类,添加服务发现的支持(可选)
//激活Eureka(两个注解都可以),新版本可以不写
@EnableEurekaClient
@EnableDiscoveryClient

3.服务消费者通过注册中心获取服务列表,并调用

1.引入坐标
        <!--引入eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <art
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值