• 注册中心

    Eureka 版本

    依赖

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

    配置

    eureka:
      instance:
        # 以IP注册到注册中心
        preferIpAddress: ${EUREKA_INSTANCE_PREFER_IP_ADDRESS:true}
        leaseRenewalIntervalInSeconds: 10
        leaseExpirationDurationInSeconds: 30
        # 服务的一些元数据信息
        metadata-map:
          VERSION: 1.0.0
      client:
        serviceUrl:
          # 注册中心地址
          defaultZone: ${EUREKA_DEFAULT_ZONE:http://dev.hzero.org:8000/eureka}
        registryFetchIntervalSeconds: 10
        disable-delta: true
    

    Zookeeper 版本

    依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    </dependency>
    

    配置

    spring:
      cloud:
        zookeeper:
          connect-string: localhost:2181
          discovery:
            enabled: true
            metadata:
              VERSION: 1.0.0
    

    示例:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.12</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    

    Nacos(edas) 版本

    依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    

    配置

    spring:
      cloud:
    	nacos:
      	  discovery:
            server-addr: 127.0.0.1:8848
            namespace: 3767dfeb-ec5f-4611-97bb-ee530d19ef89
            metadata:
              VERSION: 0.10.0
    

    Formula(cnap) 版本

    依赖

    <dependency>
        <groupId>com.baidu.formula</groupId>
        <artifactId>discovery-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baidu.formula</groupId>
        <artifactId>env-core-spring-boot-starter</artifactId>
    </dependency>
    

    配置

    formula:
      discovery:
        service-url: http://cnapregistry.bj.baidubce.com:443/api/service-center
        customs: 
          VERSION: 0.10.0
    
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </dependency>
    

    配置中心

    SpringCloud 版本

    依赖

    <dependency>
        <groupId>org.hzero.starter</groupId>
        <artifactId>hzero-starter-config-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-client</artifactId>
    </dependency>
    

    配置

    spring:
      profiles:
        active: ${SPRING_PROFILES_ACTIVE:default}
      cloud:
        config:
          fail-fast: false
          # 是否启用配置中心
          enabled: ${SPRING_CLOUD_CONFIG_ENABLED:true}
          # 配置中心地址
          uri: ${SPRING_CLOUD_CONFIG_URI:http://dev.hzero.org:8010}
          retry:
            # 最大重试次数
            maxAttempts: 6
            multiplier: 1.1
            # 重试间隔时间
            maxInterval: 2000
          # 标签
          label: ${SPRING_CLOUD_CONFIG_LABEL:}
    

    Nacos(edas) 版本

    依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
    

    配置

    spring:
      profiles:
        active: ${SPRING_PROFILES_ACTIVE:default}
      cloud:
        nacos:
          config:
            server-addr: 127.0.0.1:8848
            namespace: 3767dfeb-ec5f-4611-97bb-ee530d19ef89
    

    Formula(cnap) 版本

    依赖

    <dependency>
        <groupId>com.baidu.formula</groupId>
        <artifactId>config-client-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baidu.formula</groupId>
        <artifactId>env-core-spring-boot-starter</artifactId>
    </dependency>
    

    配置

    formula-config基于Spring Cloud Config实现,所以配置方式与 Spring Cloud Config的配置方式相似。

    spring:
      profiles:
        active: ${SPRING_PROFILES_ACTIVE:default}
      cloud:
        config:
          fail-fast: false
          # 是否启用配置中心
          enabled: ${SPRING_CLOUD_CONFIG_ENABLED:true}
          # 配置中心地址
          uri: ${SPRING_CLOUD_CONFIG_URI:http://cnapregistry.bj.baidubce.com:443/api/config}
          retry:
            # 最大重试次数
            maxAttempts: 6
            multiplier: 1.1
            # 重试间隔时间
            maxInterval: 2000
          # 标签
          label: ${SPRING_CLOUD_CONFIG_LABEL:}
    

    注意:必须确保引入了spring-cloud-starter-config-client且开启配置中心,否则会启动失败。 原因:formula目前打包的all依赖formula-cnap依赖了formula-parent,但formula-parent暂未发布,所以无法为我们自动引入spring-cloud-starter-config-client等依赖。

    spring.cloud.config.enabled: true
    

    Apollo

    简介 apollo本身提供了apollo-client的sdk,但apollo配置中心定位是在Spring、Spring Boot及更轻量的微服务架构中,如果需要整合到Spring Cloud体系中(如网关),则需要用户增强实现一些功能,所以HZERO提供了hzero-starter-apollo-client包来帮助用户将apollo轻松整合到Spring Cloud应用中。

    依赖

    <dependency>
        <groupId>org.hzero.starter</groupId>
        <artifactId>hzero-starter-apollo-config</artifactId>
    </dependency>
    

    配置

    spring:
      cloud:
        apollo:
          config:
            enable: true
            listener:
              interestedKeyPrefixes: zuul.
              interestedKeys: zuul.test.path, zuul.test.service-id
    

    Note: 使用细节可以参考hzero-starter-apollo-client

    数据库

    MySql、TiDB

    依赖

    <dependency>
        <artifactId>mysql-connector-java</artifactId>
        <groupId>mysql</groupId>
    </dependency>
    

    数据源配置

    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://db.hzero.org:3306/hzero_xxxx?useUnicode=true&characterEncoding=utf-8&useSSL=false
        username: hzero
        password: hzero
    

    Oracle

    依赖

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc7</artifactId>
    </dependency>
    

    数据源配置

    spring:
      datasource:
        driver-class-name: oracle.jdbc.driver.OracleDriver
        url: jdbc:oracle:thin:@db.hzero.org:1521:XE
        username: hzero
        password: hzero
    

    SqlServer

    依赖

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>sqljdbc4</artifactId>
    </dependency>
    

    数据源配置

    spring:
      datasource:
        driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
        url: jdbc:sqlserver://db.hzero.org:1433;DatabaseName=hzero_xxxx
        username: SA
        password: hzero