Skip to content

Commit

Permalink
add codes about http client metric
Browse files Browse the repository at this point in the history
  • Loading branch information
ctlove0523 committed Jun 7, 2021
1 parent c0e4632 commit 2212771
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 75 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,12 @@
@RestController
public class AsyncController {

@RequestMapping(value = "/v5/iot/applications/{app_id}", method = RequestMethod.GET)
public CompletableFuture<IotApplication> showIotApplication(@PathVariable(name = "app_id") String appId) {
IotApplication application = new IotApplication();
application.setId(appId);
application.setName("default application");
application.setCreatedTime("2021-02-28");
@RequestMapping(value = "/v5/iot/instances/{instance_id}", method = RequestMethod.GET)
public CompletableFuture<IotInstance> showIotApplication(@PathVariable(name = "instance_id") String instanceId) {
IotInstance application = new IotInstance();
application.setInstanceId(instanceId);
application.setClusterId(new StringBuilder(instanceId).reverse().toString());

return CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.SECONDS.sleep(3);
}
catch (InterruptedException e) {
e.printStackTrace();
}
return application;
});
return CompletableFuture.supplyAsync(() -> application);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

@Getter
@Setter
public class IotApplication {
private String id;
private String name;
private String createdTime;
public class IotInstance {
private String instanceId;
private String clusterId;
}
2 changes: 1 addition & 1 deletion samples/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
server:
port: 5230
port: 8080

spring:
mvc:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.ctlove0523.consul.health;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class TestController {

@RequestMapping(value = "/api/lb",method = RequestMethod.GET)
public ResponseEntity<String> lb() {
return new ResponseEntity<>("lb", HttpStatus.OK);
}

@RequestMapping(value = "/api/no-lb",method = RequestMethod.GET)
public ResponseEntity<String> noLb() {
return new ResponseEntity<>("no-lb", HttpStatus.OK);
}
}
2 changes: 1 addition & 1 deletion spring-cloud-consul/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
server:
port: 8765
port: 5231
spring:
application:
name: spring-cloud-consul-application
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.ctlove0523.spring.gateway.config;

import reactor.netty.http.client.HttpClient;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
public class HttpClientConfig implements BeanPostProcessor {

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof HttpClient) {
HttpClient client = (HttpClient) bean;
return client.metrics(true, s -> s);

}
return bean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.ctlove0523.spring.gateway.config;

import java.util.function.Function;

import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.config.MeterFilter;
import reactor.core.publisher.Mono;
import reactor.netty.http.server.HttpServer;

import org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryCustomizer;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.boot.web.embedded.netty.NettyServerCustomizer;
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;

@Component
public class ServerConfigure implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {


@Override
public void customize(NettyReactiveWebServerFactory factory) {
Metrics.globalRegistry
.config()
.meterFilter(MeterFilter.maximumAllowableTags("reactor.netty.http.server", "URI", 100, MeterFilter.deny()));

factory.addServerCustomizers(new NettyServerCustomizer() {
@Override
public HttpServer apply(HttpServer httpServer) {
return httpServer.metrics(true, new Function<String, String>() {
@Override
public String apply(String s) {
return s;
}
});
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.ctlove0523.spring.gateway.health;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class TestController {

@RequestMapping(value = "/api/lb",method = RequestMethod.GET)
public ResponseEntity<String> lb() {
return new ResponseEntity<>("lb", HttpStatus.OK);
}

@RequestMapping(value = "/api/no-lb",method = RequestMethod.GET)
public ResponseEntity<String> noLb() {
return new ResponseEntity<>("no-lb", HttpStatus.OK);
}
}

0 comments on commit 2212771

Please sign in to comment.