Eureka+Config集群搭建配置中心

Eureka+Config集群搭建配置中心

根据前面搭建Eureka 和 Config集群的经验,下面讲Eureka+Config集群搭建的配置中心。

Config项目

pom.xml



    4.0.0

    com.hz
    ViConfig
    pom
    1.0-SNAPSHOT
    
        eureka1
        configServer1
        configServer2
        eureka2
        configClient

    

    
    org.springframework.boot
    spring-boot-starter-parent
    2.1.0.RELEASE
    
    

    
        1.8
        UTF-8
        Finchley.RELEASE
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.RELEASE
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            repackage
                        
                    
                
            
        
    

    
        
            spring-snapshots
            Spring Snapshots
            https://repo.spring.io/snapshot
            
                true
            
        
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
        
    
    
        
            spring-snapshots
            Spring Snapshots
            https://repo.spring.io/snapshot
            
                true
            
        
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
        
    

    
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        

        
        
            org.springframework.boot
            spring-boot-starter-log4j2
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        


    

Eureka1

pom.xml




    
        ViConfig
        com.hz
        1.0-SNAPSHOT
    
    4.0.0

    eureka1
    jar


    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
 
    



application-peer1.yml

spring:
  application:
    name: eureka-server

server:
  port: 7001
eureka:
  instance:
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    hostname: peer1
#    prefer-ip-address: true   #以IP地址注册到服务中心,相互注册使用IP地址
  #    instance-id: ${spring.cloud.client.ipAddress}:${server.port}
  #    ip-address: #强制指定IP地址,默认会获取本机的IP地址
  client:
    register-with-eureka: true         # 表示是否注册自身到eureka服务器
    fetch-registry: true         # 是否从eureka上获取注册信息
    serviceUrl:
      defaultZone: http://peer2:7002/eureka/   
  server:
    eviction-interval-timer-in-ms: 3000     # 续期时间,即扫描失效服务的间隔时间(单位毫秒,默认是60*1000)
    enableSelfPreservation: false    # 设为false,关闭自我保护
#    renewalPercentThreshold: 0.49

logging:
  config: classpath:log4j2.xml

Eureka2

同上,需要修改下application.yml的名字为application-peer2.yml ,server.port=7001

configServer1

pom.xml




    
        ViConfig
        com.hz
        1.0-SNAPSHOT
    
    4.0.0

    configServer1
    jar

    
        
            org.springframework.cloud
            spring-cloud-config-server
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
    


bootstrap.yml

server:
  port: 8769

spring:
  application:
    name: config-server

  cloud:
    config:
      server:
        git:
          uri: http://[email protected]:10010/r/vi.git
          username: vi
          password: hz
          default-label: master

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/
    healthcheck:
      enabled: true                           # 开启健康检查(依赖spring-boot-starter-actuator)
  instance:
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true #以IP地址注册到服务中心
    lease-renewal-interval-in-seconds: 5      # 心跳时间,即服务续约间隔时间(缺省为30s)
    lease-expiration-duration-in-seconds: 15  # 发呆时间,即服务续约到期时间(缺省为90s)

configServer2

同configServer1,需要修改下bootstrap.yml的server.port=8768

configClient

bootstrap.yml

spring:
  application:
  name: config-client
  cloud:
    config:
      name: application                   # 对应 {application} 部分
      profile:   dis-dev                     #指定的环境
      label: master                      #指定分支
      discovery:
        enabled: true
        service-id: config-server

management:
  endpoints:
    web:
      exposure:
        include: refresh

eureka:
  client:
    serviceUrl:
        defaultZone: http://192.168.1.18:7001/eureka/,http://192.168.1.18:7002/eureka/
  instance:
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true #以IP地址注册到服务中心

    lease-renewal-interval-in-seconds: 5      # 心跳时间,即服务续约间隔时间(缺省为30s)
    lease-expiration-duration-in-seconds: 15  # 发呆时间,即服务续约到期时间(缺省为90s)

logging:
  config: classpath:log4j2-dev.xml

一次启动项目,打开eureka监控网址,http://192.168.1.18:7001/

1552374543855.png

你可能感兴趣的:(Eureka+Config集群搭建配置中心)