美文网首页Spring Cloud
Eureka+Config集群搭建配置中心

Eureka+Config集群搭建配置中心

作者: 兔斯基_大大 | 来源:发表于2019-03-25 11:39 被阅读0次

Eureka+Config集群搭建配置中心

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

Config项目

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hz</groupId>
    <artifactId>ViConfig</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>eureka1</module>
        <module>configServer1</module>
        <module>configServer2</module>
        <module>eureka2</module>
        <module>configClient</module>

    </modules>

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath/>
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!--需要将springboot内置的日志剃掉,然后引入log4j2 -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--添加对日志系统的支持,采用最新的log4j2框架-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


    </dependencies>
</project>

Eureka1

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>ViConfig</artifactId>
        <groupId>com.hz</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka1</artifactId>
    <packaging>jar</packaging>


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

</project>

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

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>ViConfig</artifactId>
        <groupId>com.hz</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>configServer1</artifactId>
    <packaging>jar</packaging>

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

bootstrap.yml

server:
  port: 8769

spring:
  application:
    name: config-server

  cloud:
    config:
      server:
        git:
          uri: http://admin@192.168.1.18: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集群搭建配置中心

    本文链接:https://www.haomeiwen.com/subject/dhdvvqtx.html