spring cloud集成Eureka

spring cloud整合Eureka

Server模块的搭建

pom.xml配置

pom.xml文件,这里没有版本号是因为,这里的依赖都继承于父工程的pom

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

启动类配置

要使用@EnableEurekaServer开启EurekaServer服务

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

application.yam配置

eureka:
  instance:
    hostname: localhost
  #client端的配置,因为这里不需要自我注册所以设置为false
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

查看Eureka主页面

使用浏览器打开http://localhost:7001 查看主页面。

请添加图片描述

client模块的搭建

pom.xml配置

在要注册的微服务模块中的pom.xml添加Eureka-client依赖

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

启动类配置

要使用@EnableEurekaClient开启EurekaClient服务

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

application.yam配置

#服务名称
spring:
  application:
    name: cloud-payment-service
#配置服务发现
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

在Eureka中查看注册

进入Eureka的主页,查看服务是否注册成功。

请添加图片描述

Eureka集群搭建

另一篇博客有写,不重复累赘:Eureka集群搭建

Eureka的保护机制

最近项目在Kubernetes上使用Eureka遇到一些问题,在网站上找到一篇针对Eureka自我保护机制原理的文章,觉得不错,总结如下:

Eureka的自我保护特性主要用于减少在网络分区或者不稳定状况下的不一致性问题

  • Eureka自我保护的产生原因:

Eureka在运行期间会统计心跳失败的比例,在15分钟内是否低于85%,如果出现了低于的情况,Eureka Server会将当前的实例注册信息保护起来,同时提示一个警告,一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据。也就是不会注销任何微服务。

原文地址:Eureka的自我保护机制 - ericnie - 博客园 (cnblogs.com)

Eureka的自我保护机制默认是开启的。可以通过一些配置进行修改

eureka:
  server:
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 2000

Eureka,zookeeper,consul对比

组件名语言CAP一致性算法服务健康检查对外暴露接口Spring Cloud集成
EurekaJavaAP可配支持HTTP已集成
ConsulGoCPRaft支持HTTP/DNS已集成
ZookeeperJavaCPPaxos支持客户端已集成

Eureka中自带的Ribbon

在新版的Eureka中自包含了Ribbon,所以在导入spring-cloud-starter-netflix-eureka-client时就自带Ribbon做负载均匀。

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

使用

@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

posted @   鸭梨的药丸哥  阅读(6)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示