Spring cloud practical tips: how does turbine aggregate the hysr with context path set
mob60475702efd6 2021-07-19 11:03:08


Before that spring for all The community sees such a problem : When actuator The endpoint is set context-path after ,turbine How to aggregate data ? First , We need to know actuator The endpoint is set context-path What does that mean? ? in other words , here spring boot actuator All the endpoints of have a prefix , such as :

management.context-path=/xxx

If the above parameters are set , That's for collecting hystrix The endpoint of the data will become :/xxx/hystrix.stream, If we still get one Spring Cloud Build the microservices architecture :Hystrix Monitor data aggregation 【Dalston edition 】 Build your turbine application , Then you'll see the following error :


INFO 7812 --- [        Timer-0] c.n.t.monitor.instance.InstanceMonitor   : Url for host: http://172.15.0.18:9020/hystrix.stream defaultERROR 7812 --- [InstanceMonitor] c.n.t.monitor.instance.InstanceMonitor   : Could not initiate connection to host, giving up: [{"timestamp":1499941336284,"status":404,"error":"Not Found","message":"No message available","path":"/hystrix.stream"}]WARN 7812 --- [InstanceMonitor] c.n.t.monitor.instance.InstanceMonitor   : Stopping InstanceMonitor for: 172.15.0.18:9020 defaultcom.netflix.turbine.monitor.instance.InstanceMonitor$MisconfiguredHostException: [{"timestamp":1499941336284,"status":404,"error":"Not Found","message":"No message available","path":"/hystrix.stream"}] at com.netflix.turbine.monitor.instance.InstanceMonitor.init(InstanceMonitor.java:318) ~[turbine-core-1.0.0.jar:na] at com.netflix.turbine.monitor.instance.InstanceMonitor.access$100(InstanceMonitor.java:103) ~[turbine-core-1.0.0.jar:na] at com.netflix.turbine.monitor.instance.InstanceMonitor$2.call(InstanceMonitor.java:235) [turbine-core-1.0.0.jar:na] at com.netflix.turbine.monitor.instance.InstanceMonitor$2.call(InstanceMonitor.java:229) [turbine-core-1.0.0.jar:na] at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_91] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_91] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_91] at java.lang.Thread.run(Thread.java:745) [na:1.8.0_91]

From the above mistakes we can see that ,turbine At the time of collection, because the visit is /hystrix.stream, At this point, the collection endpoint is /xxx/hystrix.stream, So I did 404 error .

So how do we solve it ? Through the previous configuration content , We may not be able to find the relevant configuration information , So we can only traverse the source code , Finally, find this class :org.springframework.cloud.netflix.turbine.SpringClusterMonitor. Its specific contents are as follows :


public static InstanceUrlClosure ClusterConfigBasedUrlClosure = new InstanceUrlClosure() {private final DynamicStringProperty defaultUrlClosureConfig = DynamicPropertyFactory .getInstance().getStringProperty("turbine.instanceUrlSuffix",      "hystrix.stream");private final DynamicBooleanProperty instanceInsertPort = DynamicPropertyFactory .getInstance().getBooleanProperty("turbine.instanceInsertPort", true);@Overridepublic String getUrlPath(Instance host) {if (host.getCluster() == null) {throw new RuntimeException("Host must have cluster name in order to use ClusterConfigBasedUrlClosure"); }// find url String key = "turbine.instanceUrlSuffix." + host.getCluster(); DynamicStringProperty urlClosureConfig = DynamicPropertyFactory.getInstance() .getStringProperty(key, null); String url = urlClosureConfig.get();if (url == null) { url = this.defaultUrlClosureConfig.get(); }if (url == null) {throw new RuntimeException("Config property: " + urlClosureConfig.getName() + " or " + this.defaultUrlClosureConfig.getName() + " must be set"); }// find port and scheme String port; String scheme;if (host.getAttributes().containsKey("securePort")) { port = host.getAttributes().get("securePort"); scheme = "https"; } else { port = host.getAttributes().get("port"); scheme = "http"; }if (host.getAttributes().containsKey("fusedHostPort")) {return String.format("%s://%s/%s", scheme, host.getAttributes().get("fusedHostPort"), url); }// determine if to insert port String insertPortKey = "turbine.instanceInsertPort." + host.getCluster(); DynamicStringProperty insertPortProp = DynamicPropertyFactory.getInstance() .getStringProperty(insertPortKey, null);boolean insertPort;if (insertPortProp.get() == null) { insertPort = this.instanceInsertPort.get(); }else { insertPort = Boolean.parseBoolean(insertPortProp.get()); }// format url with portif (insertPort) {if (url.startsWith("/")) { url = url.substring(1); }if (port == null) {throw new RuntimeException("Configured to use port, but port or securePort is not in host attributes"); }return String.format("%s://%s:%s/%s", scheme, host.getHostname(), port, url); }//format url without portreturn scheme + "://" + host.getHostname() + url; }};

From the above source , We can find this parameter turbine.instanceUrlSuffix, So the problem can be solved , All we need to do is turbine Add the following configuration information to the application configuration file , You can collect the previous configuration correctly management.context-path=/xxx Of micro services hystrix Data. .

turbine.instanceUrlSuffix=/xxx/hystrix.stream



Please bring the original link to reprint ,thank
Similar articles

2021-06-04

2021-06-04

2021-06-06

2021-06-27

2021-06-29