getting_started.md 12.6 KB
Newer Older
1
# BUILD
2

gejun's avatar
gejun committed
3
brpc prefers static linkages of deps, so that they don't have to be installed on every machine running the app.
4

5 6
brpc depends on following packages:

7 8 9
* [gflags](https://github.com/gflags/gflags): Extensively used to define global options.
* [protobuf](https://github.com/google/protobuf): Serializations of messages, interfaces of services.
* [leveldb](https://github.com/google/leveldb): Required by [/rpcz](rpcz.md) to record RPCs for tracing.
10

zhujiashun's avatar
zhujiashun committed
11
# Supported Environment
zhujiashun's avatar
zhujiashun committed
12

zhujiashun's avatar
zhujiashun committed
13 14
* [Ubuntu/LinuxMint/WSL](#ubuntulinuxmintwsl)
* [Fedora/CentOS](#fedoracentos)
zhujiashun's avatar
zhujiashun committed
15 16 17
* [Linux with self-built deps](#linux-with-self-built-deps)
* [MacOS](#macos)

18
## Ubuntu/LinuxMint/WSL
19 20
### Prepare deps

Ge Jun's avatar
Ge Jun committed
21
Install common deps, [gflags](https://github.com/gflags/gflags), [protobuf](https://github.com/google/protobuf), [leveldb](https://github.com/google/leveldb):
Ge Jun's avatar
Ge Jun committed
22
```shell
Ge Jun's avatar
Ge Jun committed
23
sudo apt-get install -y git g++ make libssl-dev libgflags-dev libprotobuf-dev libprotoc-dev protobuf-compiler libleveldb-dev
24 25
```

26
If you need to statically link leveldb:
Ge Jun's avatar
Ge Jun committed
27
```shell
Ge Jun's avatar
Ge Jun committed
28
sudo apt-get install -y libsnappy-dev
29
```
30

Ge Jun's avatar
Ge Jun committed
31 32
If you need to enable cpu/heap profilers in examples:
```shell
Ge Jun's avatar
Ge Jun committed
33
sudo apt-get install -y libgoogle-perftools-dev
Ge Jun's avatar
Ge Jun committed
34 35
```

36 37
If you need to run tests, install and compile libgtest-dev (which is not compiled yet):
```shell
Ge Jun's avatar
Ge Jun committed
38
sudo apt-get install -y cmake libgtest-dev && cd /usr/src/gtest && sudo cmake . && sudo make && sudo mv libgtest* /usr/lib/ && cd -
39 40 41
```
The directory of gtest source code may be changed, try `/usr/src/googletest/googletest` if `/usr/src/gtest` is not there.

42
### Compile brpc with config_brpc.sh
43
git clone brpc, cd into the repo and run
Ge Jun's avatar
Ge Jun committed
44
```shell
45
$ sh config_brpc.sh --headers=/usr/include --libs=/usr/lib
46
$ make
47
```
48 49 50
To change compiler to clang, add `--cxx=clang++ --cc=clang`.

To not link debugging symbols, add `--nodebugsymbols` and compiled binaries will be much smaller.
51

Ge Jun's avatar
Ge Jun committed
52 53
To use brpc with glog, add `--with-glog`.

gejun's avatar
gejun committed
54
To enable [thrift support](../en/thrift.md), install thrift first and add `--with-thrift`.
Ge Jun's avatar
Ge Jun committed
55

56
**Run example**
57

Ge Jun's avatar
Ge Jun committed
58
```shell
59 60 61 62
$ cd example/echo_c++
$ make
$ ./echo_server &
$ ./echo_client
63
```
64

65
Examples link brpc statically, if you need to link the shared version, `make clean` and `LINK_SO=1 make`
66

67
**Run tests**
gejun's avatar
gejun committed
68
```shell
69 70 71
$ cd test
$ make
$ sh run_tests.sh
gejun's avatar
gejun committed
72 73
```

74
### Compile brpc with cmake
Ge Jun's avatar
Ge Jun committed
75
```shell
76
mkdir bld && cd bld && cmake .. && make
77 78 79
```
To change compiler to clang, overwrite environment variable CC and CXX to clang and clang++.

80
To not link debugging symbols, use `rm -f CMakeCache.txt && cmake -DWITH_DEBUG_SYMBOLS=OFF ..` and compiled binaries will be much smaller.
81

Ge Jun's avatar
Ge Jun committed
82 83
To use brpc with glog, add `-DWITH_GLOG=ON`.

gejun's avatar
gejun committed
84
To enable [thrift support](../en/thrift.md), install thrift first and add `-DWITH_THRIFT=ON`.
Ge Jun's avatar
Ge Jun committed
85

86
**Run example with cmake**
Ge Jun's avatar
Ge Jun committed
87
```shell
88
$ cd example/echo_c++
89
$ mkdir bld && cd bld && cmake .. && make
90 91 92
$ ./echo_server &
$ ./echo_client
```
93
Examples link brpc statically, if you need to link the shared version, use `rm -f CMakeCache.txt && cmake -DLINK_SO=ON ..`
94 95

**Run tests**
Ge Jun's avatar
Ge Jun committed
96
```shell
97
$ mkdir bld && cd bld && cmake -DBUILD_UNIT_TESTS=ON .. && make && make test
98 99
```

100 101 102 103
## Fedora/CentOS

### Prepare deps

104
CentOS needs to install EPEL generally otherwise many packages are not available by default.
Ge Jun's avatar
Ge Jun committed
105
```shell
106
sudo yum install epel-release
107 108
```

109
Install common deps:
Ge Jun's avatar
Ge Jun committed
110
```shell
111
sudo yum install git gcc-c++ make openssl-devel
112
```
113

114
Install [gflags](https://github.com/gflags/gflags), [protobuf](https://github.com/google/protobuf), [leveldb](https://github.com/google/leveldb):
Ge Jun's avatar
Ge Jun committed
115
```shell
116
sudo yum install gflags-devel protobuf-devel protobuf-compiler leveldb-devel
117
```
Ge Jun's avatar
Ge Jun committed
118 119 120

If you need to enable cpu/heap profilers in examples:
```shell
121
sudo yum install gperftools-devel
122
```
Ge Jun's avatar
Ge Jun committed
123

124 125
If you need to run tests, install and compile gtest-devel (which is not compiled yet):
```shell
126
sudo yum install gtest-devel
127 128
```

129
### Compile brpc with config_brpc.sh
130

131
git clone brpc, cd into the repo and run
132

Ge Jun's avatar
Ge Jun committed
133
```shell
134 135 136
$ sh config_brpc.sh --headers=/usr/include --libs=/usr/lib64
$ make
```
137 138 139
To change compiler to clang, add `--cxx=clang++ --cc=clang`.

To not link debugging symbols, add `--nodebugsymbols` and compiled binaries will be much smaller.
140

Ge Jun's avatar
Ge Jun committed
141 142
To use brpc with glog, add `--with-glog`.

gejun's avatar
gejun committed
143
To enable [thrift support](../en/thrift.md), install thrift first and add `--with-thrift`.
Ge Jun's avatar
Ge Jun committed
144

145
**Run example**
146

Ge Jun's avatar
Ge Jun committed
147
```shell
148 149 150 151 152
$ cd example/echo_c++
$ make
$ ./echo_server &
$ ./echo_client
```
153

154 155
Examples link brpc statically, if you need to link the shared version, `make clean` and `LINK_SO=1 make`

156
**Run tests**
157 158 159 160 161
```shell
$ cd test
$ make
$ sh run_tests.sh
```
162 163

### Compile brpc with cmake
Ge Jun's avatar
Ge Jun committed
164
```shell
165
mkdir bld && cd bld && cmake .. && make
166 167 168
```
To change compiler to clang, overwrite environment variable CC and CXX to clang and clang++.

169
To not link debugging symbols, use `rm -f CMakeCache.txt && cmake -DWITH_DEBUG_SYMBOLS=OFF ..` and compiled binaries will be much smaller.
170

Ge Jun's avatar
Ge Jun committed
171 172
To use brpc with glog, add `-DWITH_GLOG=ON`.

gejun's avatar
gejun committed
173
To enable [thrift support](../en/thrift.md), install thrift first and add `-DWITH_THRIFT=ON`.
Ge Jun's avatar
Ge Jun committed
174

175 176
**Run example**

Ge Jun's avatar
Ge Jun committed
177
```shell
178
$ cd example/echo_c++
179
$ mkdir bld && cd bld && cmake .. && make
180 181 182
$ ./echo_server &
$ ./echo_client
```
183
Examples link brpc statically, if you need to link the shared version, use `rm -f CMakeCache.txt && cmake -DLINK_SO=ON ..`
184

185
**Run tests**
Ge Jun's avatar
Ge Jun committed
186
```shell
187
$ mkdir bld && cd bld && cmake -DBUILD_UNIT_TESTS=ON .. && make && make test
188 189
```

190 191 192 193 194 195
## Linux with self-built deps

### Prepare deps

brpc builds itself to both static and shared libs by default, so it needs static and shared libs of deps to be built as well.

196
Take [gflags](https://github.com/gflags/gflags) as example, which does not build shared lib by default, you need to pass options to `cmake` to change the behavior:
Ge Jun's avatar
Ge Jun committed
197 198 199
```shell
$ cmake . -DBUILD_SHARED_LIBS=1 -DBUILD_STATIC_LIBS=1
$ make
200
```
201 202 203

### Compile brpc

204
Keep on with the gflags example, let `../gflags_dev` be where gflags is cloned.
205 206 207

git clone brpc. cd into the repo and run

Ge Jun's avatar
Ge Jun committed
208
```shell
209 210 211 212
$ sh config_brpc.sh --headers="../gflags_dev /usr/include" --libs="../gflags_dev /usr/lib64"
$ make
```

Ge Jun's avatar
Ge Jun committed
213 214
Here we pass multiple paths to `--headers` and `--libs` to make the script search for multiple places. You can also group all deps and brpc into one directory, then pass the directory to --headers/--libs which actually search all subdirectories recursively and will find necessary files.

215 216 217
To change compiler to clang, add `--cxx=clang++ --cc=clang`.

To not link debugging symbols, add `--nodebugsymbols` and compiled binaries will be much smaller.
218

Ge Jun's avatar
Ge Jun committed
219
To use brpc with glog, add `--with-glog`.
220

gejun's avatar
gejun committed
221
To enable [thrift support](../en/thrift.md), install thrift first and add `--with-thrift`.
Ge Jun's avatar
Ge Jun committed
222 223

```shell
224 225 226 227 228 229
$ ls my_dev
gflags_dev protobuf_dev leveldb_dev brpc_dev
$ cd brpc_dev
$ sh config_brpc.sh --headers=.. --libs=..
$ make
```
230

231 232 233 234
### Compile brpc with cmake

git clone brpc. cd into the repo and run

Ge Jun's avatar
Ge Jun committed
235
```shell
236
mkdir bld && cd bld && cmake -DCMAKE_INCLUDE_PATH="/path/to/dep1/include;/path/to/dep2/include" -DCMAKE_LIBRARY_PATH="/path/to/dep1/lib;/path/to/dep2/lib" .. && make
237 238 239 240
```

To change compiler to clang, overwrite environment variable CC and CXX to clang and clang++.

241
To not link debugging symbols, use `rm -f CMakeCache.txt && cmake -DWITH_DEBUG_SYMBOLS=OFF ..` and compiled binaries will be much smaller.
242

Ge Jun's avatar
Ge Jun committed
243 244
To use brpc with glog, add `-DWITH_GLOG=ON`.

gejun's avatar
gejun committed
245
To enable [thrift support](../en/thrift.md), install thrift first and add `-DWITH_THRIFT=ON`.
Ge Jun's avatar
Ge Jun committed
246

zhujiashun's avatar
zhujiashun committed
247
## MacOS
248 249 250

Note: In the same running environment, the performance of the current Mac version is about 2.5 times worse than the Linux version. If your service is performance-critical, do not use MacOS as your production environment.

zhujiashun's avatar
zhujiashun committed
251 252 253
### Prepare deps

Install common deps:
Ge Jun's avatar
Ge Jun committed
254
```shell
255
brew install openssl git gnu-getopt coreutils
zhujiashun's avatar
zhujiashun committed
256 257 258
```

Install [gflags](https://github.com/gflags/gflags), [protobuf](https://github.com/google/protobuf), [leveldb](https://github.com/google/leveldb):
Ge Jun's avatar
Ge Jun committed
259
```shell
260
brew install gflags protobuf leveldb
zhujiashun's avatar
zhujiashun committed
261 262
```

Ge Jun's avatar
Ge Jun committed
263 264
If you need to enable cpu/heap profilers in examples:
```shell
265
brew install gperftools
266 267 268 269
```

If you need to run tests, install and compile googletest (which is not compiled yet):
```shell
270
git clone https://github.com/google/googletest && cd googletest/googletest && mkdir bld && cd bld && cmake -DCMAKE_CXX_FLAGS="-std=c++11" .. && make && sudo mv libgtest* /usr/lib/ && cd -
Ge Jun's avatar
Ge Jun committed
271 272
```

zhujiashun's avatar
zhujiashun committed
273 274
### Compile brpc with config_brpc.sh
git clone brpc, cd into the repo and run
Ge Jun's avatar
Ge Jun committed
275
```shell
zhujiashun's avatar
zhujiashun committed
276
$ sh config_brpc.sh --headers=/usr/local/include --libs=/usr/local/lib --cc=clang --cxx=clang++
zhujiashun's avatar
zhujiashun committed
277 278 279 280
$ make
```
To not link debugging symbols, add `--nodebugsymbols` and compiled binaries will be much smaller.

Ge Jun's avatar
Ge Jun committed
281 282
To use brpc with glog, add `--with-glog`.

gejun's avatar
gejun committed
283
To enable [thrift support](../en/thrift.md), install thrift first and add `--with-thrift`.
Ge Jun's avatar
Ge Jun committed
284

zhujiashun's avatar
zhujiashun committed
285 286
**Run example**

Ge Jun's avatar
Ge Jun committed
287
```shell
zhujiashun's avatar
zhujiashun committed
288 289 290 291 292 293 294 295 296 297
$ cd example/echo_c++
$ make
$ ./echo_server &
$ ./echo_client
```

Examples link brpc statically, if you need to link the shared version, `make clean` and `LINK_SO=1 make`

**Run tests**
```shell
298 299 300
$ cd test
$ make
$ sh run_tests.sh
zhujiashun's avatar
zhujiashun committed
301 302 303
```

### Compile brpc with cmake
Ge Jun's avatar
Ge Jun committed
304
```shell
305
mkdir bld && cd bld && cmake .. && make
zhujiashun's avatar
zhujiashun committed
306 307
```

308
To not link debugging symbols, use `rm -f CMakeCache.txt && cmake -DWITH_DEBUG_SYMBOLS=OFF ..` and compiled binaries will be much smaller.
zhujiashun's avatar
zhujiashun committed
309

Ge Jun's avatar
Ge Jun committed
310 311
To use brpc with glog, add `-DWITH_GLOG=ON`.

gejun's avatar
gejun committed
312
To enable [thrift support](../en/thrift.md), install thrift first and add `-DWITH_THRIFT=ON`.
Ge Jun's avatar
Ge Jun committed
313

zhujiashun's avatar
zhujiashun committed
314
**Run example with cmake**
Ge Jun's avatar
Ge Jun committed
315
```shell
zhujiashun's avatar
zhujiashun committed
316
$ cd example/echo_c++
317
$ mkdir bld && cd bld && cmake .. && make
zhujiashun's avatar
zhujiashun committed
318 319 320
$ ./echo_server &
$ ./echo_client
```
321
Examples link brpc statically, if you need to link the shared version, use `rm -f CMakeCache.txt && cmake -DLINK_SO=ON ..`
zhujiashun's avatar
zhujiashun committed
322 323

**Run tests**
Ge Jun's avatar
Ge Jun committed
324
```shell
325
$ mkdir bld && cd bld && cmake -DBUILD_UNIT_TESTS=ON .. && make && make test
zhujiashun's avatar
zhujiashun committed
326 327
```

328
# Supported deps
329

330
## GCC: 4.8-7.1
331

gejun's avatar
gejun committed
332
c++11 is turned on by default to remove dependencies on boost (atomic).
333

334
The over-aligned issues in GCC7 is suppressed temporarily now.
335

336
Using other versions of gcc may generate warnings, contact us to fix.
337

gejun's avatar
gejun committed
338
Adding `-D__const__=` to cxxflags in your makefiles is a must to avoid [errno issue in gcc4+](thread_local.md).
339

gejun's avatar
gejun committed
340
## Clang: 3.5-4.0
341

gejun's avatar
gejun committed
342
no known issues.
343

gejun's avatar
gejun committed
344
## glibc: 2.12-2.25
345

346
no known issues.
347

zhujiashun's avatar
zhujiashun committed
348
## protobuf: 2.4+
349

350
Be compatible with pb 3.x and pb 2.x with the same file:
351
Don't use new types in proto3 and start the proto file with `syntax="proto2";`
gejun's avatar
gejun committed
352
[tools/add_syntax_equal_proto2_to_all.sh](https://github.com/brpc/brpc/blob/master/tools/add_syntax_equal_proto2_to_all.sh)can add `syntax="proto2"` to all proto files without it.
gejun's avatar
gejun committed
353 354

Arena in pb 3.x is not supported yet.
355

356
## gflags: 2.0-2.2.1
357

358
no known issues.
359

gejun's avatar
gejun committed
360
## openssl: 0.97-1.1
361

362
required by https.
363

gejun's avatar
gejun committed
364
## tcmalloc: 1.7-2.5
365

gejun's avatar
gejun committed
366
brpc does **not** link [tcmalloc](http://goog-perftools.sourceforge.net/doc/tcmalloc.html) by default. Users link tcmalloc on-demand.
367

gejun's avatar
gejun committed
368
Comparing to ptmalloc embedded in glibc, tcmalloc often improves performance. However different versions of tcmalloc may behave really differently. For example, tcmalloc 2.1 may make multi-threaded examples in brpc perform significantly worse(due to a spinlock in tcmalloc) than the one using tcmalloc 1.7 and 2.5. Even different minor versions may differ. When you program behave unexpectedly, remove tcmalloc or try another version.
369

gejun's avatar
gejun committed
370
Code compiled with gcc 4.8.2 and linked to a tcmalloc compiled with earlier GCC may crash or deadlock before main(), E.g:
371

gejun's avatar
gejun committed
372
![img](../images/tcmalloc_stuck.png)
373

gejun's avatar
gejun committed
374
When you meet the issue, compile tcmalloc with the same GCC.
375

376
Another common issue with tcmalloc is that it does not return memory to system as early as ptmalloc. So when there's an invalid memory access, the program may not crash directly, instead it crashes at a unrelated place, or even not crash. When you program has weird memory issues, try removing tcmalloc.
377

gejun's avatar
gejun committed
378
If you want to use [cpu profiler](cpu_profiler.md) or [heap profiler](heap_profiler.md), do link `libtcmalloc_and_profiler.a`. These two profilers are based on tcmalloc.[contention profiler](contention_profiler.md) does not require tcmalloc.
379

gejun's avatar
gejun committed
380
When you remove tcmalloc, not only remove the linkage with tcmalloc but also the macro `-DBRPC_ENABLE_CPU_PROFILER`.
381

382 383
## glog: 3.3+

wangxuefeng's avatar
wangxuefeng committed
384
brpc implements a default [logging utility](../../src/butil/logging.h) which conflicts with glog. To replace this with glog, add *--with-glog* to config_brpc.sh or add `-DWITH_GLOG=ON` to cmake.
385

386
## valgrind: 3.8+
387

388
brpc detects valgrind automatically (and registers stacks of bthread). Older valgrind(say 3.2) is not supported.
389

Ge Jun's avatar
Ge Jun committed
390 391 392 393
## thrift: 0.9.3-0.11.0

no known issues.

gejun's avatar
gejun committed
394
# Track instances
395

gejun's avatar
gejun committed
396
We provide a program to help you to track and monitor all brpc instances. Just run [trackme_server](https://github.com/brpc/brpc/tree/master/tools/trackme_server/) somewhere and launch need-to-be-tracked instances with -trackme_server=SERVER. The trackme_server will receive pings from instances periodically and print logs when it does. You can aggregate instance addresses from the log and call builtin services of the instances for further information.