springcloud入门-eureka

eureka知识点

以下是本章知识点

  • @EnableEurekaClient @EnableEurekaServer
  • 心跳检查 健康检查 负载均衡
  • 高可用 prod建议 节点2+
  • 微服务中 服务注册最基础部分

开始

server创建

创建一个springboot项目. 具体步骤不写了,只贴几张图算了.
在这里插入图片描述
创建项目,选择神器 Spring initializr, 如果https网不行改成http试试,在不行的话需要kx上网了.
在这里插入图片描述

配置server端的
application.yml

server:
  port: 8761
eureka:
#  instance:
#    hostname: localhost
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/    
#  # 频繁重启后不默认显示client在线
#  server:
#    enable-self-preservation: false
spring:
  application:
    name: eureka-server

enable-self-preservation: 频繁重启后不默认显示client在线
register-with-eureka: 是否讲自身服务注册到注册中心 默认是true
fetchRegistry: 是否显现注册服务信息
EurekaApplication程序入口

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

}

@EnableEurekaServer 意思是生命引入eureka server组建

启动server

访问 http://localhost:8761/

在这里插入图片描述
即可打开euraka的配置中心页面.为什么是8761呢?

访问源码可以看到,默认地址是http://localhost:8761/

    public EurekaClientConfigBean() {
        this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/");
        this.gZipContent = true;
        this.useDnsForFetchingServiceUrls = false;
        this.registerWithEureka = true;
        this.preferSameZoneEureka = true;
        this.availabilityZones = new HashMap();
        this.filterOnlyUpInstances = true;
        this.fetchRegistry = true;
        this.dollarReplacement = "_-";
        this.escapeCharReplacement = "__";
        this.allowRedirects = false;
        this.onDemandUpdateStatusChange = true;
        this.clientDataAccept = EurekaAccept.full.name();
        this.shouldUnregisterOnShutdown = true;
        this.shouldEnforceRegistrationAtInit = false;
        this.order = 0;
    }
client创建

首先创建项目,具体搓成不详细写了,提出主要的截图
在这里插入图片描述

如果启动失败的话,看看是不是忘了添加springboot-web依赖

client配置

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
  # client 域名配置
  instance:
    hostname: clientName

spring:
  application:
    name: eureka-client

入口添加@EnableEurekaClient注解

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }

}

搞定,这时候再去访问注册中心,我们eureka-client服务已经注册了.
在这里插入图片描述

高可用

其实就是多节点如何部署,难道直接多启动一个server就行吗? 当然不是,而且需要将client和server进行关联,不仅client,server之间也需要连接的

在这里插入图片描述

server端配置

---
# 启动参数 --spring.profiles.active=server1
spring:
  profiles: server1
server:
  port: 8761
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/
---
# 启动参数 --spring.profiles.active=server2
spring:
  profiles: server2
server:
  port: 8762
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8763/eureka/

---
# 启动参数 --spring.profiles.active=server3
spring:
  profiles: server3
server:
  port: 8763
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/

client端配置

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
  # client 域名配置
  instance:
    hostname: clientName

spring:
  application:
    name: eureka-client

总结

在这里插入图片描述

本文参考: 廖师兄的Spring Cloud微服务实战

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值