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是全局配置中配置的属性,时间格式也是全局配置中指定的。下一篇讲一下有关全局配置部署热部分的知识。

 

转载于:https://www.cnblogs.com/shizhongtao/p/spring-cloud.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值