编程知识 cdmana.com

Springclub07 config configuration center

One 、 config Configuration center

yml Save the configuration file to git The server , for example github.com or gitee.com
When the microservice starts , Get the configuration file from the server

1. To configure config

1.1 newly build config Folder

image.png

1.2 take sp02,sp03,sp04,sp11 Four projects yml The configuration file , Copied to the config project , And renamed

  • item-service-dev.yml
  • user-service-dev.yml
  • order-service-dev.yml
  • zuul-dev.yml

image.png
Last , Empty the four items application.yml file

1.3 The configuration of the central client is not allowed to be overridden

The configuration priority of the default configuration center is high , The configuration center configuration will cover all configuration of the client , Including command line parameter configuration , So we are item-service and order-service The port number boot parameter configured in will be invalid , Therefore, we can set the disable configuration center to override the client configuration , Add the following configuration to the four configuration files

spring:
  application:
    name: xxx
  cloud:
    config:
      # Prevent the configuration center from overriding the client configuration 
 override-none: true

1.4 take config Upload the project to gitee

image.png

2. To configure config The server

config Configuration center from git Download all configuration files . Other microservices start from config Configuration center obtains configuration information .

2.1 newly build sp12-config project

image.png

2.2 Add dependency

 Selective dependency

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 https://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.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.tedu</groupId>
    <artifactId>sp12-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sp12-config</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.RELEASE</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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.3 modify application.yml file

spring:
  application:
    name: config-server
  
  cloud:
    config:
      server:
        git:
          uri: https://github.com/ Your personal path /sp-config
          searchPaths: config
          #username: your-username
          #password: your-password
    
server:
  port: 6001
    
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka

2.4 ### The main program adds @EnableConfigServer annotation

package cn.tedu.sp12;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class Sp12ConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(Sp12ConfigApplication.class, args);
    }
}

2.5 start-up , Access test

visit item-service-dev.yml You can use the following form :
http://localhost:6001/item-service-dev.yml
http://localhost:6001/item-service/dev
Test other files
http://localhost:6001/user-service/dev
http://localhost:6001/zuul/dev
http://localhost:6001/order-service/dev

3. To configure config client

Modify the following items , Get configuration information from configuration center

  • sp02-itemservice
  • sp03-userservice
  • sp04-orderservice
  • sp11-zuul

3.1 add to config Client dependency

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

3.2 Add four items to the project bootstrap.yml

bootstrap.yml, Boot profile , Precede application.yml load

  • item-service
#bootstrap.yml
#1.eureka Address 
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
spring:
  cloud:
    config:
      discovery:
        #2. From the registry , Discovery configuration center 
 enabled: true
        service-id: config-server
        #3. From the configuration center : download  item-service-dev.yml name: item-service
      profile: dev
  • user-service
#bootstrap.yml
#1.eureka Address 
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
spring:
  cloud:
    config:
      discovery:
        #2. From the registry , Discovery configuration center 
 enabled: true
        service-id: config-server
        #3. From the configuration center : download  user-service-dev.yml name: user-service
      profile: dev
  • order-service
#bootstrap.yml
#1.eureka Address 
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
spring:
  cloud:
    config:
      discovery:
        #2. From the registry , Discovery configuration center 
 enabled: true
        service-id: config-server
        #3. From the configuration center : download  order-service-dev.yml name: order-service
      profile: dev
  • zuul
#bootstrap.yml
#1.eureka Address 
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
spring:
  cloud:
    config:
      discovery:
        #2. From the registry , Discovery configuration center 
 enabled: true
        service-id: config-server
        #3. From the configuration center : download  zuul-dev.yml name: zuul-service
      profile: dev

3.3 Start the service , Observe the log of getting configuration information from the configuration center

 Start project
config

版权声明
本文为[Xiao Han]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224165028636f.html

Scroll to Top