spring cloud學習(五) 配置中心


Spring Cloud Config為服務端和客戶端提供了分布式系統的外部化配置支持。配置服務中心采用Git的方式存儲配置文件,因此我們很容易部署修改,有助於對環境配置進行版本管理。

一、配置中心

1.1
新建模塊config-server
pom文件

<?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">
    <parent>
        <artifactId>spring-cloud</artifactId>
        <groupId>com.feng</groupId>
        <version>0.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-server</artifactId>

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

</project>

1.2
application.yml

server:
  port: 8030

eureka:
  client:
    serviceUrl:
          defaultZone: http://localhost:8010/eureka/ #eureka服務注冊地址

# git管理配置
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/fengzp/config/ #git倉庫地址
          searchPaths: demo* #搜索路徑
#          username: username
#          password: password
  application:
    name: config-server

1.3
ConfigApplication,添加EnableConfigServer標識是一個配置中心服務

/**
 * @author fengzp
 * @date 17/5/4
 * @email fengzp@gzyitop.com
 * @company 廣州易站通計算機科技有限公司
 */
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigApplication {

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

1.4
在配置的git倉庫下新建一個demo1的文件夾,在里面創建一個叫client-a-dev.properties的配置文件
文件中隨便加上兩個配置

ip=192.168.30.51
port=9999

啟動模塊,然后打開 http://localhost:8030/client-a/dev

說明讀取配置成功

這里說明一下http請求讀取配置的匹配規則:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

二、客戶端讀取配置

2.1
修改client-a模塊
pom文件新增依賴

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

2.2
bootstrap.yml添加相關配置后

server:
  port: 8910

eureka:
  client:
    serviceUrl:
          defaultZone: http://localhost:8010/eureka/

spring:
  application:
      name: client-a
  cloud:
      config:
        discovery:
          enabled: true #開啟通過服務來訪問Config Server的功能
          service-id: config-server
        profile: dev
        label: master

2.3
在TestController添加測試方法

@RestController
public class TestController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/hi")
    @HystrixCommand(fallbackMethod = "hiFallback")
    public String hi(@RequestParam String id){
        return restTemplate.getForObject("http://service-a/hi?id="+id, String.class);
    }

    public String hiFallback(String id) {
        return "hi, " + id + ", error!";
    }

    @Value("${ip}")
    private String ip;
    @Value("${port}")
    private String port;

    @RequestMapping("/getProperties")
    public String getProperties(){
        return ip + " : " + port;
    }
}

啟動模塊后打開 http://localhost:8910/getProperties

說明讀取配置成功


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM