我的目标是创建一个不同步骤的策略,以便使用eureka,ribbon,hystrix从2个组件之间的点对点通信到"完全成熟的netflix"通信方式.每次迭代我想添加更多,同时我尝试限制实际代码的更改量.假设是我首选的客户端框架来实现这一目标.第一步是创建一个FeignClient来与服务器通信:
@FeignClient(url = "http://localhost:9000")
interface Client {
@RequestMapping(method = RequestMethod.GET, value = "/author/{author}/addedValue/{addedValue}")
Result addToTotal(@RequestParam(value="author") String author, @RequestParam(value="addedValue") long addedValue);
}
Run Code Online (Sandbox Code Playgroud)
这有效,但我不希望在注释中对URL进行硬编码.我想这个:@FeignClient()并有一个属性构造,如:client.url:http:// localhost:9000
到目前为止,我找不到任何关于如何配置的线索,我无法在spring-cloud源中找到解决方案.
它可以做到,如果是的话; 怎么样?
背景
我想实现本文中介绍的设计.
到目前为止我有什么
我完成了大部分工作:
我还编写了一个Zuul PRE过滤器,用于检查访问令牌,联系IDP并创建JWT.然后将JWT添加到转发到下游服务的请求的标头中.
问题
现在我的问题非常具体针对Zuul及其过滤器.如果由于任何原因在API网关中验证失败,我怎样才能停止路由并直接用401响应而不继续过滤链并转发呼叫?
如果验证失败,过滤器将不会将JWT添加到标头,401将来自下游服务.我希望我的网关可以阻止这种不必要的通话.
我试着看看我怎么com.netflix.zuul.context.RequestContext用来做这个,但文档很差,我找不到办法.
我想知道Springboot和Springcloud之间是否存在兼容性矩阵?
我在STS上创建了一个简单的项目,并遇到兼容性问题.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Run Code Online (Sandbox Code Playgroud)
充分了解Springboot和Springcloud的受支持版本将使事情变得更简单.
我确实观察到了一个涉及这种需求的公开问题 - https://github.com/spring-cloud/spring-cloud-build/issues/43
社区是否有办法在他们开始使用SpringCloud项目时如何选择开始使用特定版本组合?
编辑:添加我昨天遇到的这样一个问题的另一个实例
来自pom.xml的片段
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<!--<version>1.5.2.RELEASE</version> -->
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<!-- <version>Camden.SR6</version -->
<!-- <version>Brixton.SR5</version> -->
<!-- <version>Camden.SR6</version> -->
<version>Camden.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Run Code Online (Sandbox Code Playgroud)
使用带有Camden.SR5的1.5.2.RELEASE时没有maven错误,但是当应用程序启动时会抛出NoClassDefFoundError.
2017-03-28 09:51:15.148 ERROR 15808 --- [ main] o.s.boot.SpringApplication : Application startup …Run Code Online (Sandbox Code Playgroud) 我无法从另一个项目自动连接假装客户端.似乎没有生成和注入假装客户端的实现.
这是我得到的错误.
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'passportRestController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private com.wstrater.service.contacts.client.ContactService com.wstrater.service.passport.server.controllers.PassportRestController.contactService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [com.wstrater.service.contacts.client.ContactService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
Run Code Online (Sandbox Code Playgroud)
假装客户非常直截了当.为简洁起见,我删除了导入.
package com.wstrater.service.contacts.client;
@FeignClient("contact-service")
public interface ContactService {
@RequestMapping(method = RequestMethod.GET, value = ContactConstants.CONTACTS_USER_ID_PATH)
public Collection<Contact> contactsByUserId(@PathVariable("userId") String userId);
}
Run Code Online (Sandbox Code Playgroud)
我将组件扫描添加到我的项目中以包含应用程序及其控制器,并将feign客户端包含在另一个项目中.
package com.wstrater.service.passport.server;
@EnableEurekaClient
@EnableFeignClients
@SpringCloudApplication …Run Code Online (Sandbox Code Playgroud) 我试图让spring cloud与使用自动配置的消息一起工作.
我的属性文件包含:
cloud.aws.credentials.accessKey=xxxxxxxxxx
cloud.aws.credentials.secretKey=xxxxxxxxxx
cloud.aws.region.static=us-west-2
Run Code Online (Sandbox Code Playgroud)
我的Configuration类如下:
@EnableSqs
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
我的听众课程:
@RestController公共类OrderListener {
@RestController
public class OrderListener {
@MessageMapping("orderQueue")
public void orderListener(Order order){
System.out.println("Order Name " + order.getName());
System.out.println("Order Url" + order.getUrl());
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行这个.我收到以下错误:
org.springframework.context.ApplicationContextException: Failed to start bean 'simpleMessageListenerContainer'; nested exception is org.springframework.messaging.core.DestinationResolutionException: The specified queue does not exist for this wsdl version. (Service: AmazonSQS; Status Code: 400; Error Code: AWS.SimpleQueueService.NonExistentQueue; Request …Run Code Online (Sandbox Code Playgroud) spring annotations amazon-sqs amazon-web-services spring-cloud
我正在尝试运行Spring Boot(使用Spring Cloud)+ Eureka Server + Hystrix Dashboard和Turbine流,但是我遇到了一个问题,到目前为止我找不到任何解决方案.我使用Spring Boot 1.2.1.RELEASE和Spring Cloud 1.0.0.RC2.这是我有的:
第一个实例是运行Eureka服务器和Hystrix仪表板:
@Configuration
@EnableAutoConfiguration
@EnableEurekaServer
@EnableHystrixDashboard
@EnableDiscoveryClient
class Application {
public static void main(String[] args) {
SpringApplication.run Application, args
}
}
Run Code Online (Sandbox Code Playgroud)
在这里你可以找到build.gradle该实例 - https://gist.github.com/wololock/570272ad7cf2d14a4d3c
Eureka服务器运行正常,我可以在eureka服务器仪表板上看到已注册的实例,我也可以LoadBalancer使用其ID来获取已注册实例的URL.到目前为止一切都很好.
我有一些使用@EnableHystrix注释运行的实例,@HystrixCommand用于定义Hystrix必须监视哪些方法.当我将URL传递给单个实例的hystrix.stream到Hystrix仪表板时,我可以看到它运行没有问题.
我也有单独的Turbine服务器,而不是复杂的服务器:
@EnableAutoConfiguration
@EnableTurbine
@Configuration
@EnableDiscoveryClient
class Application {
public static void main(String[] args) {
SpringApplication.run Application, args
}
}
Run Code Online (Sandbox Code Playgroud)
在这里您可以找到build.gradleTurbine服务器实例 - https://gist.github.com/wololock/ff0d855b8a890232851e
它使用非常简单的配置,主要基于样本涡轮应用程序提供的配置 - https://github.com/spring-cloud-samples/turbine
info:
component: Turbine
endpoints:
restart:
enabled: …Run Code Online (Sandbox Code Playgroud) 我正在阅读 Spring Cloud 和 NetFlix API。很多地方,我读到了 Fault Tolerance 和 Fault Resilience 关键词。
请解释区别。
我正在尝试在我正在处理的项目中使用 Spring Cloud 的 AWS SQS。目前,我仅在我的开发机器上本地运行该应用程序。因此,我想要的是连接到 AWS 上的 SQS,而不必将我的应用程序部署到 EC2 实例。
但是,似乎 Spring Cloud 的 AWS 包中使用的 AWS SDK 将尝试通过元数据进行身份验证并希望解析 169.254.169.254/latest/meta-data/instance-id. 由于我仍在本地运行应用程序,因此无法解析端点并引发错误:
2019-12-29 16:38:27.420 WARN 22462 --- [ restartedMain] com.amazonaws.util.EC2MetadataUtils : Unable to retrieve the requested metadata (/latest/meta-data/instance-id). Failed to connect to service endpoint:
com.amazonaws.SdkClientException: Failed to connect to service endpoint:
at com.amazonaws.internal.EC2ResourceFetcher.doReadResource(EC2ResourceFetcher.java:100) ~[aws-java-sdk-core-1.11.699.jar:na]
at com.amazonaws.internal.EC2ResourceFetcher.doReadResource(EC2ResourceFetcher.java:70) ~[aws-java-sdk-core-1.11.699.jar:na]
at com.amazonaws.internal.InstanceMetadataServiceResourceFetcher.readResource(InstanceMetadataServiceResourceFetcher.java:75) ~[aws-java-sdk-core-1.11.699.jar:na]
at com.amazonaws.internal.EC2ResourceFetcher.readResource(EC2ResourceFetcher.java:62) ~[aws-java-sdk-core-1.11.699.jar:na]
at com.amazonaws.util.EC2MetadataUtils.getItems(EC2MetadataUtils.java:400) ~[aws-java-sdk-core-1.11.699.jar:na]
at com.amazonaws.util.EC2MetadataUtils.getData(EC2MetadataUtils.java:369) ~[aws-java-sdk-core-1.11.699.jar:na]
at org.springframework.cloud.aws.context.support.env.AwsCloudEnvironmentCheckUtils.isRunningOnCloudEnvironment(AwsCloudEnvironmentCheckUtils.java:38) ~[spring-cloud-aws-context-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.cloud.aws.context.annotation.OnAwsCloudEnvironmentCondition.matches(OnAwsCloudEnvironmentCondition.java:37) ~[spring-cloud-aws-context-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:108) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at …Run Code Online (Sandbox Code Playgroud) java amazon-sqs amazon-web-services spring-boot spring-cloud
我有一个配置了@EnableResourceServer注释的资源服务器,它通过user-info-uri参数引用授权服务器,如下所示:
security:
oauth2:
resource:
user-info-uri: http://localhost:9001/user
Run Code Online (Sandbox Code Playgroud)
授权服务器/用户端点返回的扩展名org.springframework.security.core.userdetails.User包含例如电子邮件:
{
"password":null,
"username":"myuser",
...
"email":"me@company.com"
}
Run Code Online (Sandbox Code Playgroud)
每当访问某个资源服务器端点时,Spring通过调用授权服务器的/user端点来验证后台的访问令牌,它实际上会返回丰富的用户信息(其中包含例如电子邮件信息,我已经使用Wireshark验证了该信息).
所以问题是如何在没有明确第二次调用授权服务器/user端点的情况下获取此自定义用户信息.Spring是否在授权后将其存储在资源服务器本地的某个位置,或者如果没有任何可用的开箱即用,那么实现此类用户信息的最佳方式是什么?
spring spring-security spring-boot spring-cloud spring-oauth2
我在Zuul有一个场景,URL路由的服务也可能已关闭.因此,响应主体在JSON主体响应中被抛出500 HTTP Status和ZuulException.
{
"timestamp": 1459973637928,
"status": 500,
"error": "Internal Server Error",
"exception": "com.netflix.zuul.exception.ZuulException",
"message": "Forwarding error"
}
Run Code Online (Sandbox Code Playgroud)
我想要做的就是自定义或删除JSON响应,并可能更改HTTP状态代码.
我试图用@ControllerAdvice创建一个异常处理程序,但处理程序没有抓住异常.
更新:
所以我扩展了Zuul过滤器,我可以看到它在执行错误后进入run方法,然后如何更改响应.以下是我到目前为止所得到的.我在某处读到了有关SendErrorFilter的内容,但我如何实现它以及它做了什么?
public class CustomFilter extends ZuulFilter {
@Override
public String filterType() {
return "post";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
final RequestContext ctx = RequestContext.getCurrentContext();
final HttpServletResponse response = ctx.getResponse();
if (HttpStatus.INTERNAL_SERVER_ERROR.value() == ctx.getResponse().getStatus()) {
try {
response.sendError(404, "Error Error"); //trying to change …Run Code Online (Sandbox Code Playgroud) spring-cloud ×10
spring-boot ×6
java ×3
spring ×3
amazon-sqs ×2
netflix-zuul ×2
annotations ×1
fault ×1
gateway ×1
groovy ×1
hystrix ×1
turbine ×1
version ×1