spring cloud學習(五) 配置中心

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

1、配置中心

1.1
新建模塊config-server
pom文件github

<?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.ymlspring

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標識是一個配置中心服務apache

/**
 * @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的配置文件
文件中隨便加上兩個配置bootstrap

ip=192.168.30.51
port=9999

啓動模塊,而後打開 http://localhost:8030/client-a/dev
app

說明讀取配置成功maven

這裏說明一下http請求讀取配置的匹配規則:分佈式

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

2、客戶端讀取配置

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

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

2.2
bootstrap.yml添加相關配置後rest

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

說明讀取配置成功

相關文章
相關標籤/搜索