zoukankan      html  css  js  c++  java
  • springcloud中eureka服务注册中心

    一、配置

    1.服务端

    Eureka服务端,即服务注册中心。它同其他服务注册中心一样,支持高可用配置。依托于强一致性提供良好的服务实例可用性,可以应对多种不同的故障场景。

    Eureka服务端支持集群模式部署,当集群中有分片发生故障的时候,Eureka会自动转入自我保护模式。它允许在分片发生故障的时候继续提供服务的发现和注册,当故障分配恢复时,集群中的其他分片会把他们的状态再次同步回来。集群中的的不同服务注册中心通过异步模式互相复制各自的状态,这也意味着在给定的时间点每个实例关于所有服务的状态可能存在不一致的现象。

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    
    
    @SpringBootApplication
    @EnableEurekaServer
    public class StartCloudEureka1Application {
        public static void main(String[] args) {
            SpringApplication.run(StartCloudEureka1Application.class,args);
        }
    }
    #Eureka实例名,集群中根据这里相互识别
    eureka:
      instance:
        hostname: eureka
    #客户端
      client:
    #是否开启注册服务,因为这里如果为true表示自己注册自己,而自己就是一个服务注册方,没必要自己注册自己
        register-with-eureka: false
    #是否拉取服务列表,这里我只提供服务给别的服务。
        fetch-registry: false
    #注册中心地址
        service-url:
          defaultZone: http://localhost:8888/eureka/
    
    

    2.客户端:

    Eureka客户端,途中的即服务提供者,主要处理服务的注册和发现。客户端服务通过注册和参数配置的方式,嵌入在客户端应用程序的代码中。在应用程序启动时,Eureka客户端向服务注册中心注册自身提供的服务,并周期性的发送心跳来更新它的服务租约。同时,他也能从服务端查询当前注册的服务信息并把它们缓存到本地并周期行的刷新服务状态。

    @SpringBootApplication
    @EnableDiscoveryClient
    @Slf4j
    public class StartCloudWebApplication {
        public static void main(String[] args) {
            SpringApplication.run(StartCloudWebApplication.class,args);
            log.info("start success ......");
        }
    }
    
    
    eureka:
    #客户端
      client:
    #注册中心地址
        service-url:
          defaultZone: http://localhost:8888/eureka/
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

    高可用注册中心:

    server:
      port: 9400
    spring:
      application:
        name: eureka-service
    #Eureka实例名,集群中根据这里相互识别
    eureka:
      #客户端
      client:
        #是否开启注册服务,因为这里如果为true表示自己注册自己,而自己就是一个服务注册方,没必要自己注册自己
        #register-with-eureka: false
         #是否拉取服务列表,这里我只提供服务给别的服务。
        #fetch-registry: false
        #注册中心地址
        service-url:
           defaultZone: http://localhost:9403/eureka/
    server:
      port: 9403
    spring:
      application:
        name: eureka-service
    #Eureka实例名,集群中根据这里相互识别
    eureka:
        client:
          service-url:
            defaultZone: http://localhost:9400/eureka/

    Eureka包含四个部分的配置

    1. instance:当前Eureka Instance实例信息配置

    2. client:Eureka Client客户端特性配置

    3. server:Eureka Server注册中心特性配置

    4. dashboard:Eureka Server注册中心仪表盘配置

    1.Eureka Instance实例信息配置

    Eureka Instance的配置信息全部保存在org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean配置类里,实际上它是com.netflix.appinfo.EurekaInstanceConfig的实现类,替代了netflix的com.netflix.appinfo.CloudInstanceConfig的默认实现。

    Eureka Instance的配置信息全部以eureka.instance.xxx的格式配置。

    配置列表

    @ConfigurationProperties("eureka.instance")
    public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig, EnvironmentAware {
        private static final String UNKNOWN = "unknown";
        private HostInfo hostInfo;
        private InetUtils inetUtils;
        private String actuatorPrefix = "/actuator";
        //应用名,首先获取spring.application.name的值,如果取值为空,则取默认unknown。
        private String appname = "unknown";
        //应用组名
        private String appGroupName;
        //实例注册到Eureka上是,是否立刻开启通讯。有时候应用在准备好服务之前需要一些预处理。默认是false
        private boolean instanceEnabledOnit;
        //非安全的端口 80
        private int nonSecurePort = 80;
        //安全端口 443
        private int securePort = 443;
        //是否开启非安全端口通讯
        private boolean nonSecurePortEnabled = true;
        private boolean securePortEnabled;
        //实例续约间隔时间
        private int leaseRenewalIntervalInSeconds = 30;
        //实例超时时间,表示最大leaseExpirationDurationInSeconds秒后没有续约,Server就认为他不可用了,随     //之就会将其剔除。
        private int leaseExpirationDurationInSeconds = 90;
        //虚拟主机名,首先获取spring.application.name的值,如果取值为空,则取默认unknown。
        private String virtualHostName = "unknown";
        //注册到eureka上的唯一实例ID,不能与相同appname的其他实例重复。
        private String instanceId;
        //安全虚拟主机名,首先获取spring.application.name的值,如果取值为空,则取默认unknown。
        private String secureVirtualHostName = "unknown";
        private String aSGName;
        //实例元数据,可以供其他实例使用。比如spring-boot-admin在监控时,获取实例的上下文和端口。
        private Map<String, String> metadataMap = new HashMap();
        //实例部署的数据中心。如AWS、MyOwn。
        private DataCenterInfo dataCenterInfo;
        //实例的IP地址
        private String ipAddress;
        //实例状态页相对url /actuator/info
        private String statusPageUrlPath;
        //实例状态页绝对URL
        private String statusPageUrl;
        //实例主页相对URL /
        private String homePageUrlPath;
        //实例主页绝对URL
        private String homePageUrl;
        //实例健康检查相对URL /actuator/health
        private String healthCheckUrlPath;
        //实例健康检查绝对URL
        private String healthCheckUrl;
        //实例安全的健康检查绝对URL
        private String secureHealthCheckUrl;
        //配置属性的命名空间(Spring Cloud中被忽略)
        private String namespace;
        //主机名,不配置的时候讲根据操作系统的主机名来获取
        private String hostname;
        //是否优先使用IP地址作为主机名的标识
        private boolean preferIpAddress;
        private InstanceStatus initialStatus;
        private String[] defaultAddressResolutionOrder;
        private Environment environment;
    }

    2.Eureka Client客户端特性配置

    Eureka Client客户端特性配置是对作为Eureka客户端的特性配置,包括Eureka注册中心,本身也是一个Eureka Client。

    Eureka Client特性配置全部在org.springframework.cloud.netflix.eureka.EurekaClientConfigBean中,实际上它是com.netflix.discovery.EurekaClientConfig的实现类,替代了netxflix的默认实现。

    Eureka Client客户端特性配置全部以eureka.client.xxx的格式配置。

    配置列表

    @ConfigurationProperties("eureka.client")
    public class EurekaClientConfigBean implements EurekaClientConfig {
        public static final String PREFIX = "eureka.client";
        public static final String DEFAULT_URL = "http://localhost:8761/eureka/";
        public static final String DEFAULT_ZONE = "defaultZone";
        private static final int MINUTES = 60;
        @Autowired(
            required = false
        )
        PropertyResolver propertyResolver;  
        //是否启用Eureka client。
        private boolean enabled = true;
        @NestedConfigurationProperty
        private EurekaTransportConfig transport = new CloudEurekaTransportConfig();
        //定时从Eureka Server拉取服务注册信息的间隔时间
        private int registryFetchIntervalSeconds = 30;
        //定时将实例信息(如果变化了)复制到Eureka Server的间隔时间。(InstanceInfoReplicator线程)
        private int instanceInfoReplicationIntervalSeconds = 30;
        //首次将实例信息复制到Eureka Server的延迟时间。(InstanceInfoReplicator线程)
        private int initialInstanceInfoReplicationIntervalSeconds = 40;
        //拉取Eureka Server地址的间隔时间(Eureka Server有可能增减)
        private int eurekaServiceUrlPollIntervalSeconds = 300;
        //Eureka Server的代理端口
        private String proxyPort;
        //Eureka Server的代理主机名
        private String proxyHost;
        //Eureka Server的代理用户名
        private String proxyUserName;
        //Eureka Server的代理密码
        private String proxyPassword;
        //从Eureka Server读取信息的超时时间
        private int eurekaServerReadTimeoutSeconds = 8;
        //连接Eureka Server的超时时间
        private int eurekaServerConnectTimeoutSeconds = 5;
        //Eureka Client第一次启动时获取服务注册信息的调用的回溯实现。Eureka Client启动时首次会检查有没有       //BackupRegistry的实现类,如果有实现类,则优先从这个实现类里获取服务注册信息。
        private String backupRegistryImpl;
        //Eureka client连接Eureka Server的链接总数
        private int eurekaServerTotalConnections = 200;
        //Eureka client连接单台Eureka Server的链接总数
        private int eurekaServerTotalConnectionsPerHost = 50;
        //当Eureka server的列表在DNS中时,Eureka Server的上下文路径。如http://xxxx/eureka。
        private String eurekaServerURLContext;
        //当Eureka server的列表在DNS中时,Eureka Server的端口。
        private String eurekaServerPort;
        //当Eureka server的列表在DNS中时,且要通过DNSName获取Eureka Server列表时,DNS名字。
        private String eurekaServerDNSName;
        //实例所属区域。
        private String region = "us-east-1";
        //Eureka Client和Eureka Server之间的Http连接的空闲超时时间。
        private int eurekaConnectionIdleTimeoutSeconds = 30;
        private String registryRefreshSingleVipAddress;
        //心跳(续约)执行器线程池大小。
        private int heartbeatExecutorThreadPoolSize = 2;
        //心跳执行器在续约过程中超时后的再次执行续约的最大延迟倍数。默认最大延迟时间=10 *                     //eureka.instance.leaseRenewalIntervalInSeconds
        private int heartbeatExecutorExponentialBackOffBound = 10;
        //cacheRefreshExecutord的线程池大小(获取注册信息)
        private int cacheRefreshExecutorThreadPoolSize = 2;
        //cacheRefreshExecutord的再次执行的最大延迟倍数。默认最大延迟时间=10                               //*eureka.client.registryFetchIntervalSeconds
        private int cacheRefreshExecutorExponentialBackOffBound = 10
        //Eureka Server的分区地址。默认添加了一个defualtZone。也就是最常用的配置eureka.client.service-       //url.defaultZone=xxx
        private Map<String, String> serviceUrl = new HashMap();
        private boolean gZipContent;
        private boolean useDnsForFetchingServiceUrls;
        //是否注册到Eureka Server。默认为true
        private boolean registerWithEureka;
        //是否使用相同Zone下的Eureka server。默认为true
        private boolean preferSameZoneEureka;
        //是否记录Eureka Server和Eureka Client之间注册信息的差异 默认为false
        private boolean logDeltaDiff;
        //是否开启增量同步注册信息。默认为false
        private boolean disableDelta;
        //获取注册服务的远程地区,以逗号隔开。
        private String fetchRemoteRegionsRegistry;
        //可用分区列表。用逗号隔开。
        private Map<String, String> availabilityZones;
        //是否只拉取UP状态的实例。默认为true
        private boolean filterOnlyUpInstances;
        //是否拉取注册信息。默认为true
        private boolean fetchRegistry;
        private String dollarReplacement;
        private String escapeCharReplacement;
        private boolean allowRedirects;
        private boolean onDemandUpdateStatusChange;
        private String encoderName;
        private String decoderName;
        private String clientDataAccept;
        //是否在停止服务的时候向Eureka Server发起Cancel指令。默认为true
        private boolean shouldUnregisterOnShutdown;
        //是否在初始化过程中注册服务。默认为false
        private boolean shouldEnforceRegistrationAtInit;
    }

    3.Eureka Server注册中心端配置

    Eureka Server注册中心端的配置是对注册中心的特性配置。Eureka Server的配置全部在org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean里,实际上它是com.netflix.eureka.EurekaServerConfig的实现类,替代了netflix的默认实现。

    Eureka Server的配置全部以eureka.server.xxx的格式进行配置。

    @ConfigurationProperties("eureka.server")
    public class EurekaServerConfigBean implements EurekaServerConfig {
        public static final String PREFIX = "eureka.server";
        private static final int MINUTES = 60000;
        @Autowired(
            required = false
        )
        PropertyResolver propertyResolver;
        private String aWSAccessId;
        private String aWSSecretKey;
        private int eIPBindRebindRetries = 3;
        private int eIPBindingRetryIntervalMs = 300000;
        private int eIPBindingRetryIntervalMsWhenUnbound = 60000;
        //是否开启自我保护
        private boolean enableSelfPreservation = true;
        //自我保护续约百分比阀值因子。如果实际续约数小于续约数阀值,则开启自我保护
        private double renewalPercentThreshold = 0.85D;
        //续约数阀值更新频率。
        private int renewalThresholdUpdateIntervalMs = 900000;
        //Eureka Server节点更新频率。
        private int peerEurekaNodesUpdateIntervalMs = 600000;
        private int numberOfReplicationRetries = 5;
        private int peerEurekaStatusRefreshTimeIntervalMs = 30000;
        //当从其他节点同步实例信息为空时等待的时间。
        private int waitTimeInMsWhenSyncEmpty = 300000;
        //节点间连接的超时时间。
        private int peerNodeConnectTimeoutMs = 200;
        //节点间读取信息的超时时间。
        private int peerNodeReadTimeoutMs = 200;
        //节点间连接总数。
        private int peerNodeTotalConnections = 1000;
        //单个节点间连接总数。
        private int peerNodeTotalConnectionsPerHost = 500;
        //节点间连接空闲超时时间。
        private int peerNodeConnectionIdleTimeoutSeconds = 30;
        //增量队列的缓存时间。
        private long retentionTimeInMSInDeltaQueue = 180000L;
        //清理增量队列中过期的频率。
        private long deltaRetentionTimerIntervalInMs = 30000L;
        //剔除任务频率。
        private long evictionIntervalTimerInMs = 60000L;
        private int aSGQueryTimeoutMs = 300;
        private long aSGUpdateIntervalMs = 300000L;
        private long aSGCacheExpiryTimeoutMs = 600000L;
        //注册列表缓存超时时间(当注册列表没有变化时)
        private long responseCacheAutoExpirationInSeconds = 180L;
        //注册列表缓存更新频率。
        private long responseCacheUpdateIntervalMs = 30000L;
        //是否开启注册列表的二级缓存。
        private boolean useReadOnlyResponseCache = true;
        //是否为client提供增量信息。
        private boolean disableDelta;
        private long maxIdleThreadInMinutesAgeForStatusReplication = 10L;
        private int minThreadsForStatusReplication = 1;
         //状态同步的最大线程数。
        private int maxThreadsForStatusReplication = 1;
        //状态同步队列的最大容量。
        private int maxElementsInStatusReplicationPool = 10000;
        //当时间差异时是否同步。
        private boolean syncWhenTimestampDiffers = true;
        //注册信息同步重试次数。
        private int registrySyncRetries = 0;
        //注册信息同步重试期间的时间间隔。
        private long registrySyncRetryWaitMs = 30000L;
        //节点间同步事件的最大容量。
        private int maxElementsInPeerReplicationPool = 10000;
        private long maxIdleThreadAgeInMinutesForPeerReplication = 15L;
        //节点间同步的最小线程数。
        private int minThreadsForPeerReplication = 5;
        //节点间同步的最大线程数。
        private int maxThreadsForPeerReplication = 20;
        //节点间同步的最大时间,单位为毫秒。
        private int maxTimeForReplication = 30000;
        private boolean primeAwsReplicaConnections = true;
        //是否启用远程区域增量。
        private boolean disableDeltaForRemoteRegions;
        //远程区域连接超时时间。
        private int remoteRegionConnectTimeoutMs = 1000;
        //远程区域读取超时时间。
        private int remoteRegionReadTimeoutMs = 1000;
        //远程区域最大连接数
        private int remoteRegionTotalConnections = 1000;
        //远程区域单机连接数
        private int remoteRegionTotalConnectionsPerHost = 500;
        //远程区域连接空闲超时时间。
        private int remoteRegionConnectionIdleTimeoutSeconds = 30;
        private boolean gZipContentFromRemoteRegion = true;
        private Map<String, String> remoteRegionUrlsWithName = new HashMap();
        private String[] remoteRegionUrls;
        private Map<String, Set<String>> remoteRegionAppWhitelist;
        //远程区域注册信息拉取频率。
        private int remoteRegionRegistryFetchInterval = 30;
        //远程区域注册信息线程数。
        private int remoteRegionFetchThreadPoolSize = 20;
        private String remoteRegionTrustStore = "";
        private String remoteRegionTrustStorePassword = "changeit";
        private boolean disableTransparentFallbackToOtherRegion;
        private boolean batchReplication;
        private boolean rateLimiterEnabled = false;
        private boolean rateLimiterThrottleStandardClients = false;
        private Set<String> rateLimiterPrivilegedClients = Collections.emptySet();
        private int rateLimiterBurstSize = 10;
        private int rateLimiterRegistryFetchAverageRate = 500;
        private int rateLimiterFullFetchAverageRate = 100;
        private boolean logIdentityHeaders = true;
        private String listAutoScalingGroupsRoleName = "ListAutoScalingGroups";
        private boolean enableReplicatedRequestCompression = false;
        private String jsonCodecName;
        private String xmlCodecName;
        private int route53BindRebindRetries = 3;
        private int route53BindingRetryIntervalMs = 300000;
        private long route53DomainTTL = 30L;
        private AwsBindingStrategy bindingStrategy;
        private int minAvailableInstancesForPeerReplication;
    }

    4.Eureka Server注册中心仪表盘配置

    注册中心仪表盘的配置主要是控制注册中心的可视化展示。以eureka.dashboard.xxx的格式配置。

    @ConfigurationProperties("eureka.dashboard")
    public class EurekaDashboardProperties {
        //仪表盘访问路径
        private String path = "/";
        //是否启用仪表盘
        private boolean enabled = true;
    }
  • 相关阅读:
    websocket介绍
    阿里支付接口
    王爽 汇编 检测点 14.2
    如何用汇编写出一个心形图像
    王爽 汇编 实验12
    王爽 汇编 实验11
    王爽 汇编 实验10
    王爽汇编 检测点10.5
    二元选择排序
    螺旋矩阵
  • 原文地址:https://www.cnblogs.com/mufeng07/p/12859108.html
Copyright © 2011-2022 走看看