Spring Cloud Tutorial | Part 2: Building a Configuration Center

Keywords: Spring git SQL Mybatis

1. Building config configuration center

1.1 Building springboot Template Project

ignore
Project structure:

1.2 Modify 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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hanergy</groupId>
    <artifactId>out</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-center</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <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>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <finalName>config-server</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

1.3 Startup Class Add Annotations

2. git Server Combined with Config

Using git server, you can build gitlab server by yourself, or use github, open source China git, Aliyun Git

2.1 Create Configuration Center Project on GitHub

Login git to create configuration center repository

Create dev nodes.

Description: Distinguish production and test environments by branches.
master: production environment configuration
dev: Test environment configuration

2.2 Configuration Center Configuration

2.2.1 Production Environment Configuration

Production environment: master branch configuration
Service configuration template:

template-prod.yml(Registry name templateļ¼ŒConfiguration in Project label:  prodļ¼Œprofile: master)
----------------------------------------------------------------------------------------------------------------------------------------
server:
  tomcat:
    uri-encoding: UTF-8
    max-threads: 1000
    min-spare-threads: 30
  port: 8001
  connection-timeout: 5000ms
  

spring:
  # jackson time formatting
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss
  servlet:
    multipart:
      max-file-size: 100MB
      max-request-size: 100MB
      enabled: true
  mvc:
    throw-exception-if-no-handler-found: true
    static-path-pattern: /static/**
  resources:
    add-mappings: false
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://10.4.237.111:3306/hanergy_system?useUnicode=true&characterEncoding=utf-8
    username: ycroot
    password: hnyc1!
  redis:
    open: false
    host: 10.4.237.114
    port: 6379
    password: bgredis!
  zipkin:
    base-url: http://localhost:9411/
    locator:
      discovery:
        enabled: true
  sleuth:
    sampler:
      probability: 0.1   #Sampling percentage is set to 1 for 100%, and production environment can be set to 0.1.
      

system:
  standard: http://bgs.hanergy.com/standard/api/v1/dicationary/doRefreshRedis?refresh={0}
      
#Modify call timeout
feign:
  client:
    config:
      default:
        # Service call timeout configuration
        connectTimeout: 4000
        readTimeout: 4000
  hystrix:
    #The hystrix fallback method is not configured here to execute
    enabled: true

#hystrix timeout
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            # hystrix timeout
            timeoutInMilliseconds: 4000

#Instrument panel configuration
management:
  endpoints:
    web:
      exposure:
        include: "*"


#mybatis
mybatis-plus:
  mapper-locations: classpath:mybatis/*.xml
  #Entity scanning, with multiple package s separated by commas or semicolons
  typeAliasesPackage: com.hanergy.out.entity
  global-config:
    #Custom SQL Injector
    db-config:
      #Primary key type 0: "database ID self-increasing", "user input ID", "global unique ID", "global unique ID", "global unique ID", "global unique ID UUID";
      id-type: input
      #Field Strategy 0: "Ignore Judgment", "Non-NULL Judgment", "Non-empty Judgment"
      field-strategy: NOT_NULL
      #Logical Delete Configuration
      logic-delete-value: -1
      logic-not-delete-value: 0
    #Refresh mapper debugging artifact
    refresh: true
  #    sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false
    call-setters-on-nulls: true
    #sql Print Configuration
    #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

logging:
  level:
    org.springframework.boot.autoconfigure: ERROR
    com.hanergy.activiti.data: debug
    ## Remove CONDITIONS EVALUATION REPORT log printing
    io.swagger.models.parameters.AbstractSerializableParameter: error
----------------------------------------------------------------------------------------------------------------------------------------

gatway Gateway Configuration Template:

gatway-prod.yml
----------------------------------------------------------------------------------------------------------------------------------------
# port
server:
  tomcat:
    uri-encoding: UTF-8
    max-threads: 1000
    min-spare-threads: 30
  port: 9000
  connection-timeout: 5000ms

# Service Name
spring:
  zipkin:
    # zipkin link tracking address, usually download zipkin jar package java-jar startup service online
    base-url: http://localhost:9411/
  sleuth:
    sampler:
      probability: 1   #Sampling percentage is set to 1 for 100%, and production environment can be set to 0.1.



# Automatic access to order service when gateway configures access service name to access oameeting can configure tool project access mode
zuul:
  routes:
    #order: /oameeting/**
    #Configure gatway interception, access address with oameeting will automatically call the interface registered with the name oa
    oa: /oameeting/**
  ignored-services: order   # Cancel skipping gateway to call service interface directly. Cancel the whole oa access mode /* - service /**
  host:
    connect-timeout-millis: 8000    #zuul default timeout length default 2s
    socket-timeout-millis: 10000     # socket link timeout default 10s

ribbon:
  ReadTimeout: 10000
  ConnectTimeout: 10000

----------------------------------------------------------------------------------------------------------------------------------------

2.2.2 Test Environment Configuration

Test environment: dev branch configuration
Service configuration template:

template-dev.yml
----------------------------------------------------------------------------------------------------------------------------------------
server:
  tomcat:
    uri-encoding: UTF-8
    max-threads: 1000
    min-spare-threads: 30
  port: 8001
  connection-timeout: 5000ms
  

spring:
  # jackson time formatting
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss
  servlet:
    multipart:
      max-file-size: 100MB
      max-request-size: 100MB
      enabled: true
  mvc:
    throw-exception-if-no-handler-found: true
    static-path-pattern: /static/**
  resources:
    add-mappings: false
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.18.202:3306/hanergy_system?useUnicode=true&characterEncoding=utf-8
    username: root
    password: root123
  redis:
    sentinel:
      nodes: 192.168.18.202:26380
      master: master1
  zipkin:
    #Configure zipkin link tracking address ${spring.cloud.client.ip-address}: service address
    base-url: http://${spring.cloud.client.ip-address}:9411/
    locator:
      discovery:
        enabled: true
  sleuth:
    sampler:
      probability: 0.1   #Sampling percentage is set to 1 for 100%, and production environment can be set to 0.1.
      

system:
   standard: http://198.168.18.204/standard/api/v1/dicationary/doRefreshRedis?refresh={0}
      
#Modify call timeout
feign:
  client:
    config:
      default:
        # Service call timeout configuration
        connectTimeout: 4000
        readTimeout: 4000
  hystrix:
    #The hystrix fallback method is not configured here to execute
    enabled: true

#hystrix timeout
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            # hystrix timeout
            timeoutInMilliseconds: 4000

#Instrument panel configuration
management:
  endpoints:
    web:
      exposure:
        include: "*"


#mybatis
mybatis-plus:
  mapper-locations: classpath:mybatis/*.xml
  #Entity scanning, with multiple package s separated by commas or semicolons
  typeAliasesPackage: com.hanergy.out.entity
  global-config:
    #Custom SQL Injector
    db-config:
      #Primary key type 0: "database ID self-increasing", "user input ID", "global unique ID", "global unique ID", "global unique ID", "global unique ID UUID";
      id-type: input
      #Field Strategy 0: "Ignore Judgment", "Non-NULL Judgment", "Non-empty Judgment"
      field-strategy: NOT_NULL
      #Logical Delete Configuration
      logic-delete-value: -1
      logic-not-delete-value: 0
    #Refresh mapper debugging artifact
    refresh: true
  #    sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false
    call-setters-on-nulls: true
    #sql Print Configuration
    #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

logging:
  level:
    org.springframework.boot.autoconfigure: ERROR
    com.hanergy.activiti.data: debug
    ## Remove CONDITIONS EVALUATION REPORT log printing
    io.swagger.models.parameters.AbstractSerializableParameter: error

----------------------------------------------------------------------------------------------------------------------------------------

gatway Gateway Configuration Template:

gatway-dev.yml
----------------------------------------------------------------------------------------------------------------------------------------
# port
server:
  tomcat:
    uri-encoding: UTF-8
    max-threads: 1000
    min-spare-threads: 30
  port: 9000
  connection-timeout: 5000ms

# Service Name
spring:
  zipkin:
    # Configure ziplin link tracking address
    base-url: http://10.4.237.123:9411/
  sleuth:
    sampler:
      probability: 1   #Sampling percentage is set to 1 for 100%, and production environment can be set to 0.1.




# Automatic access to order service when gateway configures access service name to access oameeting can configure tool project access mode
zuul:
  routes:
    #order: /oameeting/**
    #Configure gatway interception, access address with oameeting will automatically call the interface registered with the name oa
    oa: /oameeting/**
  ignored-services: oa   # Cancel skipping gateway to call service interface directly. Cancel the whole oa access mode /* - service /**
  host:
    connect-timeout-millis: 8000    #zuul default timeout length default 2s
    socket-timeout-millis: 10000     # socket link timeout default 10s

ribbon:
  ReadTimeout: 10000
  ConnectTimeout: 10000

----------------------------------------------------------------------------------------------------------------------------------------

III. Project Integration git Configuration Center

3.1 Configuration Center Project Configuration

application.yml
----------------------------------------------------------------------------------------------------------------------------------------
eureka:
  instance:
    # Configuration Registry Status shows that otherwise the docker configuration center id will be read and the configuration center configuration localhost:8991 will not be read.
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    # Open the Registry Status to display modifications
    prefer-ip-address: true
    hostname: ${spring.cloud.client.ip-address}
  client:
    service-url:
      # Registry Address
      #defaultZone: http://127.0.0.1:8761/eureka
      defaultZone: http://10.4.237.123:8761/eureka


spring:
  application:
    # Configuration Center Registration Name
    name: springcloud-config
  cloud:
    config:
      discovery:
        service-id: springcloud-config
        enabled: false
      server:
        git:
          # Configuration Center git Warehouse Address
          uri: https://git.hanergy.com/independent/config_springcloud.git
          # git user name
          username: xxxxxx
          # git password
          password: xxxxxx
          #Overtime
          timeout: 5
          #branch
          default-label: master
#      label: master   #master branch
#      profile: dev    #dev environment, corresponding to the file name in git
server:
  # port
  port: 8991

----------------------------------------------------------------------------------------------------------------------------------------

Posted by jbradley04 on Wed, 09 Oct 2019 17:30:54 -0700