JAVA b2b2c电子商务SpringCloud分布式微服务- Config

.Spring Cloud Config 分布式配置

a.Config服务器

①新建springboot项目,依赖选择Config Server

②pom文件关键依赖
    了解springcloud架构可以加求求:三五三六二四七二五九

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

        ......

    </dependencies>

    ......

③application.yml文件

spring:
  application:
    name: config-server
  profiles:
    #配置文件在本地
    active: native
    #配置文件的目录
    cloud:
      config:
        server:
          native:
            search-locations: classpath:/config

server:
  port: 8101

④启动类添加注解@EnableConfigServer

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

}

⑤在resources下新建config/commom-dev.properties,用于测试

test.name=vettel
test.password=111111

⑥启动后访问 http://localhost:8101/common/dev 可查看配置文件信息,访问路径有如下几种

 /{application}/{profile}[/{label}]

      /{application}-{profile}.yml

      /{label}/{application}-{profile}.yml

      /{application}-{profile}.properties

      /{label}/{application}-{profile}.properties

注:对于resources下的config/commom-dev.properties,{application}为文件名"commom",{profile}为环境名"dev",{label}为路径名"config"。
 
 b.Config客户端
 
  ①新建springboot项目,依赖选择 Config Client 、Web

②pom文件关键依赖

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

        ......

    </dependencies>
    
    ......

③bootstrap.yml文件

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:8101
      profile: dev
      name: common

server:
  port: 8102

注意:此处需要将配置写入bootstrap.yml(会优先于application.yml加载)中,因为config的配置是优先于application.yml加载的,否则会报错。了解springcloud架构可以加求求:三五三六二四七二五九

④具体使用

@SpringBootApplication
@RestController
public class ConfigClientApplication {

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

    @Value("${test.name}")
    String name;
    @Value("${test.password}")
    String password;

    @RequestMapping(value="/getConfig")
    public String getConfig(){
        return "name[" + name + "], password[" + password + "]";
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值