springcloud 简单示例之分布式配置中心config(八)

在spring cloud中,有分布式配置中心组件spring cloud config,它支持配置文件放在在配置服务的内存中,也支持放在远程Git仓库里。引入spring cloud config后,我们的外部配置文件就可以集中放置在一个git仓库里,再新建一个config server,用来管理所有的配置文件,维护的时候需要更改配置时,只需要在本地更改后,推送到远程仓库,所有的服务实例都可以通过config server来获取配置文件,这时每个服务实例就相当于配置服务的客户端config client,为了保证系统的稳定,配置服务端config server可以进行集群部署,即使某一个实例,因为某种原因不能提供服务,也还有其他的实例保证服务的继续进行。

1.新建工程,工程的pom文件追加依赖

<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.dongjin.springcloud</groupId>
    <artifactId>springcloud-root</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>springcloud-config</artifactId>
  <dependencies>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <!-- 分布式配置中心 -->
      <!-- springCloudConfigClient -->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-config-server</artifactId>
      </dependency>
      <!-- eurekaClient -->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>
  </dependencies>
</project>

2.新建application.yml,内容如下

server:
  port: 9000
 
spring:
  application:
    name: springcloud-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/tornadokuku/springcloud-config.git
          search-paths: /**
      label: master
      username: tornadokuku
      password: ********
      force-pull: true
   
eureka:
  instance:
    instance-id: ${server.port}
  client: 
    service-url: 
      defaultZone: http://admin:123@localhost:8888/eureka/

3.新建ConfigApplication.java,内容如下

package com.dongjin.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigApplication {
 
	public static void main(String[] args) {
		SpringApplication.run(ConfigApplication.class, args);
	}
}

以上步骤,spring cloud config服务就搭建好了,等待客户端连接即可。

以下是搭建访问spring cloud config的客户端服务,内容如下

4.pom文件中追加以下依赖

      <!-- springCloudConfigClient -->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-config</artifactId>
      </dependency>
      <!-- eurekaClient -->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>

5.在/src/main/resources目录下追加名称为bootstrap.yml的文件,内容如下

spring:
  cloud:
    config:
      label: master
      profile: prod
      uri: http://localhost:9000/
      discovery:
        service-id: springcloud-config
        enabled: true

eureka:
  client: 
    service-url: 
      defaultZone: http://admin:123@localhost:8888/eureka/

此文件中必须追加eureka地址设置,才能够正确从eureka中获取到spring cloud config服务

6.获取配置信息的FeiConfigController.java

package com.dongjin.springcloud;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeiConfigController {
    
	@Value("${name}")
	private String name;
	
	@Value("${age}")
	private String age;
	
	@Value("${version}")
	private String version="开发环境";
 
	@RequestMapping(value = "/TestConfig")
	public String test(){
		return "你好,我是"+name+",年龄:"+age+"岁。当前环境:"+version;
	}
}

注意点:git服务上的文件名称必须和spring.application.name的名字一直,否则无法取出信息。

7.打开浏览器,输入地址http://localhost:8001/TestConfig
在这里插入图片描述
能够正确的访问到git中的信息。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值