温馨提示:表格最后一栏是示例,也可以直接复制配置示例中的yml内容到本地查看。
配置详解
- 以配置RESTful接口为例
- 服务注册中心选用nacos
对于其他类型的通信协议,除了需要改协议类型相关配置,其余绝大多数配置是通用的:
配置key | 解释 | 数据类型 | 示例 | |
---|---|---|---|---|
application | organization | 组织名 | string | hello-world.com |
name | 服务/应用名称 | string | hello-world | |
module | 模块名 | string | hello-world server | |
version | 版本号 | string | 0.0.1 | |
owner | 所属者 | string | hello-world | |
environment | 部署环境(release:生成环境;dev:开发;test:测试环境) | string | dev | |
registries | 无key,默认第一行 | 服务注册中心名称 | string | nacos-hello-world |
protocol | 注册中心类型,支持:nacos,etcdv3,k8s(需要单独写配置)等 | string | nacos | |
timeout | 服务超时时间 | string | 3s | |
address | 服务中心启动后暴露的地址 | string | 127.0.0.1:8848 | |
username | 用户名 | string | - | |
password | 密码 | string | - | |
services | 无key,默认第一行 | 服务provider名 | string | HelloWorldProvider |
registry | 注册中心名称,要与registries配置的注册中心名称一致; 可以指定多个registry,使用逗号隔开; 不指定默认向所有注册中心注册 |
string | nacos-hello-world | |
protocol | 服务协议类型,支持dubbo,rest,grpc,jsonrpc | string | rest | |
interface | 具体发布接口服务名称,相当于dubbo.xml中的interface 格式推荐按照示例格式定义,便于提供给Java侧 |
string | com.custom.package.HelloProvider | |
loadbalance | 负载均衡策略 | string | random(随机负载均衡) | |
warmup | 限流—预热/冷启动阈值; 是为了防止长期处于低水平的系统, 突然来了大流量,系统突然拉升至高水平导致系统奔溃。 |
string | 100 | |
cluster | 容错机制—失效转移,支持:failover;failfast;failback;failsafe | string | failover | |
rest_path | URL路径(针对REST),父PATH | string | hello | |
methods | 具体方法定义,顾名思义,是一组值,会单独解释 | string | - | |
methods | name | 方法名 | string | GetHelloWorld |
rest_path | URL路径(针对REST),子PATH | string | /get | |
rest_method | HTTP方法类型 | string | GET | |
rest_query_params | Query参数,访问时类似”/hello/get?msg=world” | string | 0:msg | |
rest_path_params | Path 参数,如rest_path设置为:/hello/{who} | string | 0:who | |
rest_body | 针对请求参数是对象(go结构体)类型, 其值取决于参数的位置下标 |
int | 0 | |
rest_produces | 响应Content-Type | string | application/json | |
rest_consumes | 请求Content-Type | string | application/json;charset=utf-8,/ | |
protocols | 无key,默认第一行 | 协议类型,REST协议,支持:rest,dubbo,jsonrpc,grpc | string | rest |
name | 协议名,REST协议,支持:rest,dubbo,jsonrpc,grpc | string | rest | |
ip | IP地址 | string | 127.0.0.1 | |
port | 端口号 | int | 8080 |
配置示例
application:
organization : "hello-world.com"
name : "hello-world"
module : "hello-world serverr"
version : "0.0.1"
owner : "hello-world"
environment : "dev"
registries :
"nacos-hello-world":
protocol: "nacos"
timeout : "3s"
address: "127.0.0.1:8848"
services:
"HelloWorldProvider":
registry: "nacos-hello-world"
protocol : "rest"
interface : "com.custom.package.HelloProvider"
rest_path: "/hello"
methods:
- name: "SayHi"
rest_path: "/get"
rest_method: "GET"
rest_query_params: "0:msg" # eg: /hello/get?msg=world"
rest_produces: "application/json"
- name: "Who"
rest_path: "/post"
rest_method: "POST"
rest_body: 0 # 请求参数是对象(go结构体)类型。不要误以为是对响应体的设置
rest_produces: "application/json"
rest_consumes: "application/json;charset=utf-8,*/*"
protocols:
"rest":
name: "rest"
ip: "127.0.0.1"
port: 8081