Skip to content

Commit

Permalink
feat: use consul to do service discover and get configuration
Browse files Browse the repository at this point in the history
feat: dockerfile
feat: consul doc
  • Loading branch information
suyuan32 committed Sep 23, 2022
1 parent 1efc182 commit 0a090e8
Show file tree
Hide file tree
Showing 23 changed files with 634 additions and 283 deletions.
24 changes: 24 additions & 0 deletions Dockerfile-api
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM golang:1.19.1-alpine3.16 as builder

WORKDIR /home
COPY . .

RUN go env -w GO111MODULE=on \
&& go env -w GOPROXY=https://goproxy.cn,direct \
&& go env -w CGO_ENABLED=0 \
&& go env \
&& go mod tidy \
&& cd api \
&& go build -o /home/api/core_api core.go

FROM alpine:latest

LABEL MAINTAINER="RyanSU@yuansu.china.work@gmail.com"

WORKDIR /home

COPY --from=builder /home/api/core_api ./
COPY --from=builder /home/api/etc/core.yaml ./

EXPOSE 9100
ENTRYPOINT ./core_api -f core.yaml
24 changes: 24 additions & 0 deletions Dockerfile-rpc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM golang:1.19.1-alpine3.16 as builder

WORKDIR /home
COPY . .

RUN go env -w GO111MODULE=on \
&& go env -w GOPROXY=https://goproxy.cn,direct \
&& go env -w CGO_ENABLED=0 \
&& go env \
&& go mod tidy \
&& cd rpc \
&& go build -o /home/rpc/core_rpc core.go

FROM alpine:latest

LABEL MAINTAINER="RyanSU@yuansu.china.work@gmail.com"

WORKDIR /home

COPY --from=builder /home/rpc/core_rpc ./
COPY --from=builder /home/rpc/etc/core.yaml ./

EXPOSE 9101
ENTRYPOINT ./core_rpc -f core.yaml
21 changes: 12 additions & 9 deletions README.En.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ It can help you to develop a microservice back-end management core in a short ti
- **Fully support go-swagger**: Write comment in api file and generate swagger doc easily
- **Error handling**: Handle error messages via one module
- **International**:support different languages show in the front-end via put locale path in the message
- **Service Discover** builtin service discovery, load balancing
- **Service Discover** cunsul, load balancing
- **Authority** Manage authority via casbin
- **Other** builtin concurrency control, adaptive circuit breaker, adaptive load shedding, auto-trigger, auto recover

## Current Progress

| Module | Status |
|--------------------|----------|
| Login and Register | finished |
| Menu Management | finished |
| Role Management | finished |
| Role Authority | finished |
| User Management | finished |
| Operation log | finished |
| Module | Status |
|----------------------|----------|
| Login and Register | finished |
| Menu Management | finished |
| Role Management | finished |
| Role Authority | finished |
| User Management | finished |
| Operation log | finished |
| Service discovery | finished |
| configuration center | finished |

## Preview

Expand Down Expand Up @@ -61,6 +63,7 @@ docsify serve .
- [GORM](https://gorm.io/) - Familiar with GORM apis
- [Casbin](https://casbin.org/) - Familiar with Casbin apis
- [Go-swagger](https://goswagger.io/) - Go-swagger document generation
- [Consul](https://www.consul.io/docs) - Consul

## Install and use

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Simple Admin 是一个开箱即用的分布式微服务后端管理系统,基
| 角色权限 | 已完成 |
| 用户管理 | 已完成 |
| 操作日志 | 已完成 |
| 服务注册发现 | 已完成 |
| 配置中心 | 已完成 |


## 预览

Expand Down Expand Up @@ -61,6 +64,7 @@ docsify serve .
- [GORM](https://gorm.io/) - GORM 数据库ORM组件
- [Casbin](https://casbin.org/) - 权限管理
- [Go-swagger](https://goswagger.io/) - Go-swagger 文档生成调试
- [Consul](https://www.consul.io/docs) - Consul

## 安装使用

Expand Down
13 changes: 12 additions & 1 deletion api/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ package main
import (
"flag"
"fmt"
"github.com/suyuan32/simple-admin-tools/plugins/registry/consul"
"github.com/zeromicro/go-zero/core/logx"

"github.com/suyuan32/simple-admin-core/api/internal/config"
"github.com/suyuan32/simple-admin-core/api/internal/handler"
"github.com/suyuan32/simple-admin-core/api/internal/svc"
_ "github.com/suyuan32/simple-admin-tools/plugins/registry/consul"

"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
Expand All @@ -40,15 +43,23 @@ var configFile = flag.String("f", "etc/core.yaml", "the config file")
func main() {
flag.Parse()

var consulConfig config.ConsulConfig
conf.MustLoad(*configFile, &consulConfig)

var c config.Config
conf.MustLoad(*configFile, &c)
client, err := consulConfig.Consul.NewClient()
logx.Must(err)
consul.LoadYAMLConf(client, "coreApiConf", &c)

server := rest.MustNewServer(c.RestConf, rest.WithCors("*"))
defer server.Stop()

ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)

err = consul.RegisterService(consulConfig.Consul)
logx.Must(err)

fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}
48 changes: 10 additions & 38 deletions api/etc/core.yaml
Original file line number Diff line number Diff line change
@@ -1,38 +1,10 @@
Name: core.api
Host: 0.0.0.0
Port: 8500
Auth:
AccessSecret:
AccessExpire: # Seconds
LogConf:
ServiceName: coreApiLogger
Mode: file
Path: /data/logs/api
Level: info
Compress: false
KeepDays: 7
StackCooldownMillis: 100
RedisConf:
Host: 127.0.0.1:6379
Type: node
CoreRpc:
Etcd:
Hosts:
- 127.0.0.1:2379
Key: core.rpc
Captcha:
KeyLong: 5
ImgWidth: 240
ImgHeight: 80
DatabaseConf:
Type: mysql
Path: 127.0.0.1
Port: 3306
Config: charset=utf8mb4&parseTime=True&loc=Local
DBName: simple_admin
Username:
Password:
MaxIdleConn: 10
MaxOpenConn: 100
LogMode: error
LogZap: false
Consul:
Host: 127.0.0.1:8500 # consul endpoint
ListenOn: 127.0.0.1:9100
#Token: 'f0512db6-76d6-f25e-f344-a98cc3484d42' # consul ACL token (optional)
Key: core.api
Meta:
Protocol: grpc
Tag:
- core
- api
33 changes: 19 additions & 14 deletions api/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
package config

import (
"github.com/zeromicro/go-zero/core/logx"
"github.com/suyuan32/simple-admin-tools/plugins/registry/consul"
"github.com/zeromicro/go-zero/core/stores/gormsql"
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/zrpc"
)

type Config struct {
rest.RestConf
Auth struct {
AccessSecret string
AccessExpire int64
}
LogConf logx.LogConf `json:"LogConf" yaml:"LogConf"`
RedisConf redis.RedisConf `json:"RedisConf" yaml:"RedisConf"`
CoreRpc zrpc.RpcClientConf `json:"CoreRpc" yaml:"CoreRpc"`
Captcha Captcha `json:"Captcha" yaml:"Captcha"`
DB gormsql.GORMConf `json:"DatabaseConf" yaml:"DatabaseConf"`
rest.RestConf `yaml:",inline"`
Auth Auth `json:"auth" yaml:"Auth"`
RedisConf redis.RedisConf `json:"redisConf" yaml:"RedisConf"`
CoreRpc zrpc.RpcClientConf `json:"coreRpc" yaml:"CoreRpc"`
Captcha Captcha `json:"captcha" yaml:"Captcha"`
DB gormsql.GORMConf `json:"databaseConf" yaml:"DatabaseConf"`
}

type Captcha struct {
KeyLong int `json:"KeyLong" yaml:"KeyLong"` // captcha length
ImgWidth int `json:"ImgWidth" yaml:"ImgWidth"` // captcha width
ImgHeight int `json:"ImgHeight" yaml:"ImgHeight"` // captcha height
KeyLong int `json:"keyLong" yaml:"KeyLong"` // captcha length
ImgWidth int `json:"imgWidth" yaml:"ImgWidth"` // captcha width
ImgHeight int `json:"imgHeight" yaml:"ImgHeight"` // captcha height
}

type Auth struct {
AccessSecret string `json:"accessSecret" yaml:"AccessSecret"`
AccessExpire int64 `json:"accessExpire" yaml:"AccessExpire"`
}

type ConsulConfig struct {
Consul consul.Conf
}
3 changes: 0 additions & 3 deletions api/internal/svc/servicecontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ type ServiceContext struct {
}

func NewServiceContext(c config.Config) *ServiceContext {
// initialize logx
logx.MustSetup(c.LogConf)

// initialize redis
rds := c.RedisConf.NewRedis()
logx.Info("Initialize redis connection successfully")
Expand Down
75 changes: 75 additions & 0 deletions deploy/docker-compose/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
version: "3"

networks:
network:
ipam:
driver: default
config:
- subnet: '177.7.0.0/16'

# 设置mysql,redis持久化保存
volumes:
mysql:
redis:

services:
api:
build:
context: ../../api
dockerfile: ../../Dockerfile
container_name: gva-web
restart: always
ports:
- '8080:8080'
depends_on:
- server
command: [ 'nginx-debug', '-g', 'daemon off;' ]
networks:
network:
ipv4_address: 177.7.0.11

server:
build:
context: ../../server
dockerfile: ./Dockerfile
container_name: gva-server
restart: always
ports:
- '8888:8888'
depends_on:
- mysql
- redis
links:
- mysql
- redis
networks:
network:
ipv4_address: 177.7.0.12

mysql:
image: mysql:8.0.21 # 如果您是 arm64 架构:如 MacOS 的 M1,请修改镜像为 image: mysql/mysql-server:8.0.21
container_name: gva-mysql
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci #设置utf8字符集
restart: always
ports:
- "13306:3306" # host物理直接映射端口为13306
environment:
MYSQL_DATABASE: 'qmPlus' # 初始化启动时要创建的数据库的名称
MYSQL_ROOT_PASSWORD: 'Aa@6447985' # root管理员用户密码
volumes:
- mysql:/var/lib/mysql
networks:
network:
ipv4_address: 177.7.0.13

redis:
image: redis:6.0.6
container_name: gva-redis # 容器名
restart: always
ports:
- '16379:6379'
volumes:
- redis:/data
networks:
network:
ipv4_address: 177.7.0.14
Binary file added docs/simple-admin/assets/consul.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/simple-admin/assets/consul_kv.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion docs/simple-admin/en/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
* [Swagger](simple-admin/en/docs/swagger.md)
* [File Manager](/simple-admin/en/docs/file_manager.md)
* [Web setting](/simple-admin/en/docs/web-setting.md)
* [Validator](/simple-admin/zh-cn/docs/validator.md)
* [Validator](/simple-admin/en/docs/validator.md)
* [Consul](/simple-admin/en/docs/consul.md)
Loading

0 comments on commit 0a090e8

Please sign in to comment.