zoukankan      html  css  js  c++  java
  • Spring Cloud 4:Eureka 服务注册/发现

    Eureka Server

    pom.xml

        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
            </dependency>
        </dependencies>

    Application.java

    @SpringBootApplication
    // 通过@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话
    @EnableEurekaServer
    public class ServerApplication {
    
        /*
        Eureka
        是纯正的 servlet 应用,需构建成jar/war包部署
        使用了 Jersey 框架实现自身的 RESTful HTTP接口
        peer之间的同步与服务的注册全部通过 HTTP 协议实现
        定时任务(发送心跳、定时清理过期服务、节点同步等)通过 JDK 自带的 Timer 实现
        内存缓存使用Google的guava包实现
         */
    
        public static void main(String[] args) {
            new SpringApplicationBuilder(ServerApplication.class)
                    .web(true).run(args);
        }
    }

    bootstrap.yml

    server:
        tomcat:
            uri-encoding: utf-8
            max-threads: 1000
            min-spare-threads: 30
        port: 1001
    
    spring:
        # 指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问
        application:
            name: yang-server
    
    eureka:
        server:
          #是否开启自我保护,默认为 true,在开启自我保护的情况下,注册中心在丢失客户端时,会进入自动保护模式,注册中心并不会将该服务从注册中心删除掉。
          enable-self-preservation: false
        instance:
            #是否以 IP 注册到注册中心,Eureka 默认是以 hostname 来注册的
            preferIpAddress: true
            hostname: localhost
            instanceId: ${spring.cloud.client.ipAddress}:${server.port}
        client:
            register-with-eureka: false   # 表示是否将自己注册到Eureka Server,默认为true
            fetch-registry: false          # 表示是否从Eureka Server获取注册信息,默认为true
            service-url:
                # 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔
                defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    • eureka.server.enable-self-preservation:是否开启自我保护,默认为 true,在开启自我保护的情况下,注册中心在丢失客户端时,会进入自动保护模式,注册中心并不会将该服务从注册中心删除掉。这里我设置为 false,即关闭自我保护。根据我的经验,如果设置为 true,在负载均衡条件下,一个服务挂掉后,注册中心并没有删掉该服务,会导致客户端请求的时候可能会请求到该服务,导致系统无法访问,所以我推荐将这个属性设置为 false。
    • eureka.instance.preferIpAddress:是否以 IP 注册到注册中心,Eureka 默认是以 hostname 来注册的。
    • client.serviceUrl.defaultZone:注册中心默认地址。

    Eureka Provider

    pom.xml

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

    Application.java

    @SpringBootApplication
    // 标注该项目是一个服务提供者。
    @EnableEurekaClient
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

    bootstrap.yml

    eureka:
      client:
        serviceUrl:
          // 指定注册中心的地址
          defaultZone: http://localhost:8761/eureka/
    server:
      port: 8762
    spring:
      application:
        name: eurekaclient
  • 相关阅读:
    C/C++程序内存的分配
    【解题报告】UVA101 The Blocks Problem
    洗牌算法
    【持续更新】算法竞赛常用模板
    北京邮电大学2021乐理公共选修课期末考试
    Oracle通用大数据量存储过程分页修正版
    (转)NHibernate 3.0在PetShop 3层架构中的应用
    Json.Net学习笔记
    JS获得浏览器高度和宽度参数
    jQuery数组处理汇总
  • 原文地址:https://www.cnblogs.com/yang21/p/10024275.html
Copyright © 2011-2022 走看看