8

Spring Cloud golang混合架构的一些优化

 3 years ago
source link: https://studygolang.com/articles/32395
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

因为本身是从java转到golang的,而且团队一直使用Spring Cloud,在实际项目中,golang开发的服务是要保持和Spring Cloud一致,并且架构尽可能做到和Spring Cloud兼容,所以下面观点仅供参考

repository(数据层)要用代码生成器生成,减少人为维护工作,减少错误

repository是基于xorm的(https://gitea.com/xorm/xorm)是自己基于Spring Boot写了一个代码生成器,支持生成entity,基础crud repository,model和把java中的model转换成golang struct

repository的方法命名兼容Spring JPA(https://docs.spring.io/spring-data/jpa/docs/2.4.2

mvc架构使用的是iris(https://github.com/kataras/iris),配置文件没有使用iris的本身配置,而是直接兼容Spring Cloud的bootstrap.yml

spring:
  application:
    name: stats-report-service
    
  profiles:
    active: development-local
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery: 
        ip-address: 
      config:
        prefix: ydyun360-config
  datasource:
    url: ""
    maxIdleConns: 10
    maxOpenConns: 10
    connMaxLifetime: 1800000
  redis:
    host: localhost
    port: 6379
    password:

date-format: 2006-01-02
date-time-format: 2006-01-02 15:04:05
time-zone: Asia/Shanghai 
 
server:
  port: 8023

约定了一些简单规则,比如:

repository r
service s
controller c
数据结构体 entity
服务结构体 m
初始化slice的时候,假如已知长度,可以直接make([]type,len),假如知道最大长度,可以直接make([]type,0,len)

使用golang容器静态编译,基于Dockerfile发布


有疑问加站长微信联系(非本文作者)

280

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK