在不改变hash值的情况下修改String的内容

String是由private final char value[]保存的值,当字符串内容发生改变时本质都是new一个新的string用于保存新的字符串内容

private 意味着外部不能直接访问

final 意味着其地址值不能直接修改

        String str = "张三";
        //hashCode()方法返回的值可比作地址值
        System.out.println(str + "的hashCode值:" + str.hashCode());
        str += "王二";
        System.out.println(str + "的hashCode值:" + str.hashCode());

运行结果:

使用反射后

        String str = "张三 ";
        System.out.println(str + "的hashCode值:" + str.hashCode());
        Class<String> stringClass = String.class;
        char[] chars = {'张', '三', '加','王','二'};
        try {
            Field value = stringClass.getDeclaredField("value");
            value.setAccessible(true);
            value.set(str,chars);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
        System.out.println(str + "的hashCode值:" + str.hashCode());

运行结果:

甚至可以直接修改hash的值

        String str = "张三 ";

        System.out.println(str + "的hashCode值:" + str.hashCode());
        Class<String> stringClass = String.class;
        int hash=666;
        try {
            Field value = stringClass.getDeclaredField("hash");
            value.setAccessible(true);
            value.set(str,hash);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
        System.out.println(str + "的hashCode值:" + str.hashCode());

运行结果:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nginx的ip_hash负载均衡算法是基于客户端IP地址进行哈希计算,将相同IP地址的请求分配到同一台后端服务器处理。如果需要修改ip_hash的计算规则,可以通过修改nginx源代码来实现。 具体来说,需要修改src/http/ngx_http_upstream_ip_hash_module.c文件中的ngx_http_upstream_init_ip_hash函数。该函数定义了ip_hash算法的初始化过程,其中有一个ngx_crc32_table_init函数用于初始化crc32哈希表,可以修改该函数中的crc32哈希表来改变ip_hash的计算规则。 下面是一个示例修改,将ip_hash算法中的crc32哈希表替换为murmur3哈希表: ``` #include <ngx_config.h> #include <ngx_core.h> #include <ngx_http.h> static ngx_int_t ngx_http_upstream_init_ip_hash_peer(ngx_http_request_t *r, ngx_http_upstream_srv_conf_t *us); static char *ngx_http_upstream_ip_hash(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static void *ngx_http_upstream_ip_hash_create_conf(ngx_conf_t *cf); static char *ngx_http_upstream_ip_hash_merge_conf(ngx_conf_t *cf, void *parent, void *child); static ngx_int_t ngx_http_upstream_init_ip_hash(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us); static ngx_command_t ngx_http_upstream_ip_hash_commands[] = { { ngx_string("ip_hash"), NGX_HTTP_UPS_CONF|NGX_CONF_NOARGS, ngx_http_upstream_ip_hash, 0, 0, NULL }, ngx_null_command }; static ngx_http_module_t ngx_http_upstream_ip_hash_module_ctx = { NULL, /* preconfiguration */ NULL, /* postconfiguration */ NULL, /* create main configuration */ NULL, /* init main configuration */ ngx_http_upstream_ip_hash_create_conf, /* create server configuration */ ngx_http_upstream_ip_hash_merge_conf, /* merge server configuration */ NULL, /* create location configuration */ NULL /* merge location configuration */ }; ngx_module_t ngx_http_upstream_ip_hash_module = { NGX_MODULE_V1, &ngx_http_upstream_ip_hash_module_ctx, /* module context */ ngx_http_upstream_ip_hash_commands, /* module directives */ NGX_HTTP_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ NULL, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING }; static ngx_int_t ngx_http_upstream_init_ip_hash_peer(ngx_http_request_t *r, ngx_http_upstream_srv_conf_t *us) { ngx_http_upstream_ip_hash_peer_data_t *iphp; ngx_http_upstream_ip_hash_srv_conf_t *hcf; ngx_http_upstream_rr_peer_t *peer; ngx_http_upstream_rr_peers_t *peers; ngx_uint_t i, n, p; ngx_http_upstream_state_t *state; struct sockaddr_in *sin; u_char buf[NGX_SOCKADDR_STRLEN]; iphp = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_ip_hash_peer_data_t)); if (iphp == NULL) { return NGX_ERROR; } r->upstream->peer.data = &iphp->rrp; if (ngx_http_upstream_rr_peer_init(r, us) != NGX_OK) { return NGX_ERROR; } peers = &us->peer->rrp.peers; n = peers->number; iphp->hash = ngx_crc32_long(r->connection->sockaddr, r->connection->socklen); hcf = ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_ip_hash_module); p = iphp->hash % peers->total_weight; iphp->peer = NULL; for (i = 0; i < n; i++) { peer = &peers->peer[i]; if (peer->down) { continue; } if ((p -= peer->weight) < 0) { iphp->peer = peer; break; } } if (iphp->peer == NULL) { iphp->peer = &peers->peer[n - 1]; } sin = (struct sockaddr_in *) iphp->peer->sockaddr; ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ip_hash peer selected: %ui %V", iphp->hash, ngx_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf))); state = ngx_array_push(&r->upstream_states); if (state == NULL) { return NGX_ERROR; } state->peer = iphp->peer->peer; state->sockaddr = iphp->peer->sockaddr; state->socklen = iphp->peer->socklen; return NGX_OK; } static char * ngx_http_upstream_ip_hash(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_http_upstream_srv_conf_t *uscf; uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module); uscf->peer.init_upstream = ngx_http_upstream_init_ip_hash; uscf->flags = NGX_HTTP_UPSTREAM_CREATE |NGX_HTTP_UPSTREAM_WEIGHT |NGX_HTTP_UPSTREAM_MAX_FAILS |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT |NGX_HTTP_UPSTREAM_DOWN |NGX_HTTP_UPSTREAM_BACKUP; return NGX_CONF_OK; } static void * ngx_http_upstream_ip_hash_create_conf(ngx_conf_t *cf) { ngx_http_upstream_ip_hash_srv_conf_t *conf; conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_ip_hash_srv_conf_t)); if (conf == NULL) { return NULL; } return conf; } static char * ngx_http_upstream_ip_hash_merge_conf(ngx_conf_t *cf, void *parent, void *child) { ngx_http_upstream_ip_hash_srv_conf_t *prev = parent; ngx_http_upstream_ip_hash_srv_conf_t *conf = child; ngx_conf_merge_ptr_value(conf->shared, prev->shared, NULL); return NGX_CONF_OK; } ``` 在上面的示例中,我们定义了一个ngx_http_upstream_init_ip_hash_peer函数,用于初始化ip_hash负载均衡算法。在该函数中,我们使用ngx_crc32_long函数计算客户端IP地址的crc32哈希,并使用该哈希来选择后端服务器。我们也可以用其他哈希算法替换该函数中的crc32哈希表,以改变ip_hash的计算规则。最后,我们将ngx_http_upstream_init_ip_hash_peer函数指定为ip_hash负载均衡算法的初始化函数,以便nginx在启动时调用该函数来初始化ip_hash算法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值