最近花了一些时间,看了一下OpenResty的东西,感觉用来做API网关的确很赞:
https://github.com/neohope/NeoDemosOpenResty
官网:
https://openresty.org/cn/
Learn and share.
最近花了一些时间,看了一下OpenResty的东西,感觉用来做API网关的确很赞:
https://github.com/neohope/NeoDemosOpenResty
官网:
https://openresty.org/cn/
1、spring-boot
web容器整合,快速开发,上下文、外部配置、日志的统一管理
2、spring-boot-autoconfigure
自动配置,自动判断需要的jar包
3、spring-boot-actuator
生产环境管理,用rest方式给出多种接口:
mappings/autoconfig/configprops/beans
env/info/health/heapdump/metrics
loggers/logfile/dump/trace
shutdown/auditevents
4、spring-boot-starters
各种配置好的功能模块,用于快速拼装各种需要的功能
5、spring-boot-loader
用于加载jar包中的jar,可以实现单个jar/war的运行
注意:这种jar包放到jar包时,不要再次压缩
6、spring-boot-cli
快速开发groovy
7、spring-boot-devtools
方便调试,远程调试
1、test.groovy
@RestController class testApp{ @RequestMapping("/") String home() { "It works!" } }
2、运行
bin/spring run test.groovy
3、这样,一个spring boot的网站已经开启了哦
1、下载并运行zipkin
https://github.com/openzipkin/docker-zipkin
sudo ./docker-compose up
2、运行示例程序,并触发对应方法
spring-cloud-sleuth-sample-zipkin(需要Zipkin) 或者 spring-cloud-sleuth-sample-stream(需要RabbitMQ+Zipkin)
3、查看运行情况
http://localhost:9411
Feign是一个很好用的REST通讯包,十分强大。
1、MyFeignClient
package com.neohope.springcloud.test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import feign.hystrix.FallbackFactory; import org.springframework.stereotype.Component; @SpringBootApplication @EnableEurekaClient @EnableFeignClients(clients = {MyFeignClient.IServiceClient.class}) @RestController public class MyFeignClient { @Value("${spring.application.name}") private String appName; @RequestMapping("/appName") public String appName() { return appName; } public static void main(String[] args) { new SpringApplicationBuilder(MyFeignClient.class).web(true).run(args); } @RequestMapping("/") public String hello() { return "It works"; } @Autowired private IServiceClient aClient; @RequestMapping("/invokeC") public String invokeC() { return aClient.invokeA(); } @RequestMapping("/invokeC/{clientName}") String invokeC(@PathVariable("clientName") String clientName) { if(aClient==null) { return "aClient is null"; } else { //return aClient.invokeA(clientName); return aClient.invokeA(); } } //primary = false @FeignClient(name="netflix-feign-server", configuration = MyFeignClientConfiguration.class, fallbackFactory=HystrixClientFallbackFactory.class) public interface IServiceClient { @RequestMapping(method = RequestMethod.GET, value = "/invokeA") String invokeA(); //@RequestMapping(method = RequestMethod.GET, value = "/invokeA/{clientName}", consumes = "application/json") //String invokeA(@PathVariable("clientName") String clientName); } @Component public static class HystrixClientFallbackFactory implements FallbackFactory<IServiceClient>{ @Override public IServiceClient create(final Throwable cause) { return new IServiceClient() { @Override public String invokeA() { return "invokeA failed: "+cause.getMessage(); } //@Override //public String invokeA(String clientName) //{ // return "invokeA with clientName "+clientName+" failed: "+cause.getMessage(); //} }; } } }
2、MyFeignClientConfiguration
package com.neohope.springcloud.test; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import feign.Logger; import feign.auth.BasicAuthRequestInterceptor; @Configuration public class MyFeignClientConfiguration { @Bean public BasicAuthRequestInterceptor basicAuthRequestInterceptor() { return new BasicAuthRequestInterceptor("user", "hystrix"); } /* @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } */ }
3、pom.xml
<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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.cloud.stream.app</groupId> <artifactId>app-starters-build</artifactId> <version>1.1.3.M1</version> </parent> <groupId>com.neohope.springcloud.test</groupId> <artifactId>netflix-feign-client</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>netflix-feign-client</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.3.0.M1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
4、application.properties
spring.application.name=feign-client server.port=7002 security.user.name=user security.user.password=hystrix eureka.client.serviceUrl.defaultZone=http://user:hystrix@localhost:7000/eureka/ eureka.client.preferSameZoneEureka=true eureka.client.healthcheck.enabled=true eureka.instance.metadataMap.zone = zone1 eureka.instance.statusPageUrlPath=${management.context-path}/info eureka.instance.healthCheckUrlPath=${management.context-path}/health eureka.instance.lease-renewal-interval-in-seconds=5 eureka.instance.lease-expiration-duration-in-seconds=10 feign.hystrix.enabled=true #feign.compression.request.enabled=true #feign.compression.response.enabled=true #feign.compression.request.enabled=true #feign.compression.request.mime-types=text/xml,application/xml,application/json #feign.compression.request.min-request-size=2048 #NONE,BASIC,HEADERS,FULL logging.level.com.neohope.springcloud.test.MyFeignClient.IServiceClient=BASIC
SideCar主要是用于将非JVM语言的服务,代理为Netflix服务
1、SideCarApp
package com.neohope.springcloud.test; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.sidecar.EnableSidecar; import org.springframework.web.bind.annotation.RequestMapping; //作用一、注册到eureka并提供服务健康状态 //其他语言的服务,首先服务封装为REST或JSON通信,并提供一个health路径,反馈服务是否正常 //sidecar会将这个服务注册到eureka //其他服务,可以通过eureka查询并调用该服务 //作用二、从sidecar获取服务地址,并调用 //访问sidecar,获取服务地址 http://localhost:port/hosts/{serviceId}. //分析返回结果,得到服务地址 //调用需要的服务 @EnableSidecar public class SideCarApp { @Value("${spring.application.name}") private String appName; @RequestMapping("/appName") public String appName() { return appName; } @RequestMapping("/") public String hello() { return "It works"; } public static void main( String[] args ) { new SpringApplicationBuilder(SideCarApp.class).web(true).run(args); } }
2、pom.xml
<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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.cloud.stream.app</groupId> <artifactId>app-starters-build</artifactId> <version>1.1.3.M1</version> </parent> <groupId>com.neohope.springcloud.test</groupId> <artifactId>netflix-sidecar-test</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>netflix-sidecar-test</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-sidecar</artifactId> <version>1.3.0.M1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
3、application.properties
spring.application.name=zuul-gateway-app server.port=7001 security.user.name=user security.user.password=hystrix eureka.client.serviceUrl.defaultZone=http://user:hystrix@localhost:7000/eureka/ eureka.client.preferSameZoneEureka=true eureka.client.healthcheck.enabled=true eureka.instance.metadataMap.zone = zone1 eureka.instance.statusPageUrlPath=${management.context-path}/info eureka.instance.healthCheckUrlPath=${management.context-path}/health eureka.instance.lease-renewal-interval-in-seconds=5 eureka.instance.lease-expiration-duration-in-seconds=10 sidecar.port=9999 sidecar.health-uri=http://localhost:9999/health.json
Zuul就是一个API网关。
1、ZuulGatewayApp.java
package com.neohope.springcloud.test; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableEurekaClient @RestController @EnableZuulProxy //@EnableZuulServer public class ZuulGatewayApp { @Value("${spring.application.name}") private String appName; @RequestMapping("/appName") public String appName() { return appName; } public static void main(String[] args) { new SpringApplicationBuilder(ZuulGatewayApp.class).web(true).run(args); } @RequestMapping("/") public String hello() { return "It works"; } }
2、pom.xml
<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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.cloud.stream.app</groupId> <artifactId>app-starters-build</artifactId> <version>1.1.3.M1</version> </parent> <groupId>com.neohope.springcloud.test</groupId> <artifactId>netflix-zuul-gateway</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>netflix-zuul-gateway</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>com.netflix.feign</groupId> <artifactId>feign-okhttp</artifactId> <version>8.18.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
3、application.properties
spring.application.name=zuul-gateway-app server.port=7001 security.user.name=user security.user.password=hystrix eureka.client.serviceUrl.defaultZone=http://user:hystrix@localhost:7000/eureka/ eureka.client.preferSameZoneEureka=true eureka.client.healthcheck.enabled=true eureka.instance.metadataMap.zone = zone1 eureka.instance.statusPageUrlPath=${management.context-path}/info eureka.instance.healthCheckUrlPath=${management.context-path}/health eureka.instance.lease-renewal-interval-in-seconds=5 eureka.instance.lease-expiration-duration-in-seconds=10 zuul.host.maxTotalConnections=200 zuul.host.maxPerRouteConnections=20 #zuul.ignoredServices='*' zuul.routes.netflix-feign-server.sensitiveHeaders zuul.routes.netflix-feign-server.path=/invokeA/** zuul.routes.netflix-feign-server.serviceId=netflix-feign-server #zuul.routes.netflix-feign-server.url= #zuul.ribbonIsolationStrategy=THREAD #zuul.prefix=api zuul.stripPrefix=false #zuul.addProxyHeaders=false #zuul.ignoredHeaders= #zuul.forceOriginalQueryStringEncoding=true #zuul.SendResponseFilter.post.disable=true. #ribbon.eureka.enabled=false #ribbon.restclient.enabled=true #ribbon.okhttp.enabled=true #ribbon.ConnectTimeout=3000 #ribbon.ReadTimeout=60000 #users.ribbon.listOfServers=172.16.172.63 #hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000
Turbine是一个日志收集器,用于聚合Hystrix中的日志。
1、TurbineServer
package com.neohope.springcloud.test; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.netflix.turbine.stream.EnableTurbineStream; import org.springframework.web.bind.annotation.RequestMapping; @SpringBootApplication @EnableCircuitBreaker @EnableEurekaServer @EnableHystrixDashboard @EnableTurbineStream public class TurbineServer { @Value("${spring.application.name}") private String appName; @RequestMapping("/appName") public String appName() { return appName; } public static void main(String[] args) { new SpringApplicationBuilder(TurbineServer.class).web(true).run(args); } }
2、pom.xml
<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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.cloud.stream.app</groupId> <artifactId>app-starters-build</artifactId> <version>1.1.3.M1</version> </parent> <groupId>com.neohope.springcloud.test</groupId> <artifactId>netflix-turbine-server</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>netflix-turbine-server</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine-stream</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-kafka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.3.0.M1</version> </dependency> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
3、application.properties
spring.application.name=turbine-server server.port=7000 security.user.name=user security.user.password=hystrix eureka.instance.leaseRenewalIntervalInSeconds=5 eureka.instance.metadataMap.zone = zone1 eureka.server.enable-self-preservation=false eureka.server.eviction-interval-timer-in-ms=5000 eureka.client.serviceUrl.defaultZone=http://user:hystrix@localhost:7000/eureka/ eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false eureka.client.preferSameZoneEureka=true manager.port=7000 turbine.stream.port=8989
4、TurbineClient
package com.neohope.springcloud.test; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @SpringBootApplication @EnableEurekaClient @RestController @EnableCircuitBreaker public class TurbineClient { @Value("${spring.application.name}") private String appName; @RequestMapping("/appName") public String appName() { return appName; } @RequestMapping("/") public String hello() { return "It works"; } @RequestMapping("/invokeA") @HystrixCommand(fallbackMethod = "invoekD") public String invokeA() { if(Math.random()>0.2) { throw new RuntimeException("Hystrix Test A thrown"); } else { return "Hystrix Test A"; } } @RequestMapping("/invokeB") @HystrixCommand(fallbackMethod = "invoekD") public String invokeB() { if(Math.random()>0.5) { throw new RuntimeException("Hystrix Test A thrown"); } else { return "Hystrix Test A"; } } @RequestMapping("/invokeD") public String invoekD() { return "Hystrix Test D"; } public static void main(String[] args) { new SpringApplicationBuilder(TurbineClient.class).web(true).run(args); } }
5、pom.xml
<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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.cloud.stream.app</groupId> <artifactId>app-starters-build</artifactId> <version>1.1.3.M1</version> </parent> <groupId>com.neohope.springcloud.test</groupId> <artifactId>netflix-turbine-client</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>netflix-turbine-client</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-hystrix-stream</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-kafka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.3.0.M1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
6、application.properties
spring.application.name=turbine-client server.port=7001 security.user.name=user security.user.password=hystrix eureka.client.serviceUrl.defaultZone=http://user:hystrix@localhost:7000/eureka/ eureka.client.preferSameZoneEureka=true eureka.client.healthcheck.enabled=true eureka.instance.metadataMap.zone = zone1 eureka.instance.statusPageUrlPath=${management.context-path}/info eureka.instance.healthCheckUrlPath=${management.context-path}/health eureka.instance.lease-renewal-interval-in-seconds=5 eureka.instance.lease-expiration-duration-in-seconds=10 #eureka.instance.metadata-map.management.port=7001 #management.port=7002 turbine.aggregator.clusterConfig=default turbine.appConfig=turbine-client turbine.stream.port=8989
7、在以下地址可以访问
http://172.16.172.63:7001/hystrix.stream
http://user:hystrix@172.16.172.63:7002/turbine.stream
HyStrix主要负责日志收集。同时是一个很棒的断路器。
1、HystrixServer
package com.neohope.springcloud.test; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.netflix.turbine.EnableTurbine; import org.springframework.web.bind.annotation.RequestMapping; @SpringBootApplication @EnableCircuitBreaker @EnableEurekaServer @EnableHystrixDashboard @EnableTurbine public class HystrixServer { @Value("${spring.application.name}") private String appName; @RequestMapping("/appName") public String appName() { return appName; } public static void main(String[] args) { new SpringApplicationBuilder(HystrixServer.class).web(true).run(args); } }
2、pom.xml
<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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.cloud.stream.app</groupId> <artifactId>app-starters-build</artifactId> <version>1.1.3.M1</version> </parent> <groupId>com.neohope.springcloud.test</groupId> <artifactId>netflix-hystrix-server</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>netflix-hystrix-server</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.3.0.M1</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine</artifactId> </dependency> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
3、application.properties
spring.application.name=hystrix-server server.port=7000 security.user.name=user security.user.password=hystrix eureka.instance.leaseRenewalIntervalInSeconds=5 eureka.instance.metadataMap.zone = zone1 eureka.server.enable-self-preservation=false eureka.server.eviction-interval-timer-in-ms=5000 eureka.client.serviceUrl.defaultZone=http://user:hystrix@localhost:7000/eureka/ eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false eureka.client.preferSameZoneEureka=true #turbine.instanceUrlSuffix=/hystrix.stream turbine.aggregator.clusterConfig=default turbine.appConfig=hystrix-client01,hystrix-client02 #eureka.instance.metadata-map.management.port=${management.port:7000} #management.port=7000
4、HystrixClient01
package com.neohope.springcloud.test; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @SpringBootApplication @EnableEurekaClient @RestController @EnableCircuitBreaker public class HystrixClient01 { @Value("${spring.application.name}") private String appName; @RequestMapping("/appName") public String appName() { return appName; } @RequestMapping("/") public String hello() { return "It works"; } @RequestMapping("/invokeA") //@HystrixCommand(fallbackMethod = "invoekD", //commandProperties = {@HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")}) @HystrixCommand(fallbackMethod = "invoekD") public String invokeA() { if(Math.random()>0.2) { throw new RuntimeException("Hystrix Test A thrown"); } else { return "Hystrix Test A"; } } @RequestMapping("/invokeB") @HystrixCommand(fallbackMethod = "invoekB") public String invokeB() { if(Math.random()>0.5) { throw new RuntimeException("Hystrix Test B thrown"); } else { return "Hystrix Test B"; } } @RequestMapping("/invokeD") public String invoekD() { return "Hystrix Test D"; } public static void main(String[] args) { new SpringApplicationBuilder(HystrixClient01.class).web(true).run(args); } }
5、pom.xml
<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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.cloud.stream.app</groupId> <artifactId>app-starters-build</artifactId> <version>1.1.3.M1</version> </parent> <groupId>com.neohope.springcloud.test</groupId> <artifactId>netflix-hystrix-client01</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>netflix-hystrix-client01</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.3.0.M1</version> </dependency> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
6、application.properties
spring.application.name=hystrix-client01 server.port=7001 security.user.name=user security.user.password=hystrix eureka.client.serviceUrl.defaultZone=http://user:hystrix@localhost:7000/eureka/ eureka.client.preferSameZoneEureka=true eureka.client.healthcheck.enabled=true eureka.instance.metadataMap.zone = zone1 eureka.instance.statusPageUrlPath=${management.context-path}/info eureka.instance.healthCheckUrlPath=${management.context-path}/health eureka.instance.lease-renewal-interval-in-seconds=5 eureka.instance.lease-expiration-duration-in-seconds=10
7、HystrixClient02
package com.neohope.springcloud.test; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; //import org.springframework.cloud.netflix.turbine.EnableTurbine; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @SpringBootApplication @EnableEurekaClient @RestController @EnableCircuitBreaker //@EnableTurbine public class HystrixClient02 { @Value("${spring.application.name}") private String appName; @RequestMapping("/appName") public String appName() { return appName; } @RequestMapping("/") public String hello() { return "It works"; } @RequestMapping("/invokeA") @HystrixCommand(fallbackMethod = "invoekD") public String invokeA() { if(Math.random()>0.2) { throw new RuntimeException("Hystrix Test A thrown"); } else { return "Hystrix Test A"; } } @RequestMapping("/invokeB") @HystrixCommand(fallbackMethod = "invoekB") public String invokeB() { if(Math.random()>0.5) { throw new RuntimeException("Hystrix Test B thrown"); } else { return "Hystrix Test A"; } } @RequestMapping("/invokeD") public String invoekD() { return "Hystrix Test D"; } public static void main(String[] args) { new SpringApplicationBuilder(HystrixClient02.class).web(true).run(args); } }
8、pom.xml
<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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.cloud.stream.app</groupId> <artifactId>app-starters-build</artifactId> <version>1.1.3.M1</version> </parent> <groupId>com.neohope.springcloud.test</groupId> <artifactId>netflix-hystrix-client02</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>netflix-hystrix-client02</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <!--dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine</artifactId> </dependency--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.3.0.M1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
9、application.properties
spring.application.name=hystrix-client02 server.port=7002 security.user.name=user security.user.password=hystrix eureka.client.serviceUrl.defaultZone=http://user:hystrix@localhost:7000/eureka/ eureka.client.preferSameZoneEureka=true eureka.client.healthcheck.enabled=true eureka.instance.metadataMap.zone = zone1 eureka.instance.statusPageUrlPath=${management.context-path}/info eureka.instance.healthCheckUrlPath=${management.context-path}/health eureka.instance.lease-renewal-interval-in-seconds=5 eureka.instance.lease-expiration-duration-in-seconds=10
10、可以在下面的地址,查看日志输出
http://172.16.172.63:7001/hystrix.stream
Eureka主要负责服务注册及服务发现,但同时带有负载均衡的作用。
1、AppEurekaServer
package com.neohope.springcloud.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMapping; @Configuration @ComponentScan @EnableAutoConfiguration @EnableEurekaServer public class AppEurekaServer { @RequestMapping("/appName") public String appName() { return System.getenv("spring.application.name"); } public static void main(String[] args) { SpringApplication.run(AppEurekaServer.class, args); //new SpringApplicationBuilder(AppEurekaServer.class).web(true).run(args); } }
2、pom.xml
<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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.cloud.stream.app</groupId> <artifactId>app-starters-build</artifactId> <version>1.1.3.M1</version> <!--groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix</artifactId> <version>1.3.0.M1</version> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.4.RELEASE</version --> </parent> <groupId>com.neohope.springcloud.test</groupId> <artifactId>netflix-eureka-server</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>netflix-eureka-server</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.3.0.M1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!--dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine-stream</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-spectator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-atlas</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-archaius</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-turbine-stream</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-turbine</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-spectator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-hystrix-stream</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-hystrix-amqp</artifactId> </dependency--> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
3、application.properties
spring.application.name=eureka-server server.port=7000 security.user.name=user security.user.password=eureka eureka.instance.leaseRenewalIntervalInSeconds=5 eureka.instance.metadataMap.zone = zone1 eureka.server.enable-self-preservation=false eureka.server.eviction-interval-timer-in-ms=5000 eureka.client.serviceUrl.defaultZone=http://user:eureka@localhost:7000/eureka/ eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false eureka.client.preferSameZoneEureka = true
4、AppEurekaClient
package com.neohope.springcloud.test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.netflix.appinfo.InstanceInfo; import com.netflix.discovery.EurekaClient; @Configuration @ComponentScan @EnableAutoConfiguration @EnableEurekaClient //@EnableDiscoveryClient @RestController public class AppEurekaClient { //@Value("${spring.application.name}") //private String appName; @Autowired private EurekaClient discoveryClient; @RequestMapping("/serviceUrl") public String serviceUrl() { //InstanceInfo instance = discoveryClient.getNextServerFromEureka("eureka-client", false); //return instance.getHomePageUrl(); return System.getenv("spring.application.name"); } @RequestMapping("/") public String home() { return "It works!"; } public static void main(String[] args) { new SpringApplicationBuilder(AppEurekaClient.class).web(true).run(args); } }
5、pom.xml
<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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.cloud.stream.app</groupId> <artifactId>app-starters-build</artifactId> <version>1.1.3.M1</version> <!--groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.4.RELEASE</version --> </parent> <groupId>com.neohope.springcloud.test</groupId> <artifactId>netflix-eureka-client</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>netflix-eureka-client</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.3.0.M1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
6、application.properties
spring.application.name=eureka-client server.port=7001 security.user.name=user security.user.password=eureka eureka.client.serviceUrl.defaultZone=http://user:eureka@localhost:7000/eureka/ eureka.client.preferSameZoneEureka=true eureka.client.healthcheck.enabled=true eureka.instance.metadataMap.zone = zone1 eureka.instance.statusPageUrlPath=${management.context-path}/info eureka.instance.healthCheckUrlPath=${management.context-path}/health eureka.instance.lease-renewal-interval-in-seconds=5 eureka.instance.lease-expiration-duration-in-seconds=10