Iris微服务框架_golang web框架_完整示例Demo

这篇具有很好参考价值的文章主要介绍了Iris微服务框架_golang web框架_完整示例Demo。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Iris简介

Iris是一款Go语言中用来开发web应用的框架,该框架支持编写一次并在任何地方以最小的机器功率运行,如Android、ios、Linux和Windows等。该框架只需要一个可执行的服务就可以在平台上运行了。

Iris框架以简单而强大的api而被开发者所熟悉。iris除了为开发者提供非常简单的访问方式外,还同样支持MVC。另外,用iris构建微服务也很容易。

在iris框架的官方网站上,被称为速度最快的Go后端开发框架。在Iris的网站文档上,列出了该框架具备的一些特点和框架特性,列举如下:

Iris特性
  • 专注于高性能
  • 简单流畅的API
  • 高扩展性
  • 强大的路由和中间件生态系统
  • 使用iris独特的表达主义路径解释器构建RESTful API
  • 动态路径参数化或通配符路由与静态路由不冲突
  • 使用重定向选项从URL中删除尾部斜杠
  • 使用虚拟主机和子域名变得容易
  • 分组API和静态或甚至动态子域名
  • net / http和negroni-like处理程序通过iris.FromStd兼容
  • 针对任意Http请求错误 定义处理函数
  • 支持事务和回滚
  • 支持响应缓存
  • 使用简单的函数嵌入资源并与go-bindata 保持兼容
  • mvc
  • 上下文
  • 高度可扩展的试图渲染(目前支持markdown,json,xml,jsonp等等)
  • 正文绑定器和发送HTTP响应的便捷功能
  • 限制请求正文
  • 提供静态资源或嵌入式资产
  • 本地化i18N
  • 压缩(Gzip是内置的)
  • 身份验证
  • Basic Authentication
  • OAuth, OAuth2 (支持27个以上的热门网站)
  • JWT *服务器
  • 通过TLS提供服务时,自动安装和提供来自https://letsencrypt.org的证书
  • 默认为关闭状态
  • 在关闭,错误或中断事件时注册
  • 连接多个服务器,完全兼容 net/http#Server
  • 视图系统.支持五种模板隐隐 完全兼容 html/template
  • Websocket库,其API类似于http://socket.io [如果你愿意,你仍然可以使用你最喜欢的]
  • 热重启
  • Typescript集成 + Web IDE

官方源代码地址: https://github.com/kataras/iris

Iris web微服务框架示例

总体功能

  • 集成aiwuTech.fileLogger日志按天切割,按级别输出到不同日志文件
  • 集成redis
  • 集成mysql
  • 读取etcd配置
  • 异常处理,全局异常处理
  • 拦截器打印请求和响应参数
  • 前端html集成
  • 等等

Iris微服务框架_golang web框架_完整示例Demo,golang,微服务,golang,iris

下载iris依赖包
go get -u github.com/kataras/iris
main.go项目入口

init方法: 初始化相关配置,都有注释说明
main方法: 启动Iris服务

package main

import (
	. "web-demo/config"
	. "web-demo/log"
	. "web-demo/redis"
	. "web-demo/db"
	_ "web-demo/handler"
	"web-demo/web"
	"flag"
	"fmt"
	"time"
)

var showVersion = flag.Bool("v", true, "print version")

func init(){
	//调用 flag.Parse() 进行解析
	flag.Parse()

	//初始化配置文件
	ConfigRead()

	//初始化log日志
	LogInit()

	//初始化redis
	RedisInit(Cfg.RedisAddr,0 , Cfg.RedisPassword, Cfg.RedisMaxConn)

	//初始化mysql
	SqlDBInit(&SqlDBParam{Cfg.MysqlHost, Cfg.MysqlPort, Cfg.MysqlUser, Cfg.MysqlPwd,
	Cfg.MysqlDb})
}

func main() {
	if *showVersion {
		//这个日期就是写死的一个日期,不是这个日期就不认识,就不能正确的格式化
		//据说是go诞生之日
		version := fmt.Sprintf("%s %s@%s", "web-demo", "1.0", time.Now().Format("2006-01-02 15:04:05"))
		fmt.Println(version)
	}

	Log.Info("start server...")

	//监听端口
	Log.Info("listen on :%s", Cfg.ListenPort)
	web.RunIris(Cfg.ListenPort)
}


AccessLogMiddleware.go 中间件拦截器

在拦截器中添加请求头
把请求参数和响应参数打印到日志文件

package web

import (
	. "web-demo/util/threadlocal"
	. "web-demo/log"
	"github.com/kataras/iris"
	. "github.com/jtolds/gls"
	"strconv"
	"time"
)

func init() {
	RegisterPreMiddleware(accessLogMiddleware{})
}

type accessLogMiddleware struct {
	// your 'stateless' fields here
}

func (m accessLogMiddleware) Serve(ctx *iris.Context) {
	//check header
	//request id
	requestId := ctx.RequestHeader("X-Web-Demo-RequestId")
	if requestId == "" {
		requestId = strconv.FormatInt(time.Now().UnixNano(), 10)
	}

	//access log
	AccessLog.Info(requestId + "\t" + ctx.RequestIP() + "\t" + string(ctx.RequestURI()))

	//response requestId
	ctx.Response.Header.Add("X-Web-Demo-RequestId", requestId)

	//do chian
	Mgr.SetValues(Values{Rid: requestId}, func() {
		ctx.Next()
	})

	//rrlog
	RRLog.Info(requestId + "\t" + "-RequestIP:==" + ctx.RequestIP())
	RRLog.Info(requestId + "\t" + "-RequestP:==" + string(ctx.RequestPath(true)))
	RRLog.Info(requestId + "\t" + "-RequestD:==" + string(ctx.Request.Body()))
	RRLog.Info(requestId + "\t" + "-Response:==" + string(ctx.Response.Body()))
}


logger.go 日志定义和配置
package log

import (
	"flag"
	"github.com/aiwuTech/fileLogger"
	"os"
	. "web-demo/util/threadlocal"
	"web-demo/config"
)

var Log Logger
var ErrorLog Logger
var AccessLog Logger
var RRLog Logger
var InnerRRLog Logger

type Logger interface {
	Trace(format string, params ...interface{})
	Info(format string, params ...interface{})
	Warn(format string, params ...interface{})
	Error(format string, params ...interface{})
}

type CustomedLogger struct{
	MyLogger Logger
}
func (cl CustomedLogger)Trace(format string, params ...interface{}){
	cl.MyLogger.Trace(cl.resetFormat(format), params)
}
func (cl CustomedLogger)Info(format string, params ...interface{}){
	cl.MyLogger.Info(cl.resetFormat(format), params)
}
func (cl CustomedLogger)Warn(format string, params ...interface{}){
	cl.MyLogger.Warn(cl.resetFormat(format), params)
}
func (cl CustomedLogger)Error(format string, params ...interface{}){
	cl.MyLogger.Error(cl.resetFormat(format), params)
}
func (cl CustomedLogger)resetFormat(format string) string{
	logstr := format
	if rid, ok := Mgr.GetValue(Rid); ok {
		logstr = rid.(string) + " - " + logstr
	}
	return logstr
}

func LogInit() {
	logPath := config.Cfg.LogPath
	if (len(logPath) == 0) {
		logPath = "/Users/liang/ideaWorkspace/go/src/web-demo/logs/web-demo"
	}

	flag.Parse()
	if !isExist(logPath) {
		os.Mkdir(logPath, 0755)
	}

	logger := fileLogger.NewDailyLogger(logPath, "root.log", "", fileLogger.DEFAULT_LOG_SCAN, fileLogger.DEFAULT_LOG_SEQ)
	Log = CustomedLogger{MyLogger: logger}

	errorLog := fileLogger.NewDailyLogger(logPath, "error.log", "", fileLogger.DEFAULT_LOG_SCAN, fileLogger.DEFAULT_LOG_SEQ)
	ErrorLog = CustomedLogger{MyLogger: errorLog}

	accessLog := fileLogger.NewDailyLogger(logPath, "access.log", "", fileLogger.DEFAULT_LOG_SCAN, fileLogger.DEFAULT_LOG_SEQ)
	AccessLog = CustomedLogger{MyLogger: accessLog}

	rRLog := fileLogger.NewDailyLogger(logPath, "rr.log", "", fileLogger.DEFAULT_LOG_SCAN, fileLogger.DEFAULT_LOG_SEQ)
	RRLog = CustomedLogger{MyLogger: rRLog}

	innerRRLog := fileLogger.NewDailyLogger(logPath, "inner_rr.log", "", fileLogger.DEFAULT_LOG_SCAN, fileLogger.DEFAULT_LOG_SEQ)
	InnerRRLog = CustomedLogger{MyLogger: innerRRLog}
}

func isExist(path string) bool {
	_, err := os.Stat(path)
	return err == nil || os.IsExist(err)
}


mysql.go mysql初始化连接
package db

import (
	. "web-demo/log"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
	. "web-demo/config"
)

var Mysql *gorm.DB

type SqlDBParam struct {
	Ip       string
	Port     int
	User     string
	Pw       string
	Database string
}
// mysql初始化文件
func SqlDBInit(param *SqlDBParam) {
	Log.Info("init mysql...")
	param_s := fmt.Sprintf(
		"%v:%v@tcp(%v:%v)/%v?parseTime=True&loc=Local",
		param.User,
		param.Pw,
		param.Ip,
		param.Port,
		param.Database,
	)
	Log.Info("mysql param: %s", param_s)

	db, err := gorm.Open("mysql", param_s)
	if err != nil {
		Log.Error("open mysql error: %v", err)
		panic(err)
	}
	db.DB().SetMaxIdleConns(Cfg.MysqlMaxConn)
	db.SingularTable(true)
	db.LogMode(true)

	Mysql = db
	Log.Info("init mysql end.")
}

redis.go redis初始化和常用操作方法
package redis

import (
	. "web-demo/log"
	"github.com/garyburd/redigo/redis"
	"time"
)

var (
	redisPool *redis.Pool
)

const (
	maxIdle     = 500
	idleTimeout = 0 * time.Second
	wait        = true
)

//Init
//eg: RedisInit("127.0.0.1:6379", 0, "pwd", 8)
func RedisInit(server string, db int, password string, maxConn int) {
	maxActive := maxConn

	//Make a pool object
	redisPool = &redis.Pool{
		// Maximum number of idle connections in the pool.
		MaxIdle: maxIdle,

		// Maximum number of connections allocated by the pool at a given time.
		// When zero, there is no limit on the number of connections in the pool.
		MaxActive: maxActive,

		// Close connections after remaining idle for this duration. If the value
		// is zero, then idle connections are not closed. Applications should set
		// the timeout to a value less than the server's timeout.
		IdleTimeout: idleTimeout,

		// If Wait is true and the pool is at the MaxActive limit, then Get() waits
		// for a connection to be returned to the pool before returning.
		Wait: wait,

		// Dial is an application supplied function for creating and configuring a
		// connection.
		//
		// The connection returned from Dial must not be in a special state
		// (subscribed to pubsub channel, transaction started, ...).
		Dial: func() (redis.Conn, error) {
			c, err := redis.Dial("tcp", server)
			if err != nil {
				return nil, err
			}
			if password != "" {
				if _, err := c.Do("AUTH", password); err != nil {
					c.Close()
					return nil, err
				}
			}
			if _, err := c.Do("SELECT", db); err != nil {
				c.Close()
				return nil, err
			}
			return c, nil
		},
		// TestOnBorrow is an optional application supplied function for checking
		// the health of an idle connection before the connection is used again by
		// the application. Argument t is the time that the connection was returned
		// to the pool. If the function returns an error, then the connection is
		// closed.
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			if time.Since(t) < time.Minute {
				return nil
			}
			_, err := c.Do("PING")
			return err
		},
	}
	Log.Info("redis[%v %v] init ok", server, db)
}

......

handler 注意Iris会自动扫描handler包名

如果分了多个包,像demo里分了user和html,这时iris扫描不到.
需要在handler包中添加一个公共的文件把这2个包导入,如下所示

package handler

import (
	_ "web-demo/handler/html"
	_ "web-demo/handler/user"
)

其他功能就不详细介绍了,请看源代码

web-demo运行
#启动项目
go run main.go 
#打印如下,表示成功启动8080端口
listen on :8080

浏览器访问:
http://127.0.0.1:8080/webdemo/html/v1/passenger/login.md
显示login.md文件内容

http://127.0.0.1:8080/webdemo/html/v1/index
显示index.html内容

Demo源代码地址:https://github.com/golang-example/web-demo文章来源地址https://www.toymoban.com/news/detail-805257.html

到了这里,关于Iris微服务框架_golang web框架_完整示例Demo的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Golang Web框架性能对比

    Golang Web框架性能对比 github star排名依次: Gin Beego Iris Echo Revel Buffalo 性能上gin、iris、echo网上是给的数据都是五星,beego三星,revel两星 beego是国产,有中文文档,文档齐全 根据star数,性能,易用程度,社区活跃度和具体应用场景来选择的话,当前我更加倾向于使用gin作为基础开

    2024年01月24日
    浏览(27)
  • Golang个人web框架开发-学习流程

    github地址:ameamezhou/golang-web-frame 后续还将继续学习更新 设置免密登录 ssh-keygen 一路回车就OK 上面有告诉你密钥生成地址 红框为需要上传的公钥 首先明确目标– 我们学习开发web框架的目的是 : 在日常的web开发中,我们经常要使用到web框架, python 就有很多好用的框架,比如

    2024年01月19日
    浏览(25)
  • GoZero微服务个人探究之路(二)Go-Zero官方api demo示例探究

    api demo 代码生成 | go-zero Documentation 编辑 demo-api.yaml 编辑 服务名称:demo-api HOST地址:0.0.0.0监听所有可用网络接口 Port地址:服务运行在8888端口 config/config.go 编辑 存储config信息,这里rest.RestConf是RESTful API的结构体,此外还可以添加数据库,缓存配置信息 handler/demohandler.go 编辑

    2024年01月18日
    浏览(38)
  • GoZero微服务个人探索之路(三)Go-Zero官方rpc demo示例探究

    两个文件均为protoc-gen-go-grpc自动生成 构成一个完整的 gRPC 服务的定义和实现 demo.go goctl生成的客户端代码 Request 和 Response 别名: 定义了 Request 和 Response 两个别名,实际上是从 demo 包中导入的对应的消息类型。 Demo 接口: 定义了一个 Demo 接口,其中包含了调用 gRPC 服务中 P

    2024年01月18日
    浏览(38)
  • golang微服务框架特性分析及选型

    (以下框架均为go框架) 包括:Istio、go-zero、go-kit、go-kratos、go-micro、rpcx、kitex、goa、jupiter、dubbo-go、tarsgo   1、特性及使用场景 start统计截止至2024.04.01 序号 名称 特性 适用场景 stars 1 Istio Istio 是一个开源的服务网格(Service Mesh)解决方案,提供了流量管理、安全策略、监控

    2024年04月17日
    浏览(24)
  • 开源 Golang 微服务入门二:RPC 框架 Kitex

    前一篇笔记介绍了字节跳动的开源 Golang 微服务 HTTP 框架 Hertz, 如下: 开源 Golang 微服务入门一: HTTP 框架 Hertz 本文将要介绍同样是字节跳动开源的 Golang 微服务 RPC 框架 Kitex。 Kitex 简介 Kitex 字节跳动内部的 Golang 微服务 RPC 框架,具有高性能、强可扩展的特点,在字节内部

    2024年02月09日
    浏览(29)
  • 开源 Golang 微服务入门一: HTTP 框架 Hertz

    从本篇笔记开始将介绍 Go 框架三件套(Web / RPC / ORM),框架的学习有助于后续课程的学习以及大项目的完成。本文主要介绍字节跳动的开源 Golang 微服务 HTTP 框架 Hertz。先了解一下三件套的相关基本知识,做一下铺垫: Gorm gorm是Golang语言中一个已经迭代数十年且功能强大、性

    2024年02月08日
    浏览(29)
  • 开源 Golang 微服务入门三:ORM 框架 GORM

    前两篇笔记分别介绍了 Golang 微服务 HTTP 框架 Hertz 和 Golang 微服务 RPC 框架 Kitex,本文将要介绍面向ORM(持久层)框架 GORM。 官方文档 GORM 是面向 Golang 语言的一种 ORM(持久层)框架,支持多种数据库的接入,例如 MySQL,PostgreSQL,SQLite,SQL Server,Clickhouse。此框架的特点,弱化了开

    2024年02月14日
    浏览(27)
  • Golang | Web开发之Gin多服务配置及优雅关闭平滑重启

    欢迎关注「 全栈工程师修炼指南 」公众号 点击 👇  下方卡片  即可关注我哟! 设为 「 星标⭐ 」 每天带你  基础入门  到  进阶实践  再到  放弃学习 ! 专注  企业运维实践、网络安全、系统运维、应用开发、物联网实战、全栈文章  等知识分享 “    花开堪折直须折

    2024年02月08日
    浏览(33)
  • [golang gin框架] 39.Gin商城项目-微服务实战之微服务架构

    单体架构在 中小企业内部 用的是非常多的,当 业务不复杂 , 团队规模不大 的时候,单体架构比微服务架构具有 更高的生产率 单体架构 当 业务比较复杂 , 并发量 比较大, 团队规模扩大的时候, 就需要引入微服务架构了,它比单体架构 具有 更高的生产率, 可以 节省成本 , 解

    2024年02月12日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包