spring-cloud

Spring Cloud Zuul

混江龙づ霸主 提交于 2019-12-27 17:42:19
目录 Spring Cloud Zuul 总览 创建Zuul转发 配置依赖和完善工程结构 创建失败回调 创建服务过滤 Spring Cloud Zuul Zuul负责路由转发和过滤 总览 在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现、服务消费、负载均衡、熔断器、智能路由、配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统。一个简单的微服务系统如下图: 在 Spring Cloud 微服务系统中,一种常见的负载均衡方式是,客户端的请求首先经过负载均衡(Zuul、Ngnix),再到达服务网关(Zuul 集群),然后再到具体的服。服务统一注册到高可用的服务注册中心集群,服务的所有的配置文件由配置服务管理,配置服务的配置文件放在 GIT 仓库,方便开发人员随时改配置。 创建Zuul转发 配置依赖和完善工程结构 pom.xml <?xml version="1.0" encoding="UTF-8"?> <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

微服务SpringCloud之服务网关zuul一

左心房为你撑大大i 提交于 2019-12-27 07:14:51
前面学习了Eureka、Feign、Hystrix、Config,本篇来学习下API网关zuul。在微服务架构中,后端服务往往不直接开放给调用端,而是通过一个API网关根据请求的url,路由到相应的服务。当添加API网关后,在第三方调用端和服务提供方之间就创建了一面墙,这面墙直接与调用方通信进行权限控制,后将请求均衡分发给后台服务端。 为什么需要API Gateway 1、简化客户端调用复杂度 在微服务架构模式下后端服务的实例数一般是动态的,对于客户端而言很难发现动态改变的服务实例的访问地址信息。因此在基于微服务的项目中为了简化前端的调用逻辑,通常会引入API Gateway作为轻量级网关,同时API Gateway中也会实现相关的认证逻辑从而简化内部服务之间相互调用的复杂度。 2、数据裁剪以及聚合 通常而言不同的客户端对于显示时对于数据的需求是不一致的,比如手机端或者Web端又或者在低延迟的网络环境或者高延迟的网络环境。 因此为了优化客户端的使用体验,API Gateway可以对通用性的响应数据进行裁剪以适应不同客户端的使用需求。同时还可以将多个API调用逻辑进行聚合,从而减少客户端的请求数,优化客户端用户体验 3、多渠道支持 当然我们还可以针对不同的渠道和客户端提供不同的API Gateway,对于该模式的使用由另外一个大家熟知的方式叫Backend for front-end

Spring Boot 2下使用Feign找不到@EnableFeignClients的解决办法

假如想象 提交于 2019-12-26 23:54:24
最近在实践Spring Boot 2+Spring Cloud(Finchley.M9),在用到 Feign 的时候发现 @EnableFeignClients 注解开不了,独立使用Feign是可以的,但就是开启不了Spring对Feign的支持.经过一番摸索终于把问题解决了,在这里分享一下解决方案和思路.解决思路写得较繁琐,可以选择性阅读,结论和解决方案写在前面 0. 结论和解决方案 Spring Cloud对Feign的支持由 org.springframework.cloud:spring-cloud-netflix-core 移到 org.springframework.cloud:spring-cloud-openfeign-core 下,而 Finchley.M9 版本下的 spring-cloud-starter-openfeign:2.0.0.M2 的pom依赖文件中导入的是 spring-cloud-netflix-core 而非 spring-cloud-openfeign-core ,需要我们在pom文件中添加对应依赖管理使 spring-cloud-starter-openfeign 版本更新到 2.0.0.RC1 ,如下图 <dependencyManagement> <dependencies> <dependency> <groupId>org

I cannot see the properties/values using spring cloud config and git

人盡茶涼 提交于 2019-12-25 14:05:18
问题 I am using the sample project https://github.com/spring-cloud-samples/configserver I run the project and when i point my browser to http://localhost:8888/foo/development/ I get the following values { "name": "foo", "profiles": [ "development" ], "label": "master", "propertySources": [ { "name": "overrides", "source": { "eureka.instance.nonSecurePort": "${CF_INSTANCE_PORT:${PORT:${server.port:8080}}}", "eureka.instance.hostname": "${CF_INSTANCE_IP:localhost}", "eureka.client.serviceUrl

Hystrix.stream and management.context

[亡魂溺海] 提交于 2019-12-25 07:00:26
问题 Setting management.context-path = /admin and using @EnableCircuitBreaker makes Hystrix endpoint /admin/hystrix.stream This becomes an issue when using Turbine to aggregate metrics as its looking for instanceserver:port/hystrix.stream when discovering instances via Eureka Any suggestions? 回答1: Full config for turbine: server.port=8082 spring.application.name=turbine management.endpoint.health.enabled=true management.endpoints.jmx.exposure.include=* management.endpoints.web.exposure.include=*

Is there a way to convert an OpenAPI specification to a spring cloud contract?

非 Y 不嫁゛ 提交于 2019-12-24 23:44:14
问题 I'm trying to convert an openAPI specification (written in yaml) into a spring cloud contract YAML DSL. Is there any plugin that could do this type of conversion? 回答1: Yes you can. Please read this blog post on how to encorporate the yaml contract inside the openapi spec https://springframework.guru/defining-spring-cloud-contracts-in-open-api/ 来源: https://stackoverflow.com/questions/56989445/is-there-a-way-to-convert-an-openapi-specification-to-a-spring-cloud-contract

How to use AWS CodeCommit as repository for Spring Cloud Config

爱⌒轻易说出口 提交于 2019-12-24 12:09:13
问题 I am trying to use AWS CodeCommit repository with spring cloud config. I have managed to get it working with SSH. But I would like to use https instead of SSH. AWS suggest to use credential helper. Does anyone know how can I configure spring config cloud to use credential helper? I have looked AWS CodeCommit HTTPS access without setting up credential helper But there is no answer yet and I was wondering if there is some way in spring cloud config to do it. 回答1: I don't know if you have seen

How to capture log on each instance of the microservice through zuul

耗尽温柔 提交于 2019-12-24 11:13:04
问题 I have setup multiple instances of my microservice and registered to my eureka server. It uses ribbon for client side load balancing and uses zuul as gateway server. All usual stuff. I would like to capture the logs of which instance of my service is responding for each request. So that I can able to bring some conclusion based on my usage of each instances. How to do that? 回答1: You can try to set the loglevel just of the LoadBalancerContext to debug in application.properties #logging logging

Hystrix getting access to the current execution state within fallback

丶灬走出姿态 提交于 2019-12-24 10:39:20
问题 I successfully configured spring-cloud (via spring-cloud-starter-hystrix ) to wrap a call to a service. This all works fine and looks like the following: @Component public class MyService { @HystrixCommand(fallbackMethod = "fallback") public void longRunning() { // this could fail } public void fallback() { // fallback code } } My question now is, I would like to log some statistics about the execution error in longRunning() Trying to access HystrixRequestLog.getCurrentRequest() within the

Selecting parameters that should be used by Spring Boot when creating datasource in CF?

99封情书 提交于 2019-12-24 05:48:26
问题 I have an app with Spring Boot (1.3.3.RELEASE)/Cloud (Brixton.M5) with MySQL deployed in Stackato CF, where I use CrudRepository. Unfortunately when the app starts, Spring Boot automatically configures DataSource using one of JDBC urls provided by Stackato, one that includes username and password, e.g. jdbc:mysql://yourUser:yourPassword@172.16.1.48:3306/yourDB Unfortunately apparently then Spring Boot assumes that everything before : is my host, while yourPassword@172.16.1.48:3306 is the port