摘要:序主要有做服務發現做分布式配置做做客戶端負載均衡做斷路器做聚合的做指標監控。配置訪問配置訪問實例調用其他微服務使用使用訪問轉向使用訪問配置的目前看來每秒發送一次貌似太頻繁可以另起一個進行訪問參考
序
主要有eureka做服務發現、config做分布式配置、zuul做api-gateway、feign做客戶端負載均衡、hystrix做斷路器、turbine做聚合的monitor、graphite做指標監控。
eurekapom配置
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-eureka-server junit junit test
application.yml
server: port: 8761 eureka: instance: hostname: discovery client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://discovery:${server.port}/eureka/ spring.cloud.config.discovery.enabled: true
bootstrap.yml
spring: application: name: discovery
application
@SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main( String[] args ) { SpringApplication.run(EurekaApplication.class, args); } }
訪問
http://192.168.99.100:8761/
configpom配置
org.springframework.cloud spring-cloud-starter org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-starter-eureka
application.yml
spring: cloud: config: server: native: search-locations: classpath:/config server: port: 8888
bootstrap.yml
spring: application: name: config profiles: active: native eureka: instance: preferIpAddress: true client: service-url: defaultZone: http://discovery:8761/eureka/
application
@SpringBootApplication @EnableConfigServer @EnableEurekaClient public class ConfigApplication { public static void main( String[] args ) { SpringApplication.run(ConfigApplication.class,args); } }
訪問
http://192.168.99.100:8888/review/default/master
feign實例pom
org.springframework.cloud spring-cloud-starter org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-config-client org.springframework.cloud spring-cloud-starter-eureka com.codecraft common 1.0-SNAPSHOT org.springframework.cloud spring-cloud-starter-ribbon org.springframework.cloud spring-cloud-starter-feign org.springframework.cloud spring-cloud-starter-hystrix org.springframework.cloud spring-cloud-starter-hystrix-dashboard
application.yml
server: port: 9001 endpoints: restart: enabled: true shutdown: enabled: true health: sensitive: false ribbon: eureka: enabled: true
bootstrap.yml
spring: application: name: product cloud: config: uri: http://config:8888 encrypt: failOnError: false eureka: instance: preferIpAddress: true client: registerWithEureka: true fetchRegistry: true serviceUrl: defaultZone: http://discovery:8761/eureka/
FeignClient
@FeignClient("recommend") public interface RemoteRecommendService { @RequestMapping(method = RequestMethod.GET,value = "/recommend") public ListgetRecommendations( @RequestParam(value = "productId", required = true) int productId); }
feign使用
@RestController public class ProductController { private static final Logger LOG = LoggerFactory.getLogger(ProductController.class); @Autowired private SetProcTimeBean setProcTimeBean; @Autowired RemoteRecommendService remoteRecommendService; @RequestMapping("/product/recommends") @HystrixCommand(fallbackMethod = "callRecommendFallback", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "100") }) public ListremoteRecommends(@RequestParam(value = "productId", required = true) int productId){ return remoteRecommendService.getRecommendations(productId); } public List callRecommendFallback(int productId) { return Collections.emptyList(); } @RequestMapping("/product/{productId}") public Product getProduct(@PathVariable int productId) { int pt = setProcTimeBean.calculateProcessingTime(); LOG.info("/product called, processing time: {}", pt); sleep(pt); LOG.debug("/product return the found product"); return new Product(productId, "name", 123); } }
application
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients @EnableCircuitBreaker @EnableHystrix @EnableHystrixDashboard public class ProductApplication { private static final Logger LOG = LoggerFactory.getLogger(ProductApplication.class); public static void main(String[] args){ SpringApplication.run(ProductApplication.class,args); LOG.info("Register ShutdownHook"); Runtime.getRuntime().addShutdownHook(new Thread(){ @Override public void run() { LOG.info("Shutting down product service, unregister from Eureka!"); DiscoveryManager.getInstance().shutdownComponent(); } }); } }api-gateway
pom
org.springframework.cloud spring-cloud-starter-zuul org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator
application.yml
server: port: 10000 #sidecar: #port: 8000 endpoints: restart: enabled: true shutdown: enabled: true health: sensitive: false zuul: ignored-services: "*" routes: product: path: /product/** url: http://product:9001/product recommend: path: /recommend/** url: http://recommend:9002/recommend review: path: /review/** url: http://review:9003/review
bootstrap.yml
spring: application: name: gateway cloud: config: uri: http://config:8888 encrypt: failOnError: false eureka: instance: preferIpAddress: true client: registerWithEureka: true fetchRegistry: true serviceUrl: defaultZone: http://discovery:8761/eureka/
application
@SpringBootApplication @EnableCircuitBreaker @EnableDiscoveryClient @EnableZuulProxy public class ApiGatewayApplication { public static void main( String[] args ) { new SpringApplicationBuilder(ApiGatewayApplication.class).web(true).run(args); } }
訪問
http://192.168.99.100:10000/recommend?productId=1
轉向
http://192.168.99.100:9002/recommend?productId=1
http://192.168.99.100:9001/product/recommends?productId=1
http://192.168.99.100:9001/hystrix
http://192.168.99.100:9003/hystrix/monitor?stream=http%3A%2F%2F192.168.99.100%3A9001%2Fhystrix.stream
turbinepom
org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter org.springframework.cloud spring-cloud-starter-turbine org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-config-client org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-hystrix org.springframework.cloud spring-cloud-starter-hystrix-dashboard
application.yml
server: port: 8889 eureka: instance: preferIpAddress: true client: registerWithEureka: true fetchRegistry: true serviceUrl: defaultZone: http://discovery:8761/eureka/ turbine: appConfig: product,review clusterNameExpression: new String("default")
bootstrap.yml
spring: application: name: turbine cloud: config: uri: http://config:8888 encrypt: failOnError: false
application
@SpringCloudApplication @EnableTurbine @EnableHystrixDashboard public class TurbineApplication { public static void main(String[] args){ SpringApplication.run(TurbineApplication.class,args); } }
訪問
http://192.168.99.100:9001/hystrix
http://192.168.99.100:9001/product/recommends?productId=1
http://192.168.99.100:9003/review/product/100
pom
org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-test test io.dropwizard.metrics metrics-core ${dropwizard-metrics.version} io.dropwizard.metrics metrics-graphite ${dropwizard-metrics.version} io.dropwizard.metrics metrics-annotation ${dropwizard-metrics.version} io.dropwizard.metrics metrics-jvm ${dropwizard-metrics.version} com.ryantenney.metrics metrics-spring 3.1.0 spring-beans org.springframework spring-aop org.springframework
configuration
@Configuration @AutoConfigureAfter(MetricRepositoryAutoConfiguration.class) @ConditionalOnProperty(prefix = "graphite", name = "enabled", matchIfMissing = true) @EnableConfigurationProperties(GraphiteProperties.class) @EnableScheduling @EnableMetrics public class GraphiteAutoConfiguration { private static final Logger logger = LoggerFactory.getLogger(GraphiteAutoConfiguration.class); @Bean public MetricsConfigurerAdapter metricsConfigurerAdapter(final GraphiteProperties graphiteProperties) { return new GraphiteReportingManager(graphiteProperties); } /** * https://qbgbook.gitbooks.io/spring-boot-reference-guide-zh/content/IV.%20Spring%20Boot%20features/36.3.3.%20Property%20conditions.html * @param graphiteProperties * @param metricRegistry * @return */ @Bean @ConditionalOnProperty(value = "graphite.host",matchIfMissing = true) public ConsoleReporter consoleReporter(GraphiteProperties graphiteProperties,MetricRegistry metricRegistry) { ConsoleReporter.Builder builder = ConsoleReporter.forRegistry(metricRegistry); ConsoleReporter reporter = builder.build(); reporter.start(graphiteProperties.getReportInterval(), TimeUnit.MILLISECONDS); return reporter; } }
report
public class GraphiteReportingManager extends MetricsConfigurerAdapter implements DisposableBean { private final Logger logger = LoggerFactory.getLogger(getClass()); private GraphiteProperties props; public GraphiteReportingManager(GraphiteProperties props) { this.props = props; } @Override public void configureReporters(MetricRegistry metricRegistry) { //gc的metrics,目前看來每秒發送一次貌似太頻繁,可以另起一個reporter進行 metricRegistry.register("jvm.gc", new GarbageCollectorMetricSet()); metricRegistry.register("jvm.mem", new MemoryUsageGaugeSet()); metricRegistry.register("jvm.thread-states", new ThreadStatesGaugeSet()); logger.info("graphite host:{},port:{}", props.getHost(), props.getPort()); GraphiteReporter reporter = GraphiteReporter.forRegistry(metricRegistry) .prefixedWith(props.getPrefix()) // .convertRatesTo(TimeUnit.SECONDS) .convertDurationsTo(TimeUnit.MILLISECONDS) .filter(MetricFilter.ALL) .build(createSender(props)); registerReporter(reporter); reporter.start(1L, TimeUnit.SECONDS); } @Override public void destroy() throws Exception { super.destroy(); } private GraphiteSender createSender(GraphiteProperties props) { switch (props.getSenderType()) { case udp: return new GraphiteUDP(props.getHost(), props.getPort()); case tcp: return new Graphite(props.getHost(), props.getPort()); case pickled: return new PickledGraphite(props.getHost(), props.getPort()); default: return new GraphiteUDP(props.getHost(), props.getPort()); } } }
訪問
http://192.168.99.100:8070/
參考Blog Series - Building Microservices
git.blog-microservices
exposing-jvm-metrics-in-spring-boot
git.spring-boot-jvm-monitoring-demo
exporting-to-graphite-with-the-prometheus-java-client
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/65941.html
摘要:序主要有做服務發現做分布式配置做做客戶端負載均衡做斷路器做聚合的做指標監控。配置訪問配置訪問實例調用其他微服務使用使用訪問轉向使用訪問配置的目前看來每秒發送一次貌似太頻繁可以另起一個進行訪問參考 序 主要有eureka做服務發現、config做分布式配置、zuul做api-gateway、feign做客戶端負載均衡、hystrix做斷路器、turbine做聚合的monitor、grap...
摘要:服務提供者的運行機制用了雙層結構來維護注冊的服務信息,第一層為服務的名稱,第二層為服務的實例名稱。服務注冊中心的運行機制為了防止服務的異常下線,會周期性的清理列表中未續約的服務。負載均衡器的基本功能維護該服務下的所有節點列表。 Spring Boot Spring Boot有什么作用 Spring Boot通過自動化的配置簡化Spring原有的樣板化的配置。 Spring Boo...
摘要:劉超,網易云計算首席架構師,有多年的云計算架構與開發經歷,積累了豐富的企業級應用的微服務化,容器化實戰經驗。近日,記者對劉超進行了采訪,跟大家分享了微服務實戰的挑戰和一些常見的微服務誤解,以及他對微服務發展趨勢的判斷。 劉超,網易云計算首席架構師,有10多年的云計算架構與開發經歷,積累了豐富的企業級應用的微服務化,容器化實戰經驗。劉超將擔任今年 5 月份 QCon 全球軟件開發大會廣州...
摘要:微服務集成服務間通信微服務架構下,應用的服務直接相互獨立。微服務架構傾向于降低中心消息總線類似于的依賴,將業務邏輯分布在每個具體的服務終端。 引言:微服務是當前軟件架構領域非常熱門的詞匯,能找到很多關于微服務的定義、準則,以及如何從微服務中獲益的文章,在企業的實踐中去應用微服務的資源卻很少。本篇文章中,會介紹微服務架構(Microservices Architecture)的基礎概念,...
閱讀 977·2021-11-22 09:34
閱讀 2161·2021-11-11 16:54
閱讀 2196·2021-09-27 14:00
閱讀 940·2019-08-30 15:55
閱讀 1525·2019-08-29 12:46
閱讀 599·2019-08-26 18:42
閱讀 639·2019-08-26 13:31
閱讀 3183·2019-08-26 11:52