Spring Cloud Config 远程仓库

远程仓库受网络影响,可以从本地(配置中心)
注册中心与配置中心合并在一起,可能导致页面没有样式,所以分开来。

添加依赖

<?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.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>org.baozi</groupId>
    <artifactId>cloud-config</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>config-registration</module>
        <module>config-server</module>
        <module>config-clients</module>
    </modules>

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

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

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

        <!--
            config client 可以多项目通用,放在父依赖中
            config server 必须放在配置中心的依赖中,否则会导致有些属性不能注入
         -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</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>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
<?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.baozi</groupId>
      <artifactId>cloud-config</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>config-registration</artifactId>

</project>
<?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.baozi</groupId>
      <artifactId>cloud-config</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>config-server</artifactId>

   <dependencies>
      <!-- 只能在配置中心添加config-server -->
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-config-server</artifactId>
      </dependency>
   </dependencies>

</project>
<?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.baozi</groupId>
      <artifactId>cloud-config</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>config-clients</artifactId>

</project>

注册中心

spring.application.name=config-registration
server.port=9000

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class ConfigRegistrationApplication {

   public static void main(String[] args) {
      SpringApplication.run(ConfigRegistrationApplication.class, args);
   }

}

配置中心

spring.application.name=config-server
server.port=9500

eureka.client.service-url.defaultZone=http://localhost:9000/eureka
# 连接到远程仓库
spring.cloud.config.server.git.uri=https://gitee.com/baozi-baozi/cloud-config.git
1.png
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;

@SpringBootApplication
@EnableConfigServer  // 开启配置中心
//@EnableDiscoveryClient // 这个可以省略
public class ConfigServerApplication {

   public static void main(String[] args) {
      SpringApplication.run(ConfigServerApplication.class, args);
   }

}
访问规则:

1.  application:上面文件中的config-clients
2.  profile:上面文件中的dev,如果没有就是default
3.  labei:就是分支,不写默认为master
4.  /application/profile[/label]
http://localhost:9500/config-clients/default
http://localhost:9500/config-clients/default/master
http://localhost:9500/config-clients/dev
http://localhost:9500/config-clients/dev/master
5.  /application-profile.[yml | properties | json ]
http://localhost:9500/config-clients-default.properties
http://localhost:9500/config-clients-default.yml
http://localhost:9500/config-clients-default.json
http://localhost:9500/config-clients-dev.properties
http://localhost:9500/config-clients-dev.yml
http://localhost:9500/config-clients-dev.json
6.  /label/application-profile.[yml | properties | json ]
在前一个基础上加上分支。

配置客户端

bootstrap.properties

# 配置文件对应的application部分, 同时也是spring.application.name的名字
spring.application.name=config-clients
server.port=10000

# 可以写在配置中心的配置文件中
eureka.client.service-url.defaultZone=http://localhost:9000/eureka

# config-server的url, 可以写多个。
# spring.cloud.config.uri=http://localhost:9500/
# 可以用服务发现的方式代替写多个uri
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-server

# profile部分
#spring.cloud.config.profile=default
spring.cloud.config.profile=dev

# label部分
spring.cloud.config.label=master
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientsApplication {

   public static void main(String[] args) {
      SpringApplication.run(ConfigClientsApplication.class, args);
   }

}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.MessageFormat;

@RestController
public class ClientsController {

    // ~方式一: 如果远程仓库延迟比较高,运行时会报错,采用本地文件比较好
    // -------------------------------

    @Value("${user.name}")
    private String name;

    @Value("${user.age}")
    private String age;

    @GetMapping("/m1")
    public String m1() {
        return MessageFormat.format("m1  name: {0}, age: {1}", name, age);
    }

    // ~方式二
    // -------------------------------

    @Autowired
    private Environment environment;

    @GetMapping("/m2")
    public String m2() {
        String name = environment.getProperty("user.name", "undefined");
        String age = environment.getProperty("user.age", "-1");
        return MessageFormat.format("m2  name: {0}, age: {1}", name, age);
    }


}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容