# 业务服务一主多备机制

服务部署在框架有两种模式, 一种普通模式(bak\_type=0), 一种主备模式(bak\_type=1), 服务在这两种模式下都有备机状态(bak\_flag=1), 同一个服务, 不同节点上的服务的bak\_type必须相同, 要么都为1, 要么都为0

**修改bak\_type, 服务必须重启才能生效!!**

* 普通模式

> - 服务如果设置为备机状态, 无论服务是否启动, 则ip list中都不对外提供ip
> - 发布服务时, 如果是备机, 则不启动服务

* 主备模式

> - 主备模式主要为了支持一主多备的模式, 且备机也正常启动, 但是ip list中不对外提供ip
> - 业务服务会定时(每隔2秒)抢主, 如果抢到主机则回调业务(onMaster), 否则变成备机回调业务(onSlave)
> - 扩容服务时, 扩容出来的节点必须是备机
> - 主备服务是业务服务自己抢占的, 因此这种模式下, 无法在页面上编辑主备状态
> - 提供独立的接口, 切换主机, 即强制设置某服务为主机
> - 如果bak\_type从0设置1, 则将所有服务的bak\_flag都设置为0


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tarscloud.gitbook.io/tarsdocs/qi-ye-ban-ben-jie-shao/tars-master-slave.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
