Commit a5faf716 by 钟明宏

增加了环信的接口

parent 5349caab
......@@ -94,6 +94,11 @@
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.61</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.9</version>
......
......@@ -67,7 +67,7 @@ public class Test2 {
}
int i = 0;
for (long key : treeMap.keySet()) {
System.out.println(++i + ":"+key+" :"+treeMap.get(key));
System.out.println(++i + ":" + key + " :" + treeMap.get(key));
}
}
......@@ -94,10 +94,85 @@ public class Test2 {
}
@Test
public void demo2(){
for(int i=0;i<100;i++){
public void demo2() {
for (int i = 0; i < 100; i++) {
int random = ThreadLocalRandom.current().nextInt(10);
System.out.println(random);
}
}
/**
* 测试一下dubbo 的随机负载均衡算法
*/
@Test
public void RandomLoadBalance_test() {
List<Invoker> invokers = new ArrayList<>();
for (int i = 0; i < 10; i++) {
invokers.add(new Invoker(ThreadLocalRandom.current().nextInt(100), "invoker-" + i));
}
System.out.println("打印 invoker");
for(Invoker invoker : invokers){
System.out.println(invoker);
}
System.out.println("打印 invoker-----------------end");
int length = invokers.size();
// 假设每个调用器的权重一样
boolean sameWeight = true;
// the weight of every invokers
int[] weights = new int[length];
// 获取第一个invoker的权重
int firstWeight = invokers.get(0).weight;
//设置第一个invoker的权重到数组中
weights[0] = firstWeight;
// 设置总权重的值
int totalWeight = firstWeight;
for (int i = 1; i < length; i++) {
int weight = invokers.get(i).weight;
// save for later use
weights[i] = weight;
// Sum
totalWeight += weight;
//如果存在和第一个权重一样的值 就把 sameWeight 设置为fasle
if (sameWeight && weight != firstWeight) {
sameWeight = false;
}
}
//没有重复权重的
if (totalWeight > 0 && !sameWeight) {
// If (not every invoker has the same weight & at least one invoker's weight>0), select randomly based on totalWeight.
int offset = ThreadLocalRandom.current().nextInt(totalWeight);
// Return a invoker based on the random value.
System.out.println("获取的数据阻断值是:"+offset+" 总权重值:"+totalWeight);
for (int i = 0; i < length; i++) {
offset -= weights[i];
if (offset < 0) {
Invoker invoker = invokers.get(i);
System.out.println("当前选择的invoker是"+invoker);
return;
}
}
}
//当所有权重值一样的时候
Invoker invoker = invokers.get(ThreadLocalRandom.current().nextInt(length));
System.out.println("当前选择的invoker是"+invoker);
}
private class Invoker {
private int weight;
private String name;
public Invoker(int weight, String name) {
this.weight = weight;
this.name = name;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Invoker{");
sb.append("weight=").append(weight);
sb.append(", name='").append(name).append('\'');
sb.append('}');
return sb.toString();
}
}
}
测试一下环信即时通讯的调用
\ No newline at end of file
package com.yunmaozj.huanxin;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import org.junit.Test;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* 服务端集成demo
*
* @author zhongminghong
* @email zhongmh@yunmaozj.com
* @create 2020-04-14 9:39
*/
public class ServerDemo {
private OkHttpClient client = new OkHttpClient.Builder().build();
private String appKey = "1104200414107015#zhongmh-test";
private String organName = "1104200414107015";
private String appName = "zhongmh-test";
private String clientId = "YXA6lbB1FqEHSN-QTyWtM1fE5A";
private String clientSecret = "YXA6U9XBzxdLAXu7ouVXXHf3gbIdyr8";
private final HttpUrl BASE_URL = new HttpUrl.Builder()
.host("a1.easemob.com")
.scheme("http")
.addPathSegment(organName)
.addPathSegment(appName).build();
/**
* 获取token
*/
@Test
public void getAccessToken() throws IOException {
HttpUrl url = getHttpUrl().addPathSegment("token").build();
Map<String, String> map = new HashMap<>();
map.put("grant_type", "client_credentials");
map.put("client_id", clientId);
map.put("client_secret", clientSecret);
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
JSONObject.toJSONString(map));
Request request = new Request.Builder().url(url)
.post(body).build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
}
/**
* 创建账户
*/
@Test
public void createUser() throws IOException {
HttpUrl url = getHttpUrl().addPathSegment("users").build();
Map<String, String> map = new HashMap<>();
map.put("username","1");
map.put("password","123456");
map.put("nickname","this is 昵称");
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
JSONObject.toJSONString(map));
Request request = new Request.Builder().url(url)
.post(body).headers(getHeader().build()).build();
Response response = client.newCall(request)
.execute();
System.out.println(response.body().string());
}
/**
* 获取账户列表
*/
@Test
public void userList() throws IOException {
HttpUrl url = getHttpUrl().addPathSegment("users").addQueryParameter("limit","5").build();
Request request = new Request.Builder().url(url).headers(getHeader().build()).build();
Response response = client.newCall(request)
.execute();
System.out.println(response.body().string());
}
/**
* 查看用户在线状态
*/
@Test
public void status() throws IOException {
//
HttpUrl url = getHttpUrl()
.addPathSegment("users").addPathSegment("1").addPathSegment("status")
.build();
Request request = new Request.Builder().url(url)
.get().headers(getHeader().build()).build();
Response response = client.newCall(request)
.execute();
System.out.println(response.body().string());
}
/**
* 获取聊天消息
*/
@Test
public void chatmessages() throws IOException {
HttpUrl url = getHttpUrl().addPathSegment("chatmessages").addEncodedQueryParameter("ql","select+*").build();
System.out.println("即将访问的url:"+url);
Request request = new Request.Builder().url(url)
.headers(getHeader().build()).build();
Response response = client.newCall(request)
.execute();
System.out.println(response.body().string());
}
/**
* 测试环信消息发送
*/
@Test
public void sendMsg() throws IOException {
String msg = "{\n" +
" \"target_type\": \"users\",\n" +
" \"target\": [\n" +
" \"1\"\n" +
" ],\n" +
" \"msg\": {\n" +
" \"type\": \"txt\",\n" +
" \"msg\": \"string\"\n" +
" },\n" +
" \"from\": \"李四\",\n" +
" \"ext\": \"{\"name\":\"张三\"}\"\n" +
"}";
RequestBody body = RequestBody.create(MediaType.parse("application/json"), msg);
HttpUrl url = getHttpUrl().addPathSegment("messages").build();
Request request = new Request.Builder().url(url).post(body)
.headers(getHeader().build()).build();
Response response = client.newCall(request)
.execute();
System.out.println(response.body().string());
}
private HttpUrl.Builder getHttpUrl() {
return BASE_URL.newBuilder();
}
private Headers.Builder getHeader() throws IOException {
HttpUrl url = getHttpUrl().addPathSegment("token").build();
Map<String, String> map = new HashMap<>();
map.put("grant_type", "client_credentials");
map.put("client_id", clientId);
map.put("client_secret", clientSecret);
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
JSONObject.toJSONString(map));
Request request = new Request.Builder().url(url)
.post(body).build();
Response response = client.newCall(request).execute();
String responseBody = response.body().string();
return new Headers.Builder().add("Authorization", "Bearer " + JSONObject.parseObject(responseBody).getString("access_token"));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment