分片工具类
上传
*maven
非必须的*
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>{Dubbo版本号}</version>
</dependency>
在服务消费者端的代码中获取Zookeeper注册中心上的全部可用服务
// 创建Zookeeper注册中心实例
RegistryService registryService = ExtensionLoader.getExtensionLoader(RegistryFactory.class)
.getExtension("zookeeper")
.getRegistry(new URL("zookeeper", "localhost", 2181));
// 获取可用的服务列表
List<URL> urls = registryService.lookup(new URL("dubbo", "localhost", 0, "org.example.UserService"));
for (URL url : urls) {
System.out.println(url.getHost() + ":" + url.getPort());
}
以上示例代码中,通过RegistryFactory的getExtension方法获取到ZookeeperRegistryFactory实例,然后通过其getRegistry方法构造一个Zookeeper注册中心实例。然后,通过调用RegistryService的lookup方法获取到全部可用服务的URL列表。
```java
package com.jinw;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
如果我们把请求参数如果放在HttpPost或者HttpGet中后端该如何取参?
有两种方式
一种是从请求数据流中获取数据
@AnonymousPostMapping("/api/router")
public RestResult router(HttpServletRequest requestEntity) throws Exception {
String inputStream = getInputStream(requestEntity);
// Object requestEntityBody = requestEntity.getBody();
System.out.println(123);
return RestResult.success();
}
<!--more-->
public static String getInputStream(HttpServletRequest request) throws Exception {
ServletInputStream stream = null;
BufferedReader reader = null;
StringBuffer sb = new StringBuffer();
try {
stream = request.getInputStream();
// 获取响应
reader = new BufferedReader(new InputStreamReader(stream));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
// logger.error(e);
} finally {
reader.close();
}
return sb.toString();
}
方式二:
如果用的是springboot框架
可以用自带的 RequestEntity 作为实体进行接收
包是:package org.springframework.http;
@AnonymousPostMapping("/api/router")
public RestResult router(RequestEntity requestEntity) throws IOException {
Object requestEntityBody = requestEntity.getBody();
System.out.println(123);
return RestResult.success();
}