Skip to content

Commit

Permalink
Merge 5d567b1 into 973271a
Browse files Browse the repository at this point in the history
  • Loading branch information
glmapper committed Apr 12, 2019
2 parents 973271a + 5d567b1 commit 261cc80
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 0 deletions.
1 change: 1 addition & 0 deletions tracer-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<module>tracer-sample-with-resttemplate</module>
<module>tracer-sample-with-sampler</module>
<module>tracer-sample-with-dubbo</module>
<module>tracer-sample-with-okhttp</module>
</modules>

<properties>
Expand Down
33 changes: 33 additions & 0 deletions tracer-samples/tracer-sample-with-okhttp/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>sofa-tracer-samples</artifactId>
<groupId>com.alipay.sofa</groupId>
<version>2.4.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>tracer-sample-with-okhttp</artifactId>
<properties>
<main.user.dir>${project.basedir}/../..</main.user.dir>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>tracer-sofa-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.12.1</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.tracer.examples.okhttp;

import com.alipay.sofa.tracer.examples.okhttp.instance.OkHttpClientInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* HttpClientDemoApplication
*
* @author yangguanchao
* @since 2018/09/27
*/
@SpringBootApplication
public class OkHttpDemoApplication {

private static Logger logger = LoggerFactory.getLogger(OkHttpDemoApplication.class);

public static void main(String[] args) throws Exception {
SpringApplication.run(OkHttpDemoApplication.class, args);
OkHttpClientInstance httpClient = new OkHttpClientInstance();
String httpGetUrl = "http://localhost:8081/okhttp?name=sofa";
String responseStr = httpClient.executeGet(httpGetUrl);
logger.info("Response is {}", responseStr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.tracer.examples.okhttp.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
* SampleRestController
* @author guolei.sgl
*/
@RestController
public class SampleRestController {

private final AtomicLong counter = new AtomicLong(0);

/**
* Request http://localhost:8081/okhttp?name=
* @param name name
* @return Map of Result
*/
@RequestMapping("/okhttp")
public Map<String, Object> greeting(@RequestParam(value = "name", defaultValue = "okhttp") String name) {
Map<String, Object> map = new HashMap<>();
map.put("count", counter.incrementAndGet());
map.put("name", name);
return map;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.tracer.examples.okhttp.instance;

import com.alipay.common.tracer.core.utils.StringUtils;
import com.alipay.sofa.tracer.plugins.okhttp.SofaTracerOkHttpBuilder;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
* @author: guolei.sgl (guolei.sgl@antfin.com) 2019/4/12 1:29 PM
* @since:
**/
public class OkHttpClientInstance {

private OkHttpClient okHttpClient;

public OkHttpClientInstance() {
this.okHttpClient = getOkHttpClient();
}

private OkHttpClient getOkHttpClient() {
if (okHttpClient != null) {
return okHttpClient;
} else {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
return SofaTracerOkHttpBuilder.clientBuilder(builder).build();
}
}

public String executeGet(String url) throws Exception {
if (StringUtils.isBlank(url)) {
return null;
}
if (!url.startsWith("http://")) {
url = "http://" + url;
}
Request request = new Request.Builder().url(url).build();
return okHttpClient.newCall(request).execute().body().string();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Application Name
spring.application.name=OkHttpDemo
# logging path
logging.path=./logs
server.port=8081

0 comments on commit 261cc80

Please sign in to comment.