Skip to content

Commit

Permalink
update daily
Browse files Browse the repository at this point in the history
  • Loading branch information
shiqinfeng1 committed Jan 7, 2024
1 parent 9e14f44 commit e8fbc55
Show file tree
Hide file tree
Showing 20 changed files with 325 additions and 91 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ api:
.PHONY: build
# build execute
build:
# go build -ldflags "app/bff-gomono/cmd/cmd.Version=$(VERSION)" -o ./deploy/bin/ ./...
# go build -ldflags "app/bff-gomono/cmd/cmd.Version=$(VERSION)" -o ./deploy/standalone/bin/ ./...
for x in $(names); do \
echo -e "\nbuild $$x ... $(MODULE)/$$x/cmd.Version=$(VERSION)"; \
go build -ldflags "-X $(MODULE)/$$x/cmd.Version=$(VERSION)" -o ./deploy/bin/ ./$$x; \
go build -ldflags "-X $(MODULE)/$$x/cmd.Version=$(VERSION)" -o ./deploy/standalone/bin/ ./$$x; \
echo "ok"; \
done

Expand Down
2 changes: 1 addition & 1 deletion app/biz-trainer/cmd/svc-trainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func newApp(register *conf.Register, logger klog.Logger, regstr registry.Registr
kratos.Server(
gs,
),
// kratos.Registrar(regstr),
kratos.Registrar(regstr),
kratos.Endpoint(&url.URL{Scheme: "http", Host: register.Endpoints[0]}), // 指定服务地址,该地址会提交给注册中心,如果不指定,那么将注册容器内部地址,导致外部无法访问
)
}
Expand Down
2 changes: 1 addition & 1 deletion app/biz-training/cmd/svc-training.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func newApp(register *conf.Register, logger klog.Logger, regstr registry.Registr
kratos.Server(
gs,
),
// kratos.Registrar(regstr),
kratos.Registrar(regstr),
kratos.Endpoint(&url.URL{Scheme: "http", Host: register.Endpoints[0]}), // 指定服务地址,该地址会提交给注册中心,如果不指定,那么将注册容器内部地址,导致外部无法访问
)
}
Expand Down
237 changes: 157 additions & 80 deletions app/common/conf/public.pb.go

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions app/common/conf/public.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ message Public{
Registry registry = 3;
Discovery discovery = 4;
Adapter adapter = 5;
GatewayRegister gateway_register = 6;
}
message Log {
string level = 1;
Expand Down Expand Up @@ -48,3 +49,7 @@ message Adapter {
Database database = 1;
Redis redis = 2;
}

message GatewayRegister {
repeated string endpoints = 1;
}
11 changes: 9 additions & 2 deletions app/common/discovery/nacos.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package discovery

import (
"errors"

"github.com/go-kratos/kratos/contrib/registry/nacos/v2"
"github.com/go-kratos/kratos/v2/registry"
"github.com/shiqinfeng1/goMono/app/common/client"
)

func MustNacosDiscovery(endpoint string) registry.Discovery {
var ErrServiceKindInvaild = errors.New("service kind is invalid")

func MustNacosDiscovery(endpoint string, kind string) registry.Discovery {
c, err := client.NewNacosNamingClient(endpoint)
if err != nil {
panic(err)
}
return nacos.New(c)
if kind != "grpc" && kind != "http" {
panic(ErrServiceKindInvaild)
}
return nacos.New(c, nacos.WithDefaultKind(kind))
}
11 changes: 9 additions & 2 deletions app/gateway-gomono/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ func init() {
rand.Seed(uint64(time.Now().Nanosecond()))
// 动态更新配置。key:需要监听的字段;value:配置变化后的处理函数
onChanges := map[string]func(key string, value kcfg.Value){
"public.log.level": func(key string, value kcfg.Value) {
_ = key
lvl, _ := value.String()
log.SetLevel(lvl) // 动态更新level等级
},
"middlewares": func(key string, value kcfg.Value) {
_ = key
var middl []*gcfg.Middleware
Expand Down Expand Up @@ -96,10 +101,11 @@ func main() {
Version: gatewayCfg.Version,
}, pubCfg.Log)

klog.SetLogger(logger)
// 实例化一个main函数使用的log
l := klog.NewHelper(klog.With(logger, "scope", "main"))

clientFactory := client.NewFactory(discovery.MustNacosDiscovery(pubCfg.Discovery.Endpoints[0]))
// util.SetFilterNetNumberAndMask("192.168.68.0/24")
clientFactory := client.NewFactory(discovery.MustNacosDiscovery(pubCfg.Discovery.Endpoints[0], "http"))
var err error
p, err = proxy.New(clientFactory, middleware.Create)
if err != nil {
Expand Down Expand Up @@ -128,6 +134,7 @@ func main() {
),
kratos.Metadata(map[string]string{}),
kratos.Logger(logger),
// kratos.Endpoint(&url.URL{Scheme: "http", Host: pubCfg.GatewayRegister.Endpoints[0]}), // 指定服务地址,该地址会提交给注册中心,如果不指定,那么将注册容器内部地址,导致外部无法访问
)
if err := app.Run(); err != nil {
l.Errorf("failed to run servers: %v", err)
Expand Down
11 changes: 10 additions & 1 deletion deploy/docker/ansible_playbook/install_bff.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,21 @@
- "{{bff_compose_file}}"
- name: Copy BFF config Files
template:
src: ../../config/{{ item }}
src: ../tmpl/{{ item }}
dest: "{{bff_config_dir}}/{{ item }}"
force: true
loop:
- "public.yaml"
- "bff.yaml"
- name: Backup BFF config Files
delegate_to: localhost
template:
src: ../tmpl/{{ item }}
dest: ../../standalone/config/{{ item }}
force: true
loop:
- "public.yaml"
- "bff.yaml"

- name: Stop BFF
shell:
Expand Down
11 changes: 10 additions & 1 deletion deploy/docker/ansible_playbook/install_gateway.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,21 @@
- "{{gateway_compose_file}}"
- name: Copy Gateway config Files
template:
src: ../../config/{{ item }}
src: ../tmpl/{{ item }}
dest: "{{gateway_config_dir}}/{{ item }}"
force: true
loop:
- "gateway.yaml"
- "public.yaml"
- name: Backup Gateway config Files
delegate_to: localhost
template:
src: ../tmpl/{{ item }}
dest: ../../standalone/config/{{ item }}
force: true
loop:
- "gateway.yaml"
- "public.yaml"

- name: Stop Gateway
shell:
Expand Down
1 change: 1 addition & 0 deletions deploy/docker/ansible_playbook/vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ registry_srv_port: 8080 # docker私有仓库的服务端口
#########################################################################
mode: develop # develop test product
debug: true
proxy_host: 192.168.72.84
proxy_port1: 8180
proxy_port2: 8181
proxy_addrs: :{{proxy_port1}},:{{proxy_port2}} # 网关服务地址,可以由多个,逗号隔开
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ endpoints:
# 请求域名
# host: localhost
backends:
# - target: '{{bff_host}}:{{bff_port}}'
- target: 'discovery:///bff.http'
weight: 100
# - target: '{{bff_host}}:{{bff_port}}'
# 各个endpoint的中间件
middlewares:
- name: circuitbreaker
Expand Down
4 changes: 4 additions & 0 deletions deploy/config/public.yaml → deploy/docker/tmpl/public.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ public:
discovery:
endpoints:
- "{{nacos_host}}:{{nacos1_srv_port}}"

gateway_register:
endpoints:
- "{{proxy_host}}:{{proxy_port1}}"
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions deploy/standalone/config/bff.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
server:
http:
addr: ":{{bff_port}}"
timeout: 1s
# 本服务的服务注册地址
register:
endpoints:
- "{{bff_host}}:{{bff_port}}"
53 changes: 53 additions & 0 deletions deploy/standalone/config/gateway.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# This is a gateway config.
name: gateway-gomono
version: v1.0.0
# 全局中间件
middlewares:
- name: tracing
options:
'@type': type.googleapis.com/gateway.middleware.tracing.v1.Tracing
httpEndpoint: '192.168.72.84:4318' # default opentelemetry collector port
insecure: true
- name: logging
- name: transcoder
- name: cors
options:
'@type': type.googleapis.com/gateway.middleware.cors.v1.Cors
allowCredentials: true
allowOrigins:
- .google.com
allowMethods:
- GET
- POST
- OPTIONS
endpoints:
- path: /demo/v1/*
timeout: 1s
# 业务服务使用的通信协议
protocol: HTTP
# 请求域名
# host: localhost
backends:
- target: 'discovery:///bff.http'
weight: 100
# - target: '192.168.72.84:8200'
# 各个endpoint的中间件
middlewares:
- name: circuitbreaker
options:
'@type': type.googleapis.com/gateway.middleware.circuitbreaker.v1.CircuitBreaker
successRatio: {"success":0.6, "request":"1", "bucket":"10", "window":"3s"}
backupService: {"endpoint":{"backends":[{"target":"127.0.0.1:8001"}]}}
assertCondtions:
- {"by_status_code":"200"}
- name: rewrite
options:
'@type': type.googleapis.com/gateway.middleware.rewrite.v1.Rewrite
retry:
attempts: 3
perTryTimeout: 0.1s
conditions:
- byStatusCode: '502-504'
- byHeader:
name: 'Grpc-Status'
value: '14'
35 changes: 35 additions & 0 deletions deploy/standalone/config/public.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

public:
adapter:
database:
driver: mysql
source: "root:root@tcp(192.168.72.84:23306)/test"
redis:
addr: 127.0.0.1:26379
read_timeout: 0.2s
write_timeout: 0.2s
log:
level: debug
monitor:
endpoint: tcp://127.0.0.1:23224
file:
max_size: 100 # megabytes
max_backups: 7 # file numbers
max_age: 28 # days
compress: true

registry:
addrs:
- "192.168.72.84:8848"
- "192.168.72.84:8858"
- "192.168.72.84:8868"
trace:
endpoint: "192.168.72.84:4318"

discovery:
endpoints:
- "192.168.72.84:8848"

gateway_register:
endpoints:
- "192.168.72.84:8180"
9 changes: 9 additions & 0 deletions deploy/standalone/config/trainer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

server:
grpc:
addr: "0.0.0.0:{{biz_trainer_port}}"
timeout: 1s
# 本服务的服务注册地址
register:
endpoints:
- "{{biz_trainer_host}}:{{biz_trainer_port}}"
9 changes: 9 additions & 0 deletions deploy/standalone/config/training.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

server:
grpc:
addr: "0.0.0.0:{{biz_training_port}}"
timeout: 1s
# 本服务的服务注册地址
register:
endpoints:
- "{{biz_training_host}}:{{biz_training_port}}"

0 comments on commit e8fbc55

Please sign in to comment.