spring-cloud構架微服務(1)-全局配置


使用spring-cloud是基於熟悉springboot基礎上進行的。本篇介紹全局配置,spring-boot版本就以1.4.0來做吧。項目地址:

https://git.oschina.net/bingyulei007/spring-cloud-simple

一、搭建全局配置服務器

  首先構建spring-boot項目,pom加入如下引用:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
</parent>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Camden.SR2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId> 
  </dependency>
</dependencies>

  簡單來說,就是提供一個地址,可以獲取項目的配置屬性,如果你不適用spring-cloud提供的配置服務器,你甚至可以自己寫一個基於rest的服務器,來下發配置文件屬性。

  啟動類ConfigServer.java代碼。加入對飲的configserver的注解

@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
  public static void main(String[] args) {
    SpringApplication.run(ConfigServer.class, args);
  }
}

  configserver讀取配置文件可以從git服務器上,也可以從本地git目錄,也可以讀取本地磁盤的位置。這里我們配置一種基於本地磁盤位置的配置。其中端口號設置為8088,不啟用健康檢查,配置文件存放到本地D盤配置文件目錄:

server:
  port: 8088
spring:
  profiles:
    active: native
  cloud:
    config:
      server:
        health:
          enabled:  false
        native:
          searchLocations: D:/ideaprojects/cloud-config-repo

然后在對應的 'D:/ideaprojects/cloud-config-repo'目錄加入配置文件:cloud-config-client.properties和cloud-config-common.properties,里面添加如下內容

cloud-config-common.properties的內容
———————————————————————————————————————————————————————————————————————
#url編碼,解決中文問題 server.tomcat.uri
-encoding=UTF-8 #序列化時間格式 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.mvc.date-format=yyyy-MM-dd HH:mm:ss #mvc序列化時候時區選擇 spring.jackson.time-zone=GMT+8 #aop啟用 spring.aop.auto=true spring.aop.proxy-target-class=true ————————————————————————————————————————————————————————————————————————
cloud-config-client.properties的內容
———————————————————————————————————————————————————————————————————————— 
#
<!-- 項目名稱(spring默認讀取)-->
spring.application.name
=client-test
#數據庫配置
#自定義屬性配置
bing.
for.test=hello-word

 

ok,至此,全局配置服務器已經准備好,我們可以啟動測試了。

啟動服務,GET方式訪問'http://localhost:8088/cloud-config-common/application.property'或者'http://localhost:8088/cloud-config/common'就可以返回配置的屬性。

 

  

 

二、客戶端訪問

現在我們創建一個客戶端,來讀取全局配置,新建項目。pom加入如下引用:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
    </dependencies>

在resource目錄中創建bootstrap.yum文件(讀取全局配置使用,不必須有application.yml的配置文件),加入如下配置:

server:
  port: 8081
spring:
  cloud:
    config:
      uri: http://localhost:8088
#      全局配置文件名的前面部分
      name: cloud-config
#     可以讀取多個配置文件
      profile: client,common
      failFast: true
  application:
    name: config-client-01

然后創建啟動類ConfigClient.java 和測試類UserController.java類:

package com.bing;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


/**
 * 全局配置服務器
 *
 */
@SpringBootApplication
public class ConfigClient {
  public static void main(String[] args) {
    SpringApplication.run(ConfigClient.class, args);
  }
}
package com.bing.User;

import com.bing.model.User;

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

import java.util.Date;

/**
 * Created by szt on 2016/11/18.
 */
@RestController
public class UserController {
  @Value("${bing.for.test}")
  private String testProperty;

  @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
  public User getUser(@PathVariable("id") String id) {
    User user=new User();
    user.setName(testProperty);
    user.setId(id);
    user.setCreatedTime(new Date());
    return user;
  }
}
bingyulei007

啟動測試訪問地址:http://localhost:8081/user/as12返回如下消息:

{
  "id": "as12",
  "name": "hello-word",
  "createdTime": "2016-11-18 12:17:34"
}

至此,一個簡單的全局配置就完成了。注意上面返回的name是全局配置中配置的屬性,時間格式也是全局配置中指定的。下一篇講一下有關全局配置部署熱部分的知識。

 


免責聲明!

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



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