Skip to content

Commit

Permalink
nanopb upgrade 0.4.9. cluster script specified. weight. max_fails fai…
Browse files Browse the repository at this point in the history
…ls_timeout
  • Loading branch information
zhwaaaaaa committed Dec 14, 2024
1 parent db782d9 commit 4542817
Show file tree
Hide file tree
Showing 21 changed files with 2,668 additions and 1,511 deletions.
10 changes: 7 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ set(NGX_C_FILES nginx/src/core/nginx.c
nginx/src/os/unix/ngx_user.c
nginx/src/os/unix/ngx_dlopen.c
nginx/src/os/unix/ngx_process_cycle.c
nginx/src/os/unix/ngx_linux_init.c
nginx/src/event/modules/ngx_epoll_module.c
nginx/src/os/unix/ngx_linux_sendfile_chain.c
nginx/src/core/ngx_bpf.c
nginx/src/event/ngx_event_openssl.c
nginx/src/event/ngx_event_openssl_stapling.c
nginx/src/core/ngx_regex.c
Expand All @@ -85,6 +89,8 @@ set(NGX_C_FILES nginx/src/core/nginx.c
nginx/src/http/ngx_http_upstream.c
nginx/src/http/ngx_http_upstream_round_robin.c
nginx/src/http/ngx_http_file_cache.c
nginx/src/http/ngx_http_huff_decode.c
nginx/src/http/ngx_http_huff_encode.c
nginx/src/http/ngx_http_write_filter_module.c
nginx/src/http/ngx_http_header_filter_module.c
nginx/src/http/modules/ngx_http_chunked_filter_module.c
Expand All @@ -101,8 +107,6 @@ set(NGX_C_FILES nginx/src/core/nginx.c
nginx/src/http/v2/ngx_http_v2.c
nginx/src/http/v2/ngx_http_v2_table.c
nginx/src/http/v2/ngx_http_v2_encode.c
nginx/src/http/v2/ngx_http_v2_huff_decode.c
nginx/src/http/v2/ngx_http_v2_huff_encode.c
nginx/src/http/v2/ngx_http_v2_module.c
nginx/src/http/modules/ngx_http_static_module.c
nginx/src/http/modules/ngx_http_autoindex_module.c
Expand Down Expand Up @@ -133,7 +137,7 @@ set(NGX_C_FILES nginx/src/core/nginx.c
nginx/src/http/modules/ngx_http_upstream_random_module.c
nginx/src/http/modules/ngx_http_upstream_keepalive_module.c
nginx/src/http/modules/ngx_http_upstream_zone_module.c
)
)

set(NGX_INC_DIR nginx/src/core
nginx/src/event
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,23 @@ upstream backend {
# server 127.0.0.1:8080;
# 如果provider使用的spring,service_name 要和 spring.application.name一致
# 不知道 provider 端怎么写请参考 https://github.com/zhwaaaaaa/springmvc-nacos-registry
nacos_subscribe_service service_name=springmvc-nacos-demo group=DEFAULT_GROUP;
# weight * nacos_weight 是 nginx 的权重,默认1 max_fails=1 fail_timeout 对应 nginx 的server 配置
nacos_subscribe_service service_name=springmvc-nacos-demo group=DEFAULT_GROUP weight=1 max_fails=1 fail_timeout=10s;
}
```

### nacos_use_cluster
如果指定,则对应使用 cluster ip. 支持变量和字面量组合. 不指定,则使用所有集群 ip
```
set $cluster "DEFAULT";
upstream backend {
nacos_subscribe_service service_name=springmvc-nacos-demo group=DEFAULT_GROUP weight=1 max_fails=1 fail_timeout=10s;
nacos_use_cluster $cluster;
}
```


### nacos_config_var
订阅 nacos 的配置,nginx把它写到 http 变量中。这个配置项可以出现在 http server location if {} 块中。
```
Expand Down
7 changes: 4 additions & 3 deletions conf/my.conf
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ nacos {
#udp_bind 0.0.0.0:19999; # 绑定udp 地址
# username "nacos";
# password "nacos";
error_log cmake-build-debug/logs/nacos.log info;
error_log objs/logs/nacos.log info;
default_group DEFAULT_GROUP; # 默认的nacos group name
cache_dir cmake-build-debug/nacos/;
cache_dir objs/nacos/;
}
http {
include mime.types;
Expand All @@ -39,7 +39,8 @@ http {

#gzip on;
upstream s {
nacos_subscribe_service service_name=springmvc-nacos-demo; # data_id 要和 spring.application.name一致
nacos_subscribe_service service_name=springmvc-nacos-demo weight=100 max_fails=1 fail_timeout=10s; # service_name 要和 spring.application.name一致
nacos_use_cluster DEFAULT;
keepalive 300;
}
nacos_config_var $n_var data_id=ccccdddddd group=DEFAULT_GROUP md5_var=$dd default=123456;
Expand Down
10 changes: 10 additions & 0 deletions generate_proto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -e
mkdir -p objs
cd objs
curl -sSL https://jpa.kapsi.fi/nanopb/download/nanopb-0.4.9-linux-x86.tar.gz -o nanopb.tar.gz

tar zxvf nanopb.tar.gz
mv nanopb-0.4.9-linux-x86 nanopb

python3 nanopb/generator/nanopb_generator.py backup.proto nacos_grpc_service.proto -I ../modules/nacos
28 changes: 6 additions & 22 deletions modules/nacos/backup.pb.c
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
/* Automatically generated nanopb constant definitions */
/* Generated by nanopb-0.3.9.8 at Fri Aug 9 21:47:15 2024. */
/* Generated by nanopb-0.4.9 */

#include "backup.pb.h"

/* @@protoc_insertion_point(includes) */
#if PB_PROTO_HEADER_VERSION != 30
#if PB_PROTO_HEADER_VERSION != 40
#error Regenerate this file with the current version of nanopb generator.
#endif

PB_BIND(Config, Config, AUTO)


PB_BIND(Instance, Instance, AUTO)

const pb_field_t Config_fields[4] = {
PB_FIELD( 1, UINT64 , SINGULAR, STATIC , FIRST, Config, version, version, 0),
PB_FIELD( 2, STRING , SINGULAR, CALLBACK, OTHER, Config, content, version, 0),
PB_FIELD( 3, STRING , SINGULAR, CALLBACK, OTHER, Config, md5, content, 0),
PB_LAST_FIELD
};

const pb_field_t Instance_fields[4] = {
PB_FIELD( 1, STRING , SINGULAR, CALLBACK, FIRST, Instance, host, host, 0),
PB_FIELD( 2, INT32 , SINGULAR, STATIC , OTHER, Instance, port, host, 0),
PB_FIELD( 3, INT32 , SINGULAR, STATIC , OTHER, Instance, weight, port, 0),
PB_LAST_FIELD
};
PB_BIND(Service, Service, AUTO)

const pb_field_t Service_fields[3] = {
PB_FIELD( 1, UINT64 , SINGULAR, STATIC , FIRST, Service, version, version, 0),
PB_FIELD( 2, MESSAGE , REPEATED, CALLBACK, OTHER, Service, instances, version, &Instance_fields),
PB_LAST_FIELD
};


/* @@protoc_insertion_point(eof) */
65 changes: 40 additions & 25 deletions modules/nacos/backup.pb.h
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
/* Automatically generated nanopb header */
/* Generated by nanopb-0.3.9.8 at Fri Aug 9 21:47:15 2024. */
/* Generated by nanopb-0.4.9 */

#ifndef PB_BACKUP_PB_H_INCLUDED
#define PB_BACKUP_PB_H_INCLUDED
#include <pb/pb.h>

/* @@protoc_insertion_point(includes) */
#if PB_PROTO_HEADER_VERSION != 30
#if PB_PROTO_HEADER_VERSION != 40
#error Regenerate this file with the current version of nanopb generator.
#endif

#ifdef __cplusplus
extern "C" {
#endif

/* Struct definitions */
typedef struct _Config {
uint64_t version;
pb_callback_t content;
pb_callback_t md5;
/* @@protoc_insertion_point(struct:Config) */
} Config;

typedef struct _Instance {
pb_callback_t host;
int32_t port;
int32_t weight;
/* @@protoc_insertion_point(struct:Instance) */
pb_callback_t cluster;
} Instance;

typedef struct _Service {
uint64_t version;
pb_callback_t instances;
/* @@protoc_insertion_point(struct:Service) */
} Service;

/* Default values for struct fields */

#ifdef __cplusplus
extern "C" {
#endif

/* Initializer values for message structs */
#define Config_init_default {0, {{NULL}, NULL}, {{NULL}, NULL}}
#define Instance_init_default {{{NULL}, NULL}, 0, 0}
#define Instance_init_default {{{NULL}, NULL}, 0, 0, {{NULL}, NULL}}
#define Service_init_default {0, {{NULL}, NULL}}
#define Config_init_zero {0, {{NULL}, NULL}, {{NULL}, NULL}}
#define Instance_init_zero {{{NULL}, NULL}, 0, 0}
#define Instance_init_zero {{{NULL}, NULL}, 0, 0, {{NULL}, NULL}}
#define Service_init_zero {0, {{NULL}, NULL}}

/* Field tags (for use in manual encoding/decoding) */
Expand All @@ -52,30 +48,49 @@ typedef struct _Service {
#define Instance_host_tag 1
#define Instance_port_tag 2
#define Instance_weight_tag 3
#define Instance_cluster_tag 4
#define Service_version_tag 1
#define Service_instances_tag 2

/* Struct field encoding specification for nanopb */
extern const pb_field_t Config_fields[4];
extern const pb_field_t Instance_fields[4];
extern const pb_field_t Service_fields[3];
#define Config_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT64, version, 1) \
X(a, CALLBACK, SINGULAR, STRING, content, 2) \
X(a, CALLBACK, SINGULAR, STRING, md5, 3)
#define Config_CALLBACK pb_default_field_callback
#define Config_DEFAULT NULL

#define Instance_FIELDLIST(X, a) \
X(a, CALLBACK, SINGULAR, STRING, host, 1) \
X(a, STATIC, SINGULAR, INT32, port, 2) \
X(a, STATIC, SINGULAR, INT32, weight, 3) \
X(a, CALLBACK, SINGULAR, STRING, cluster, 4)
#define Instance_CALLBACK pb_default_field_callback
#define Instance_DEFAULT NULL

#define Service_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT64, version, 1) \
X(a, CALLBACK, REPEATED, MESSAGE, instances, 2)
#define Service_CALLBACK pb_default_field_callback
#define Service_DEFAULT NULL
#define Service_instances_MSGTYPE Instance

extern const pb_msgdesc_t Config_msg;
extern const pb_msgdesc_t Instance_msg;
extern const pb_msgdesc_t Service_msg;

/* Defines for backwards compatibility with code written before nanopb-0.4.0 */
#define Config_fields &Config_msg
#define Instance_fields &Instance_msg
#define Service_fields &Service_msg

/* Maximum encoded size of messages (where known) */
/* Config_size depends on runtime parameters */
/* Instance_size depends on runtime parameters */
/* Service_size depends on runtime parameters */

/* Message IDs (where set with "msgid" option) */
#ifdef PB_MSGID

#define BACKUP_MESSAGES \


#endif

#ifdef __cplusplus
} /* extern "C" */
#endif
/* @@protoc_insertion_point(eof) */

#endif
1 change: 1 addition & 0 deletions modules/nacos/backup.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ message Instance {
string host = 1;
int32 port = 2;
int32 weight = 3;
string cluster = 4;
}

message Service {
Expand Down
64 changes: 6 additions & 58 deletions modules/nacos/nacos_grpc_service.pb.c
Original file line number Diff line number Diff line change
@@ -1,73 +1,21 @@
/* Automatically generated nanopb constant definitions */
/* Generated by nanopb-0.3.9.10 at Mon Jun 3 14:49:02 2024. */
/* Generated by nanopb-0.4.9 */

#include "nacos_grpc_service.pb.h"

/* @@protoc_insertion_point(includes) */
#if PB_PROTO_HEADER_VERSION != 30
#if PB_PROTO_HEADER_VERSION != 40
#error Regenerate this file with the current version of nanopb generator.
#endif

/**
* #define PB_FIELD(tag, type, rules, allocation, placement, message, field, prevfield, ptr) \
PB_ ## rules ## _ ## allocation(tag, message, field, \
PB_DATAOFFSET_ ## placement(message, field, prevfield), \
PB_LTYPE_MAP_ ## type, ptr)
*/


const pb_field_t google_protobuf_Any_fields[3] = {
PB_FIELD( 1, STRING , SINGULAR, CALLBACK, FIRST, google_protobuf_Any, type_url, type_url, 0),
PB_FIELD( 2, BYTES , SINGULAR, CALLBACK, OTHER, google_protobuf_Any, value, type_url, 0),
PB_LAST_FIELD
};
PB_BIND(Any, Any, AUTO)


const pb_field_t Metadata_HeadersEntry_fields[3] = {
PB_FIELD( 1, STRING , SINGULAR, CALLBACK, FIRST, Metadata_HeadersEntry, key, key, 0),
PB_FIELD( 2, STRING , SINGULAR, CALLBACK, OTHER, Metadata_HeadersEntry, value, key, 0),
PB_LAST_FIELD
};
PB_BIND(Metadata, Metadata, AUTO)

const pb_field_t Metadata_fields[4] = {
PB_FIELD( 3, STRING , SINGULAR, CALLBACK, FIRST, Metadata, type, type, 0),
// PB_SINGULAR_CALLBACK(3, Metadata, type, PB_DATAOFFSET_FIRST(Metadata, type, type), PB_LTYPE_MAP_STRING, 0),
PB_FIELD( 7, MESSAGE , REPEATED, CALLBACK, OTHER, Metadata, headers, type, &Metadata_HeadersEntry_fields),
PB_FIELD( 8, STRING , SINGULAR, CALLBACK, OTHER, Metadata, clientIp, headers, 0),
PB_LAST_FIELD
};

PB_BIND(Metadata_HeadersEntry, Metadata_HeadersEntry, AUTO)

const pb_field_t Payload_fields[3] = {
PB_FIELD( 2, MESSAGE , SINGULAR, STATIC , FIRST, Payload, metadata, metadata, &Metadata_fields),
// PB_SINGULAR_STATIC(2, Payload,metadata, PB_DATAOFFSET_FIRST(Payload, metadata, metadata), PB_LTYPE_MAP_MESSAGE, &Metadata_fields),
PB_FIELD( 3, MESSAGE , SINGULAR, STATIC , OTHER, Payload, body, metadata, &google_protobuf_Any_fields),
PB_LAST_FIELD
};

PB_BIND(Payload, Payload, AUTO)

/* Check that field information fits in pb_field_t */
#if !defined(PB_FIELD_32BIT)
/* If you get an error here, it means that you need to define PB_FIELD_32BIT
* compile-time option. You can do that in pb.h or on compiler command line.
*
* The reason you need to do this is that some of your messages contain tag
* numbers or field sizes that are larger than what can fit in 8 or 16 bit
* field descriptors.
*/
PB_STATIC_ASSERT((pb_membersize(Payload, metadata) < 65536 && pb_membersize(Payload, body) < 65536), YOU_MUST_DEFINE_PB_FIELD_32BIT_FOR_MESSAGES_Metadata_Metadata_HeadersEntry_Payload)
#endif

#if !defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)
/* If you get an error here, it means that you need to define PB_FIELD_16BIT
* compile-time option. You can do that in pb.h or on compiler command line.
*
* The reason you need to do this is that some of your messages contain tag
* numbers or field sizes that are larger than what can fit in the default
* 8 bit descriptors.
*/
PB_STATIC_ASSERT((pb_membersize(Payload, metadata) < 256 && pb_membersize(Payload, body) < 256), YOU_MUST_DEFINE_PB_FIELD_16BIT_FOR_MESSAGES_Metadata_Metadata_HeadersEntry_Payload)
#endif


/* @@protoc_insertion_point(eof) */
Loading

0 comments on commit 4542817

Please sign in to comment.