2019独角兽企业重金招聘Python工程师标准>>>
此次实践项目需要两个两个,一个是eureka-server,另一个是需要新建的项目service-hi-hystrix-dashboard,
1.新建一个项目service-hi-hystrix-dashboard
build.gradle文件
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
ext {
springCloudVersion = 'Finchley.SR1'
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator') #这个必须有,它能启动metrics filter
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
compile('org.springframework.cloud:spring-cloud-starter-netflix-hystrix')
compile('org.springframework.cloud:spring-cloud-starter-netflix-hystrix-dashboard')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
主方法:
package com.example.servicehihystrixdashboard;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@EnableDiscoveryClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
@SpringBootApplication
public class ServiceHiHystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceHiHystrixDashboardApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
@HystrixCommand(fallbackMethod = "hiError")
public String home(@RequestParam String name) {
return "hi "+name+",i am from port:" +port;
}
public String hiError(String name) {
return "hi,"+name+",sorry,error!";
}
}
application.yml文件:
server:
port: 9294
spring:
application:
name: service-hi-hystrix-dashboard
eureka:
client:
service-url:
defaultZone: http://localhost:8791/eureka/
management:
endpoints:
web:
exposure:
include: hystrix.stream
此时依次启动eureka-server项目和service-hi-hystrix-dashboard项目,启动成功后,控制台是这样的:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.4.RELEASE)
2018-08-14 15:36:22.022 INFO 3449 --- [ main] e.s.ServiceHiHystrixDashboardApplication : No active profile set, falling back to default profiles: default
2018-08-14 15:36:22.041 INFO 3449 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6bc407fd: startup date [Tue Aug 14 15:36:22 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@7c7b252e
2018-08-14 15:36:22.839 INFO 3449 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=f7939dd6-e700-3887-bcd5-6cd87ef02579
2018-08-14 15:36:22.854 INFO 3449 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2018-08-14 15:36:23.033 INFO 3449 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$8065379a] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-08-14 15:36:23.327 INFO 3449 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9294 (http)
2018-08-14 15:36:23.345 INFO 3449 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-08-14 15:36:23.345 INFO 3449 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.32
2018-08-14 15:36:23.348 INFO 3449 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/hfguan/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2018-08-14 15:36:23.438 INFO 3449 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-08-14 15:36:23.438 INFO 3449 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1397 ms
2018-08-14 15:36:23.700 WARN 3449 --- [ost-startStop-1] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2018-08-14 15:36:23.701 INFO 3449 --- [ost-startStop-1] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2018-08-14 15:36:23.707 INFO 3449 --- [ost-startStop-1] c.netflix.config.DynamicPropertyFactory : DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@175eb86f
2018-08-14 15:36:24.374 INFO 3449 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-08-14 15:36:24.374 INFO 3449 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'webMvcMetricsFilter' to: [/*]
2018-08-14 15:36:24.375 INFO 3449 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-08-14 15:36:24.375 INFO 3449 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-08-14 15:36:24.375 INFO 3449 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-08-14 15:36:24.375 INFO 3449 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpTraceFilter' to: [/*]
2018-08-14 15:36:24.375 INFO 3449 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet proxyStreamServlet mapped to [/proxy.stream]
2018-08-14 15:36:24.376 INFO 3449 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-08-14 15:36:24.377 INFO 3449 --- [ost-startStop-1] o.s.b.a.e.web.ServletEndpointRegistrar : Registered '/actuator/hystrix.stream' to hystrix.stream-actuator-endpoint
2018-08-14 15:36:24.462 INFO 3449 --- [ main] o.s.ui.freemarker.SpringTemplateLoader : SpringTemplateLoader for FreeMarker: using resource loader [org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6bc407fd: startup date [Tue Aug 14 15:36:22 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@7c7b252e] and template loader path [classpath:/templates/]
2018-08-14 15:36:24.462 INFO 3449 --- [ main] o.s.w.s.v.f.FreeMarkerConfigurer : ClassTemplateLoader for Spring macros added to FreeMarker configuration
2018-08-14 15:36:24.500 WARN 3449 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2018-08-14 15:36:24.500 INFO 3449 --- [ main] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2018-08-14 15:36:24.594 INFO 3449 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-08-14 15:36:24.809 INFO 3449 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6bc407fd: startup date [Tue Aug 14 15:36:22 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@7c7b252e
2018-08-14 15:36:24.859 INFO 3449 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hi]}" onto public java.lang.String com.example.servicehihystrixdashboard.ServiceHiHystrixDashboardApplication.home(java.lang.String)
2018-08-14 15:36:24.860 INFO 3449 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hystrix]}" onto public java.lang.String org.springframework.cloud.netflix.hystrix.dashboard.HystrixDashboardController.home(org.springframework.ui.Model,org.springframework.web.context.request.WebRequest)
2018-08-14 15:36:24.860 INFO 3449 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hystrix/{path}]}" onto public java.lang.String org.springframework.cloud.netflix.hystrix.dashboard.HystrixDashboardController.monitor(java.lang.String,org.springframework.ui.Model,org.springframework.web.context.request.WebRequest)
2018-08-14 15:36:24.863 INFO 3449 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-08-14 15:36:24.864 INFO 3449 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-08-14 15:36:24.894 INFO 3449 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-08-14 15:36:24.894 INFO 3449 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-08-14 15:36:25.377 INFO 3449 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator'
2018-08-14 15:36:25.386 INFO 3449 --- [ main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto protected java.util.Map> org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping.links(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-08-14 15:36:25.434 INFO 3449 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-08-14 15:36:25.442 INFO 3449 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'environmentManager' has been autodetected for JMX exposure
2018-08-14 15:36:25.444 INFO 3449 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'configurationPropertiesRebinder' has been autodetected for JMX exposure
2018-08-14 15:36:25.444 INFO 3449 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'refreshScope' has been autodetected for JMX exposure
2018-08-14 15:36:25.447 INFO 3449 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located managed bean 'environmentManager': registering with JMX server as MBean [org.springframework.cloud.context.environment:name=environmentManager,type=EnvironmentManager]
2018-08-14 15:36:25.454 INFO 3449 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located managed bean 'refreshScope': registering with JMX server as MBean [org.springframework.cloud.context.scope.refresh:name=refreshScope,type=RefreshScope]
2018-08-14 15:36:25.462 INFO 3449 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located managed bean 'configurationPropertiesRebinder': registering with JMX server as MBean [org.springframework.cloud.context.properties:name=configurationPropertiesRebinder,context=6bc407fd,type=ConfigurationPropertiesRebinder]
2018-08-14 15:36:25.471 INFO 3449 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2018-08-14 15:36:25.482 INFO 3449 --- [ main] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING
2018-08-14 15:36:25.512 INFO 3449 --- [ main] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1
2018-08-14 15:36:25.703 INFO 3449 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson
2018-08-14 15:36:25.703 INFO 3449 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson
2018-08-14 15:36:25.803 INFO 3449 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml
2018-08-14 15:36:25.803 INFO 3449 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml
2018-08-14 15:36:25.973 INFO 3449 --- [ main] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
2018-08-14 15:36:25.989 INFO 3449 --- [ main] com.netflix.discovery.DiscoveryClient : Disable delta property : false
2018-08-14 15:36:25.989 INFO 3449 --- [ main] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null
2018-08-14 15:36:25.989 INFO 3449 --- [ main] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false
2018-08-14 15:36:25.989 INFO 3449 --- [ main] com.netflix.discovery.DiscoveryClient : Application is null : false
2018-08-14 15:36:25.989 INFO 3449 --- [ main] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true
2018-08-14 15:36:25.989 INFO 3449 --- [ main] com.netflix.discovery.DiscoveryClient : Application version is -1: true
2018-08-14 15:36:25.989 INFO 3449 --- [ main] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2018-08-14 15:36:26.162 INFO 3449 --- [ main] com.netflix.discovery.DiscoveryClient : The response status is 200
2018-08-14 15:36:26.165 INFO 3449 --- [ main] com.netflix.discovery.DiscoveryClient : Starting heartbeat executor: renew interval is: 30
2018-08-14 15:36:26.166 INFO 3449 --- [ main] c.n.discovery.InstanceInfoReplicator : InstanceInfoReplicator onDemand update allowed rate per min is 4
2018-08-14 15:36:26.170 INFO 3449 --- [ main] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1534232186169 with initial instances count: 1
2018-08-14 15:36:26.174 INFO 3449 --- [ main] o.s.c.n.e.s.EurekaServiceRegistry : Registering application service-hi-hystrix-dashboard with eureka with status UP
2018-08-14 15:36:26.174 INFO 3449 --- [ main] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1534232186174, current=UP, previous=STARTING]
2018-08-14 15:36:26.176 INFO 3449 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_SERVICE-HI-HYSTRIX-DASHBOARD/10.207.21.172:service-hi-hystrix-dashboard:9294: registering service...
2018-08-14 15:36:26.207 INFO 3449 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_SERVICE-HI-HYSTRIX-DASHBOARD/10.207.21.172:service-hi-hystrix-dashboard:9294 - registration status: 204
2018-08-14 15:36:26.230 INFO 3449 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9294 (http) with context path ''
2018-08-14 15:36:26.232 INFO 3449 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 9294
2018-08-14 15:36:26.242 INFO 3449 --- [ main] e.s.ServiceHiHystrixDashboardApplication : Started ServiceHiHystrixDashboardApplication in 5.174 seconds (JVM running for 5.787)
2018-08-14 15:36:28.042 INFO 3449 --- [nio-9294-exec-1] ashboardConfiguration$ProxyStreamServlet :
Proxy opening connection to: http://localhost:9294/actuator/hystrix.stream
2018-08-14 15:36:28.042 INFO 3449 --- [nio-9294-exec-2] ashboardConfiguration$ProxyStreamServlet :
Proxy opening connection to: http://localhost:9294/actuator/hystrix.stream
2018-08-14 15:36:37.799 INFO 3449 --- [nio-9294-exec-6] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-08-14 15:36:37.799 INFO 3449 --- [nio-9294-exec-6] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-08-14 15:36:37.819 INFO 3449 --- [nio-9294-exec-6] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 20 ms
访问一下http://loclahost:9294/hi?12352
访问 http://localhost:9294/actuator/hystrix.stream
接下来访问http://localhost:9294/hystrix
点击monitor stream