If you are new to userver it is a good idea to start with using the service template
git repository to design your first userver-based service.
For a service without a database use https://github.com/userver-framework/service_template
1. Clone service template
and userver repositories.
More information about hello service
can be found here: Writing your first HTTP server
2. Install userver dependencies.
There is an option to use a docker container.
Alternatively you can install build dependencies for userver.
For example to install ubuntu_22_04 build dependencies:
3. Build and start your service.
During the build, you can make a coffee break until approximately the following output appears:
4. Try to send a request.
The answer should be something like this:
5. Now you are ready for fast and comfortable creation of C++ microservices, services and utilities!
There are prepared and ready to use service templates at the github:
Link | Contains |
---|---|
https://github.com/userver-framework/service_template | |
https://github.com/userver-framework/pg_service_template | postgreSQL |
https://github.com/userver-framework/pg_grpc_service_template | postgreSQL, gRPC |
Just use the template to make your own service:
git clone your-service-repo && cd your-service-repo
For local development of your service either
make docker-test
;Makefile.local
file to provide platform-specific CMake options to the template:The service templates allow to kickstart the development of your production-ready service, but there can't be a repo for each and every combination of userver libraries. To use additional userver libraries, e.g. userver::grpc
, add to the root CMakeLists.txt
:
See tutorial_services for minimal usage examples of various userver libraries.
userver uses CPM for downloading missing packages.
By default, we first try to find the corresponding system package. With USERVER_FORCE_DOWNLOAD_PACKAGES=ON
, no such attempt is made, and we skip straight to CPMAddPackage
. This can be useful to guarantee that the build uses the latest (known to userver) versions of third-party packages.
You can control the usage of CPM with USERVER_DOWNLOAD_*
options. See cmake_options. For example, USERVER_DOWNLOAD_PACKAGES=OFF
is useful for CI and other controlled environments to make sure that no download attempts are made, which ensures repeatability and the best CI build speed.
For details on the download options, refer to CPM documentation. Some advice:
CPM_SOURCE_CACHE
helps to avoid re-downloads with multiple userver build modes or multiple CPM-using projects;CPM_USE_NAMED_CACHE_DIRECTORIES
(which userver enables by default) avoids junk library names shown in IDEs.userver itself can be downloaded and built using CPM.
First, install build dependencies. There are options:
Make sure to enable the CMake options to build userver libraries you need, then link to those libraries.
You can install userver globally and then use it from anywhere with find_package
. Make sure to use the same build mode as for your service, otherwise subtle linkage issues will arise.
To build libuserver-all-dev.deb
package run the following shell command:
And install the package with the following:
To install userver build it with USERVER_INSTALL=ON
flags in Debug
and Release
modes:
Next, write
in your CMakeLists.txt
. Choose only the necessary components.
Finally, link your source with userver component.
For instance:
Done! You can use installed userver.
Link mariadbclient
additionally for mysql component:
The Docker images provide a container with all the build dependencies preinstalled and with a proper setup of PPAs with databases, compilers and tools:
Image reference | Contains |
---|---|
ghcr.io/userver-framework/ubuntu-22.04-userver-pg:latest | PostgreSQL and userver preinstalled |
ghcr.io/userver-framework/ubuntu-22.04-userver:latest | userver preinstalled |
ghcr.io/userver-framework/ubuntu-22.04-userver-base:latest | only userver dependencies preinstalled |
To run it just use a command like
After that, install the databases and compiler you are planning to use via apt install
and start developing.
To install userver in ghcr.io/userver-framework/ubuntu-22.04-userver-base:latest
you should run the following command
Alternatively see userver_install
scripts/docker/ubuntu-22.04-pg.dockerfile
, scripts/docker/ubuntu-22.04.dockerfile
and scripts/docker/base-ubuntu-22.04.dockerfile
respectively. See scripts/docker/
directory and Building Docker container for userver for more inspiration on building your own custom docker containers.Thanks to Open-Source community we have Conan support.
You must run the following in the userver directory:
Make sure to pass flags corresponding to the desired userver libraries, e.g. --with_grpc=1
Now you can use userver as conan package and build it in your services:
The userver framework is available at Yandex Cloud Marketplace.
To create a VM with preinstalled userver just click the "Create VM" button and pay for the Cloud hardware usage.
After that the VM is ready to use. SSH to it and use find_package(userver REQUIRED)
in the CMakeLists.txt
to use the preinstalled userver framework.
You can start with service template.
If there a need to update the userver in the VM do the following:
PGO compilation consists of 2 compilation stages: profile collecting and compilation with PGO. You can use PGO compilation doing the following steps:
1) configure userver AND your service with cmake option -DUSERVER_PGO_GENERATE=ON, compile the service; 2) run the resulting binary under the production-like workload to collect profile; 3) run llvm-profdata to convert profraw profile data format into profdata: llvm-profdata merge -output=code.profdata default.profraw 4) configure userver AND your service with cmake option -DUSERVER_PGO_USE=<path_to_profdata>, compile the service.
The resulting binary should be 2-15% faster than without PGO, depending on the code and workload.