springcloud入门-eureka

eureka知识点

以下是本章知识点

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

开始

server创建

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

配置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/

image
即可打开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创建

首先创建项目,具体搓成不详细写了,提出主要的截图
image

如果启动失败的话,看看是不是忘了添加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服务已经注册了.
image

高可用

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

image

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

总结

image

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

posted @ 2019-05-29 22:48  dadongx  阅读(190)  评论(0编辑  收藏  举报