Serverpod 适合全栈 Dart 开发吗?

简介: Serverpod 是一款适用于全栈Dart开发的框架,尤其适合熟悉Dart的开发者。它提供了Docker部署、代码自动生成、数据库连接等功能,简化了前后端开发流程。通过Serverpod,可以创建单体服务架构,但处理复杂业务时可能需要借助其他语言的丰富生态。框架包括对postgres和redis的支持,以及可视化工具Insights。开发步骤涉及安装、创建项目、启动服务和配置文件。Serverpod还允许动态生成接口和数据库代码,简化CURD操作。虽然适合简单到中等复杂度的应用,对于更复杂需求,可能需要结合其他语言或服务。

Serverpod 适合全栈 Dart 开发吗?

视频

https://youtu.be/RiicBU8fE-o

https://www.bilibili.com/video/BV1nM4m1k7DE/

前言

原文 https://ducafecat.com/blog/full-stack-development-with-dart-and-serverpod

Serverpod 是一个符合现代化需求的大前端框架,支持 Docker 部署、代码自动生成、数据库连接等功能。对于只会 Dart 语言的开发者来说,Serverpod 是否能胜任全栈开发?本文为您详细评估 Serverpod 的优势和局限性。

Serverpod, Dart 全栈开发, Dart 开发框架, 大前端框架, 全栈开发解决方案

Serverpod

https://serverpod.dev/

主要功能有:

  • Docker 生态部署
  • 代码自动生成 model、dao、controller、client sdk
  • 连接 postgres、redis
  • 可配置 yaml , 开发、生产隔离
  • 可视化日志UI工具 insights
  • 缓存、日志、验证、上传文件、健康检查、调度任务、流消息、RPC

如果你只是用来做以上业务,没有问题,但是再复杂些就要靠生态了,如图片加工、视频压缩、经纬度转换、P2P等等,你估计要用 java、go 这种生态丰富的语言来搭建了,然后用 Serverpod 来访问,当然你也可以用 saas 云服务解决。

如果你正在构建一个精简的单体服务架构,你可以从 Serverpod 的设计中寻找灵感。

参考

https://github.com/serverpod/serverpod

https://docs.serverpod.dev/

https://docs.serverpod.dev/insights

https://marketplace.visualstudio.com/items?itemName=serverpod.serverpod

前提条件

步骤

1 全局安装 serverpod 命令行工具

安装

$ dart pub global activate serverpod_cli

自检

$ serverpod
❯ serverpod 

Manage your serverpod app development

Usage: serverpod <command> [arguments]

Global options:
-h, --help              Print this usage information.
-q, --quiet             Suppress all serverpod cli output. Is overridden by  -v, --verbose.
-v, --verbose           Prints additional information useful for development. Overrides --q, --quiet.
-a, --[no-]analytics    Toggles if analytics data is sent to Serverpod.
                        (defaults to on)

Available commands:
  create                    Creates a new Serverpod project, specify project name (must be lowercase with no special characters).
  create-migration          Creates a migration from the last migration to the current state of the database.
  create-repair-migration   Repairs the database by comparing the target state to what is in the live database instead of comparing to
                            the latest migration.
  generate                  Generate code from yaml files for server and clients.
  language-server           Launches a serverpod language server communicating with JSON-RPC-2 intended to be used with a client
                            integrated in an IDE.
  version                   Prints the active version of the Serverpod CLI.

Run "serverpod help <command>" for more information about a command.

2 创建项目

create 命令

$ serverpod create mypod

mypod 是项目名称

目录

代码 名称
mypod_client 客户端链接库
mypod_flutter flutter 项目
mypod_server serverpod 服务端

3 启动项目

Docker 方式启动 postgres、redis

$ cd mypod/mypod_server
$ docker compose up --build --detach

启动 Serverpod 服务

$ dart bin/main.dart --apply-migrations

启动 Flutter 程序

$ cd mypod/mypod_flutter
$ flutter run -d chrome

4 配置文件

开发、生产、测试 配置

mypod_server/config/development.yaml

mypod_server/config/production.yaml

mypod_server/config/staging.yaml

# This is the configuration file for your local development environment. By
# default, it runs a single server on port 8080. To set up your server, you will
# need to add the name of the database you are connecting to and the user name.
# The password for the database is stored in the config/passwords.yaml.
#
# When running your server locally, the server ports are the same as the public
# facing ports.

# Configuration for the main API server.
apiServer:
  port: 8080
  publicHost: localhost
  publicPort: 8080
  publicScheme: http

# Configuration for the Insights server.
insightsServer:
  port: 8081
  publicHost: localhost
  publicPort: 8081
  publicScheme: http

# Configuration for the web server.
webServer:
  port: 8082
  publicHost: localhost
  publicPort: 8082
  publicScheme: http

# This is the database setup for your server.
database:
  host: localhost
  port: 8090
  name: mypod
  user: postgres

# This is the setup for Redis.
redis:
  enabled: false
  host: localhost
  port: 8091

密码配置

mypod_server/config/passwords.yaml

# Use this file to store passwords to services that your server you use. When
# the server starts, the passwords will be automatically loaded and can be
# accessed from the `session.passwords` field. If you don't have access to a
# session object, passwords can also be accessed from
# `Serverpod.instance.passwords`. You can provide different passwords for
# different run configurations. If you want the same password for any
# configuration used, place it under `shared`.
#
# Note that this file should not be under version control. Store it in a safe
# place.

# Save passwords used across all configurations here.
shared:
  mySharedPassword: 'my password'

# These are passwords used when running the server locally in development mode
development:
  database: 'jBXWslvjYpfszBbFKS661o1MrY1esjVC'
  redis: 'ijH4ON9CCHtK9Cojnqivvym1zxHkwjig'

  # The service secret is used to communicate between servers and to access the
  # service protocol.
  serviceSecret: 'uVoVJ-cqnwc9Bn8gQG-ZLtYfgXGnMUu3'

# Passwords used in your staging environment if you use one. The default setup
# use a password for Redis.
staging:
  database: '3_5RPi7yVOSwSEhr8ecNftp1K4H0_7nL'
  serviceSecret: 'JgM8DPORU5OwOyG23_gdjhYuRLD6mk1J'

# Passwords used in production mode.
production:
  database: 'ReE97mCuS0QaxlXEy4hNMQYtE_Q1lxP4'
  serviceSecret: '0XpMqP4aybryFxm4N8V5ZXHcPFTHUXpP'

生成配置

type: server

client_package_path: ../mypod_client

docker 服务配置

mypod_server/docker-compose.yaml

version: '3.7'

services:
  postgres:
    image: postgres:14.1
    ports:
      - '8090:5432'
    environment:
      POSTGRES_USER: postgres
      POSTGRES_DB: mypod
      POSTGRES_PASSWORD: "jBXWslvjYpfszBbFKS661o1MrY1esjVC"
    volumes:
      - mypod_data:/var/lib/postgresql/data
  redis:
    image: redis:6.2.6
    ports:
      - '8091:6379'
    command: redis-server --requirepass "ijH4ON9CCHtK9Cojnqivvym1zxHkwjig"
    environment:
      - REDIS_REPLICATION_MODE=master
volumes:
  mypod_data:

5 动态生成接口代码

添加 example.hello2 方法

mypod_server/lib/src/endpoints/example_endpoint.dart

class ExampleEndpoint extends Endpoint {
   
   
  // You create methods in your endpoint which are accessible from the client by
  // creating a public method with `Session` as its first parameter.
  // `bool`, `int`, `double`, `String`, `UuidValue`, `Duration`, `DateTime`, `ByteData`,
  // and other serializable classes, exceptions and enums from your from your `protocol` directory.
  // The methods should return a typed future; the same types as for the parameters are
  // supported. The `session` object provides access to the database, logging,
  // passwords, and information about the request being made to the server.
  Future<String> hello(Session session, String name) async {
   
   
    return 'Hello $name';
  }

  Future<String> hello2(Session session, String name) async {
   
   
    return 'Hello $name';
  }
}

执行生成命令

$ cd mypod/mypod_server
$ serverpod generate

查看 generated 目录的文件变化

mypod_server/lib/src/generated

客户端 lib 库

mypod_client/lib/src/protocol/client.dart

class EndpointExample extends _i1.EndpointRef {
   
   
  EndpointExample(_i1.EndpointCaller caller) : super(caller);

  
  String get name => 'example';

  _i2.Future<String> hello(String name) => caller.callServerEndpoint<String>(
        'example',
        'hello',
        {
   
   'name': name},
      );

  _i2.Future<String> hello2(String name) => caller.callServerEndpoint<String>(
        'example',
        'hello2',
        {
   
   'name': name},
      );
}

多了 hello2 的定义

Flutter 项目中调用

mypod_flutter/lib/main.dart

  void _callHello2() async {
   
   
    try {
   
   
      final result = await client.example.hello2(_textEditingController.text);
      setState(() {
   
   
        _errorMessage = null;
        _resultMessage = result;
      });
    } catch (e) {
   
   
      setState(() {
   
   
        _errorMessage = '$e';
      });
    }
  }
...

  Padding(
              padding: const EdgeInsets.only(bottom: 16.0),
              child: ElevatedButton(
                onPressed: _callHello2,
                child: const Text('Send to Server'),
              ),
            ),

6 动态生成数据库代码

编写 model 定义

mypod_server/lib/src/models/note.yaml

class: Note
table: note
fields:
  text: String

生成代码

serverpod generate

编写 CURD 接口定义

mypod_server/lib/src/endpoints/notes_endpoint.dart

import 'package:serverpod/server.dart';

import '../generated/protocol.dart';

class NotesEndpoint extends Endpoint {
   
   
  Future<List<Note>> getAllNotes(Session session) async {
   
   
    // By ordering by the id column, we always get the notes in the same order
    // and not in the order they were updated.
    return await Note.db.find(
      session,
      orderBy: (t) => t.id,
    );
  }

  Future<void> createNote(Session session, Note note) async {
   
   
    await Note.db.insertRow(session, note);
  }

  Future<void> deleteNote(Session session, Note note) async {
   
   
    await Note.db.deleteRow(session, note);
  }
}

生成迁移内容

$ serverpod create-migration

服务器启动应用迁移

$ cd mypod/mypod_server
$ dart bin/main.dart --apply-migrations

重启服务

迁移完成后需要重新启动服务器

dart bin/main.dart --apply-migrations

Flutter 编写代码

mypod_flutter/lib/main.dart

  void _callNoteCreate() async {
   
   
    try {
   
   
      await client.notes.createNote(Note(text: "this is example."));
      setState(() {
   
   
        _errorMessage = null;
        _resultMessage = "create send~";
      });
    } catch (e) {
   
   
      setState(() {
   
   
        _errorMessage = '$e';
      });
    }
  }
Padding(
              padding: const EdgeInsets.only(bottom: 16.0),
              child: ElevatedButton(
                onPressed: _callNoteCreate,
                child: const Text('Note Create Send To Server'),
              ),
            ),

Flutter 项目访问测试

$ cd mypod/mypod_flutter
$ flutter run -d chrome

7 部署项目

Docker 部署

mypod_server/Dockerfile

FROM dart:3.2.5 AS build

WORKDIR /app
COPY . .

RUN dart pub get
RUN dart compile exe bin/main.dart -o bin/server

FROM alpine:latest

ENV runmode=production
ENV serverid=default
ENV logging=normal
ENV role=monolith

COPY --from=build /runtime/ /
COPY --from=build /app/bin/server server
COPY --from=build /app/config/ config/
COPY --from=build /app/web/ web/

EXPOSE 8080
EXPOSE 8081
EXPOSE 8082

ENTRYPOINT ./server --mode=$runmode --server-id=$serverid --logging=$logging --role=$role

构建 docker 包

$ cd mypod/mypod_server
$ docker build -t mypod_server:v1.0 .

原生部署

mypod_server/deploy

  • aws
  • gcp

代码

官方有些不错的例子

https://docs.serverpod.dev/tutorials/code-example

小结

Serverpod 是一个功能强大的 Dart 全栈开发框架,为只掌握 Dart 语言的开发者提供了一个全面的解决方案。它支持 Docker 部署、代码自动生成、数据库连接等关键功能,满足了现代化应用开发的需求。但是对于更加复杂的业务场景,开发者可能还需要使用其他语言的生态工具。总的来说,Serverpod 是一个值得 Dart 开发者认真考虑的全栈开发框架,能够大幅提高开发效率和部署效果。

感谢阅读本文

如果有什么建议,请在评论中让我知道。我很乐意改进。


flutter 学习路径


© 猫哥
ducafecat.com

end

相关文章
|
安全 网络虚拟化 网络架构
配置通过流策略实现不同网段间限制互访示例
MQC是指通过将具有某类共同特征的报文划分为一类,并为同一类报文提供相同的服务,也可以对不同类的报文提供不同的服务。 高级ACL可以在规则中使用源IP地址和目的IP地址信息来定义允许和拒绝通过的数据流。在流分类中按照ACL对报文进行分类,并指定对匹配的报文采取的动作(允许或拒绝通过)。本例就是使用该方法实现不同网段间的互访限制。
554 6
|
12月前
|
人工智能 自然语言处理 搜索推荐
企业AI客服系统建设费用大揭秘:免费系统能用吗?如何控制预算?
面对AI客服升级需求,企业需平衡成本与服务。免费系统适合小微企轻量入门,但功能有限;瓴羊、Salesforce、Zendesk及探域等高性价比方案,分别在全链路智能、全球化服务、电商垂直场景具优势。不同规模企业应结合咨询量、行业特性与预算,选择匹配的AI客服解决方案,实现服务与增长双提升。(239字)
|
JSON JavaScript 中间件
POST 请求如何处理表单数据?
【10月更文挑战第24天】POST请求处理表单数据需要客户端和服务器端的协同工作,客户端负责将表单数据正确地编码并发送给服务器,服务器端则需要准确地接收、验证和处理数据,并向客户端返回合适的响应。
1370 163
|
人工智能 IDE Java
IntelliJ IDEA 2025.1 完整安装指南:激活 + 中文设置 + 全程丝滑 附安装包
IntelliJ IDEA 2025.1 发布,全面支持 Java 24、默认启用 Kotlin K2 模式,增强 Scala 3 支持。AI 助手免费使用,支持 GPT-4.1、Claude 3.7 等云模型,提升代码补全与多文件编辑能力。调试、终端架构及 Markdown 预览搜索等功能全面优化,开发体验更高效流畅。
10539 3
IntelliJ IDEA 2025.1 完整安装指南:激活 + 中文设置 + 全程丝滑 附安装包
|
11月前
|
人工智能 运维 前端开发
[深度测评] 2026 阿里云开发者选型:通义灵码与主流 AI 编程助手的“双剑合璧”实战指南
在阿里云的云原生生态下,AI 编程助手已成为标配。作为一名在阿里云生态摸爬滚打多年的全栈开发者,我实测了市面上主流的工具。结论是:成年人不做选择,极致的效率往往来自“组合拳”。 本文将从 Web 开发视角,深度解析通义灵码与行业顶尖竞品文心快码 (Comate) 的实战表现。
|
SQL NoSQL 数据库
Flutter Hive NoSql 数据库使用指南
本文将会写一个 Hive CURD 的例子,详细介绍 Hive 这个轻量级的 Flutter 离线数据库的使用方法,包括 Hive 在 Flutter 开发中的重要性、Hive 与 SQLite 的比较等,帮助开发者快速上手 Hive 数据库。
799 6
Flutter Hive NoSql 数据库使用指南
|
Linux
Debian下载ISO镜像的方法
步骤 1:访问Debian官方网站 打开你的网络浏览器,在地址栏中输入 https://www.debian.org/ 并回车,这将带你到Debian的官方网站。
3604 6
Debian下载ISO镜像的方法
|
存储 容器
Flutter 构建自适应布局
Flutter 构建自适应布局
765 2
Flutter 构建自适应布局
|
开发者 iOS开发 C#
Uno Platform 入门超详细指南:从零开始教你打造兼容 Web、Windows、iOS 和 Android 的跨平台应用,轻松掌握 XAML 与 C# 开发技巧,快速上手示例代码助你迈出第一步
【8月更文挑战第31天】Uno Platform 是一个基于 Microsoft .NET 的开源框架,支持使用 C# 和 XAML 构建跨平台应用,适用于 Web(WebAssembly)、Windows、Linux、macOS、iOS 和 Android。它允许开发者共享几乎全部的业务逻辑和 UI 代码,同时保持原生性能。选择 Uno Platform 可以统一开发体验,减少代码重复,降低开发成本。安装时需先配置好 Visual Studio 或 Visual Studio for Mac,并通过 NuGet 或官网下载工具包。
2306 0
|
JavaScript 前端开发 网络架构
1024特刊|前端开发:Vue路由传递参数和重定向的使用总结
前端开发过程中,作为前端开发者来说关于vue的使用并不陌生,vue相关常用的知识点也是非常重要的,不管是在实际开发中还是在求职面试中都很重要。在vue使用中,路由相关的知识点是非常重要的,而且在实际开发中也是必用知识点,那么本篇博文就来聊聊vue的路由参数传递和路由重定向相关的知识点。
1557 2
1024特刊|前端开发:Vue路由传递参数和重定向的使用总结