• SpringCloud-02-整合Eureka



    搭建Eureka

    引入模块都是这个套路

    1.导入依赖;
    2.编写配置文件;
    3.开启这个功能@EnableXxx;
    4.配置类;

    版本兼容问题是个特别恶心又浪费时间的东西,解决方法是在父工程中尝试

    <!--springcloud的依赖-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
    <!--springboot-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring-boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>

    导入依赖

    <!--eureka-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
        <version>1.4.7.RELEASE</version>
    </dependency>
    <!--热部署工具-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>

    配置文件

    server:
      port: 7001
    #Eureka
    eureka:
      instance:
        hostname: localhost  #服务端的实例名称
      client:
        register-with-eureka: false #表示是否向eureka注册中心注册自己
        fetch-registry: false #false表示自己为注册中心
        service-url: #监控页面
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

    开启注解

    @SpringBootApplication
    @EnableEurekaServer//服务端的启动类  可以接受别人注册进来
    public class EurekaServer_7001 {
        public static void main(String[] args) {
            SpringApplication.run(EurekaServer_7001.class,args);
        }
    }

    管理页面 http://localhost:7001/


    服务提供者整合Eureka

    引入依赖

    <!-- eureka -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.4.7.RELEASE</version>
    </dependency>

    配置文件

    #eureka
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka/

    开启注解

    @SpringBootApplication
    @EnableEurekaClient//自动注册到eureka中
    public class DeptProvider_8001 {
        public static void main(String[] args) {
            SpringApplication.run(DeptProvider_8001.class,args);
        }
    }

    宁可同时保留所有微服务,也不盲目注销任何健康的服务丶

    eureka.server.enable-self-preservation = false #禁用自我保护模式,不推荐!

    (扩展功能:info信息、团队服务发现  见源码丶)


    搭建Eureka集群

    老老实实修改host文件!C:WindowsSystem32driversetc

    #Eureka集群
    127.0.0.1 eureka7001.com
    127.0.0.1 eureka7002.com
    127.0.0.1 eureka7003.com

    springcloud-eureka-7003

    server:
      port: 7003
    #Eureka
    eureka:
      instance:
        hostname: eureka7003.com  #服务端的实例名称
      client:
        register-with-eureka: false #表示是否向eureka注册中心注册自己
        fetch-registry: false #false表示自己为注册中心
        service-url: #监控页面
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

    springcloud-provider-dept-8001服务注册

    #eureka
    eureka:
      client:
        service-url:
          #往Eureka单机发布 defaultZone: http://localhost:7001/eureka/
          #往Eureka集群发布 
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
      instance:
        instance-id: Springcloud-provider-dept8001

    引入Eureka,服务提供者和Eureka本身就可以实现集群了丶


    Eureka和Zookeeper的区别

    CAP原则(C强一致性,A可用性,P分区容错性;不可能同时满足三个)
    Zookeeper保证的是CP(可用性不强,选举leader时不可用)(直男)
    Eureka保证的是AP(一致性不强,过时的注册)(渣男)

    https://github.com/ChenCurry/springcloud.git


    击石乃有火,不击元无烟!!
  • 相关阅读:
    JavaScript] 让iframe框架网页在任何浏览器下自动伸缩
    struts基类覆盖+数据库操作类!呵呵…………
    JavaScript] 判断file框选择的是否为图片
    取得地址栏的URL地址方法
    网页制作之在线视频播放代码
    【T_SQL】Clear DataBase Log
    我认为够长!!!~~:>
    OWC:产品销量统计图
    AJAX之xmlHttp
    ASP.NET 2.0 只读 TextBox 回发后信息丢失
  • 原文地址:https://www.cnblogs.com/rain2020/p/13510207.html
Copyright © 2020-2023  润新知