使用viper 进行golang 应用的配置管理
viper 是一个强大的golang 配置管理包,支持多种配置格式内容的读取,同时支持读取key/value 存储的数据
而且不只是读取内容 ,同时也包含了,配置的写入操作。
以下是一个简单的demo,同时使用docker 运行
项目结构
项目使用go mod 进行包的管理
- 目录结构
├── Dockerfile
├── Makefile
├── README.md
├── cmd
│ └── yaml
│ └── main.go
├── docker-compose.yaml
├── go.mod
└── go.sum
代码说明
- go.mod
module github.com/rongfengliang/viper-app
go 1.13
require github.com/spf13/viper v1.4.0
- 代码
cmd/yaml/mai.go
package main
import (
"bytes"
"log"
"github.com/spf13/viper"
)
func main() {
viper.SetConfigType("yaml")
var yamlExample = []byte(`
Hacker: true
name: steve
hobbies:
- skateboarding
- snowboarding
- go
clothing:
- jacket: leather
- trousers: denim
age: 35
eyes: brown
beard: true
`)
viper.AutomaticEnv()
if err := viper.ReadConfig(bytes.NewBuffer(yamlExample)); err != nil {
log.Fatal("some error", err)
}
log.Print(viper.GetString("name"))
viper.Set("name", "demoapp rong")
log.Print(viper.GetString("name"))
var appname string = viper.GetString("APIKEY")
log.Print(appname)
}
- Makefile
build-app: clean make-dir build-mac build-linux build-windows
clean:
rm -rf build/*
make-dir:
mkdir -p build/{mac,linux,windows}
build-mac:
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o build/mac ./cmd/...
build-linux:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/linux ./cmd/...
build-windows:
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o build/windows ./cmd/...
- dockerfile
FROM alpine
LABEL EMAIL="1141591465@qq.com"
LABEL AUTHOR="dalongrong"
WORKDIR /app
COPY build/linux/yaml /app/yaml
RUN chmod +x /app/yaml
CMD [ "./yaml" ]
- docker-compose 文件
version: "3"
services:
app:
build: ./
image: dalongrong/viper-app
env_file:
- .env
运行&&构建
- 本地运行
不包含环境变量的
go run cmd/yam/main.go
支持环境变量的
APIKEY=rongfengliang go run cmd/yaml/main.go
- 容器运行
构建镜像
docker-compose build
启动
docker-compose up
效果
Attaching to viper-golang_app_1
app_1 | 2019/10/30 08:56:31 steve
app_1 | 2019/10/30 08:56:31 demoapp rong
app_1 | 2019/10/30 08:56:31 rongfengliang
viper-golang_app_1 exited with code 0
说明
以上是一个简单的demo,实际功能很强大,还需要再学习官方文档了解
参考资料
https://github.com/spf13/viper
https://github.com/rongfengliang/viper-learning
使用viper 进行golang 应用的配置管理的更多相关文章
- Golang项目的配置管理——Viper简易入门配置
Golang项目的配置管理--Viper简易入门配置 What is Viper? From:https://github.com/spf13/viper Viper is a complete co ...
- golang中的配置管理库viper
viper简介 Viper是适用于Go应用程序的完整配置解决方案.它旨在在应用程序中工作,并且可以处理所有类型的配置需求和格式.它支持: 设置默认值 从JSON,TOML,YAML,HCL,envfi ...
- golang中的viper示例
这是第二次实操viper了, 年纪大了就多练练,才能记住. http://go.coder55.com/article/6589 https://github.com/spf13/viper pack ...
- golang常用库:配置文件解析库-viper使用
一.viper简介 viper 配置解析库,是由大神 Steve Francia 开发,他在google领导着 golang 的产品开发,他也是 gohugo.io 的创始人之一,命令行解析库 cob ...
- golang的配置文件操作:viper
参考: 1.http://blog.51cto.com/13599072/2072753 2.https://studygolang.com/articles/14453 3.https://www. ...
- golang模块viper读取配置文件
一.介绍 Viper是一个方便Go语言应用程序处理配置信息的库.它可以处理多种格式的配置.它支持的特性: 设置默认值 从JSON.TOML.YAML.HCL和Java properties文件中读取配 ...
- Go语言配置管理神器——Viper中文教程
Viper是适用于Go应用程序的完整配置解决方案.它被设计用于在应用程序中工作,并且可以处理所有类型的配置需求和格式. Viper Viper是适用于Go应用程序的完整配置解决方案.它被设计用于在应用 ...
- viper配置管理
安装 go get github.com/spf13/viper viper支持的功能 1.可以设置默认值 2.可以加载多种格式的配置文件,如JSON,TOML,YAML,HCL和Java属性配置文件 ...
- Golang优秀开源项目汇总, 10大流行Go语言开源项目, golang 开源项目全集(golang/go/wiki/Projects), GitHub上优秀的Go开源项目
Golang优秀开源项目汇总(持续更新...)我把这个汇总放在github上了, 后面更新也会在github上更新. https://github.com/hackstoic/golang-open- ...
随机推荐
- python解决使用镜像源来安装包
一.问题在进行python包安装的时候出现一个问题就是无法进行安装,且出现了如下的错误 报错代码 Retrying (Retry(total=4, connect=None, read=None, r ...
- PHP清除数组中有字符串空格的方法
清除数组中字符串有空格的方法函数 function TrimArray($arr){ if (!is_array($arr)){ return $arr; } while (list($key, $v ...
- delphi 格式转换
TO_CHAR 是把日期或数字转换为字符串 TO_DATE 是把字符串转换为数据库中得日期类型转换函数TO_NUMBER 将字符转化为数字 TO_CHAR 使用TO_CHAR函数处理数字 TO_CHA ...
- mysql数据库事务
事务: 一个或者一组sql语句组成一个执行的单元,这个单元要么全都执行,要么都不执行.也就是每个sql语句相互依赖.如果中间有一条出现错误则整个单元将回滚.(回滚就是刚刚的操作都撤销) 事务的属性: ...
- Eclipse中js文件修改后浏览器不能及时更新的解决办法
项目中js文件修改后浏览器不能及时更新的解决办法 转载:http://www.codeweblog.com/%E9%A1%B9%E7%9B%AE%E4%B8%ADjs%E6%96%87%E4%BB%B ...
- SpringMVC中的400错误,The request sent by the client was syntactically incorrect.
在其他对象属性类型一样情况下,只需要创建一个类,再在springmvc.xml中添加配置: package com.ujiuye.common; import org.springframework. ...
- React学习笔记①
三种导出方式 export let num = 1://1 let num2 = 2://2 export {num2};//2 export default {default}//3 三种导入方式 ...
- Springboot自动化部署到docker以及logback按天生成日志
Dockerfile FROM java:8 VOLUME /tmp ADD maven/sms-0.0.1-SNAPSHOT.jar app.jar RUN sh -c 'touch /app.ja ...
- ip协议栈
struct iphdr { #if defined(__LITTLE_ENDIAN_BITFIELD) __u8 ihl:4, version:4; #elif defined (__BIG_END ...
- 【python】多任务(1. 线程)
线程执行的顺序是不确定,可以通过适当的延时,保证某一线程先执行 基础语法 # 多线程的使用方式 import threading def test1():... # 如果创建Thread时执行的函数, ...