SpringCloud第九篇 Config配置中心-Config Client(GIT)

1.新建一个项目config-client

2.具体的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>com.star.guo</groupId>
        <artifactId>stage4</artifactId>
    <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
<!--     <groupId>com.star.guo</groupId> -->
    <artifactId>config-client</artifactId>
<!--     <version>0.0.1-SNAPSHOT</version> -->
    <name>config-client</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</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>


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

</project>
3.新增注解

package com.star.guo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication {

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

}

4.新增和修改核心配置文件bootstrap.yml,不是application.yml

spring: 
  application:  
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      uri: http://localhost:8008/
#此配置可以指定文件名,默认根据application name 打开,本例来说,默认读取的是config-client-dev.yml/properties
      name: config,config-client
      
server: 
  port: 8009
  
eureka: 
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/

5.新建一个Controller class

@RestController
public class ConfigController {

    @Value("${starguo1}")
    String starguo1;
    @Value("${starguo2}")
    String starguo2;

    @RequestMapping(value = "/readConfig")
    public String readConfig() {
        return starguo1+":"+starguo2;
        //http://localhost:8009/readConfig
    }
}
6.启动

先启动eureka

再启动config-server

再启动config-client

7.访问测试

http://localhost:8009/readConfig

会看见guoxingege1:guoxingege2

8.升级高可用

拷贝一个项目config-client,项目名为config-client2

修改bootstrap.yml即可,如下

删掉或者注释掉uri,使用server id,具体的配置如下

spring: 
  application:  
    name: config-client2
  cloud:
    config:
      label: master
      profile: dev
#此配置可以指定文件名,默认根据application name 打开,本例来说,默认读取的是config-client2-dev.yml/properties
#使用默认可不配置name
      name: config,config-client
      discovery:
        enabled: true #从配置中心读取文件
        service-id: config-server #配置中心的application name

      
server: 
  port: 8010
  
eureka: 
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值