我使用spring-cloud构建了一个spring boot应用程序,并希望在我的客户端应用程序(也是一个微服务)中使用RestTemplate,这样我就可以继续使用mockMvc进行集成测试.我正在使用我正在调用的服务中的客户端微服务和eureka客户端的默认ribbon/eureka/hystrix客户端设置.这是有效的(一旦我发现serviceIds是标识restTemplate中的服务端点的东西).我的问题是,我似乎无法更改restTemplate读取和连接超时,似乎默认为300毫秒.
电话详情:
`@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableConfigurationProperties
@EnableHystrix
@EnableEurekaClient
public class Application { ... public static void main(String[] args) {} ... }
@Component
class EricComponentToDoHystrix { // apparently this has to be a component for hystrix to work btw
@Autowired
RestTemplate restTemplate;
..
@HystrixCommand(fallbackMethod="defaultRestTemplateCall")
public void doRestTemplateCall()
ResponseEntity<String> result = restTemplate.getForEntity("http://someservice/doSomething", String.class); // actually make a call
..
}
}`
Run Code Online (Sandbox Code Playgroud)
使用包含以下内容的application.properties:
spring:
cloud:
client:
serviceIds:
- someservice
someservice:
ribbon:
#listOfServers: localhost:7080
#NIWSServerListClassName: com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList
# the eureka vipAddress of the target …Run Code Online (Sandbox Code Playgroud) 我想为我正在编写的spring cloud微服务编写一些自己的自定义zuul过滤器.一旦我编写了过滤器,我该如何集成它,以便底层的netflix zuul框架可以利用它.
我有这个类来启动spring-cloud配置服务器.这是一个spring-boot应用程序.
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {
public static void main( String[] args ) {
SpringApplication.run( ConfigServerApplication.class, args );
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序运行正常,我所有的单元测试都很好.但是,在我们的竹子管道中,它将初始化一个声纳过程来分析代码.我们不断收到这些小警告,表明以下内容:
Utility classes should not have a public constructor
Run Code Online (Sandbox Code Playgroud)
我知道这是一个小问题,但我的任务是从代码中删除这些.
理想情况下,您可以将类标记为final并提供私有构造函数,或者所有搜索都提供解决方案.但是,Spring Configuration类不能成为final,也不能有私有构造函数.
任何想法如何解决这个问题?
spring spring-boot spring-java-config sonarqube spring-cloud
我目前正面临一个问题,即Eureka从未清除因虚拟机意外停机而变得陈旧的服务实例.可以理解的是,Eureka的自我保护模式已经启动,因为在服务续订/心跳请求中存在大幅下降(低于阈值).然而,15个多小时后,死亡的实例仍在Eureka注册.这是一个主要问题,因为服务请求继续被定向到死实例,仅返回错误.
我的希望是门槛不断调整,经过一段时间后,尤里卡的门槛将处于新的标准水平,自我保护模式将被重置.我们在镜像设置中使用Eureka,我们的配置不是很复杂.
我们的设置:
Eureka通过spring-boot-starter-parent 1.2.5.RELEASE
eureka:
dashboard:
path: services
enabled: false
instance:
hostname: localhost
leaseRenewalIntervalInSeconds: 3
metadataMap:
managementPath: /admin
instanceId: discoveryPrimary
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
waitTimeInMsWhenSyncEmpty: 0
Run Code Online (Sandbox Code Playgroud)
是否可以调整Eureka配置以重置自我保存模式(停止清除实例的位置)并允许它在服务已停止5分钟以上时清除服务注册表?
我想转发带有假装客户端RequestInterceptor的请求标头,但是,在内RequestInterceptor.apply, RequestContextHolder.getRequestAttributes()是null,所以SecurityContextHolder.getContext().getAuthentication()(我最终也可以得到我的标头的值).
这曾经在升级到Spring-Cloud Brixton之前工作,其中hystrix命令现在必须可以在单独的线程中运行,因为更改为以下参数可以解决问题:
hystrix.command.default.execution.isolation.strategy: SEMAPHORE
Run Code Online (Sandbox Code Playgroud)
现在,如果没有必要,我不太热衷于更改这种默认值,是否有另一种推荐的转发标头现在的方式?
谢谢
我有一个Spring Cloud应用程序,我正在自定义功能区客户端,如" 自定义功能区客户端"一节中所述,我的IRule如下所示:
public class HeadersRule extends AbstractLoadBalancerRule {
public HeadersRule () {
}
public HeadersRule(ILoadBalancer lb) {
this();
this.setLoadBalancer(lb);
}
public Server choose(ILoadBalancer lb, Object key) {
//I want the key to contain the headers from the request so I can decide choose the server by one of the headers
}
Run Code Online (Sandbox Code Playgroud)
我有一个休息控制器:
@RequestMapping("/")
public String hello(HttpServletRequest request, HttpServletResponse response) {
//here I want to pass the key parameter to ribbon
return result;
}
Run Code Online (Sandbox Code Playgroud)
我想在我的IRule中按其中一个标题的值选择下一个服务器.如何将标题传递给我的自定义IRule关键参数?(通过RestTemplate或Feign,或者如果您有另一个使用Ribbon的选项...)
编辑可能的方向
在AbstractLoadBalancerAwareClient类中
public T …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring Cloud的Zuul将一些API请求代理到一些外部服务器.代理本身运行良好,但每个服务都需要请求头中提供的(不同)令牌.
我已成功为每个应用适当标头的令牌编写了一个简单的预过滤器.但是,我现在有一个问题.即使在完成文档后,我也无法弄清楚如何使每个过滤器仅适用于正确的路径.我不想在网址随环境变化时执行网址匹配.理想情况下,我有一些方法可以在过滤器中获取路径的名称.
我的application.yml:
zuul:
routes:
foo:
path: /foo/**
url: https://fooserver.com
bar:
path: /bar/**
url: https://barserver.com
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想在FooFilter.java(预过滤器)中执行类似的操作:
public bool shouldFilter() {
return RequestContext.getCurrentContext().getRouteName().equals("foo");
}
Run Code Online (Sandbox Code Playgroud)
但我似乎无法找到任何方法来做到这一点.
我正在使用Spring Cloud Config Server并能够检测来自git存储库的更改并将其传递给Config客户端.
有两种方法,我已经实现了它:
参考:http: //tech.asimio.net/2017/02/02/Refreshable-Configuration-using-Spring-Cloud-Config-Server-Spring-Cloud-Bus-RabbitMQ-and-Git.html
参考:https://spring.io/guides/gs/centralized-configuration/
所以两者都运行良好,那么使用Spring Cloud Bus是否有任何优势,或者在生产环境中,没有Spring Cloud Bus会有任何问题吗?因为在生产中将RabbitMQ集群(HA)设置为Spring Cloud Bus需要额外的努力.
谢谢,大卫
spring-boot spring-cloud spring-cloud-netflix spring-cloud-config
我有一个AWS SQS,其中已有5000条消息在队列中(示例消息看起来像'Hello @ 1')我创建了一个SpringBoot应用程序,并在其中一个组件类中创建了一个从SQS读取消息的方法.
package com.example.aws.sqs.service;
import org.springframework.cloud.aws.messaging.listener.SqsMessageDeletionPolicy;
import org.springframework.cloud.aws.messaging.listener.annotation.SqsListener;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
public class MessageReceiverService {
@SqsListener(value = { "${cloud.aws.sqs.url}" }, deletionPolicy = SqsMessageDeletionPolicy.ALWAYS)
public void readMessage(String message){
log.info("Reading Message... {}", message);
}
Run Code Online (Sandbox Code Playgroud)
}
我的主要SpringBoot类
@SpringBootApplication
public class AwsSqsApplicationConsumer {
public static void main(String[] args) {
SpringApplication.run(AwsSqsApplicationConsumer.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
我运行应用程序时遇到的异常:
s.c.a.m.l.SimpleMessageListenerContainer : An Exception occurred while polling queue '<my sqs name>'. The failing operation will be retried in 10000 milliseconds
org.springframework.core.task.TaskRejectedException: Executor [java.util.concurrent.ThreadPoolExecutor@7c1594a5[Running, pool size …Run Code Online (Sandbox Code Playgroud) 我的应用程序启动时遇到问题。在ClassCastException上配置日志级别时,应用程序失败java.util.HashMap cannot be cast to org.springframework.boot.logging.LogLevel。
我尝试logging.level.root=INFO在我的设置,application.properties并发生相同的错误。不知道还有什么尝试。
我刚刚将其升级到SpringBoot 2.x,特别是使用了此依赖项BOM:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springframework.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Run Code Online (Sandbox Code Playgroud)
请找到依赖关系树的相关部分:
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.2.0.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.2.0.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot:jar:2.2.0.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.2.0.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.2.0.RELEASE:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.12.1:compile
[INFO] | | | | \- org.apache.logging.log4j:log4j-api:jar:2.12.1:compile
[INFO] | | | \- …Run Code Online (Sandbox Code Playgroud) spring-cloud ×10
spring-boot ×5
java ×3
spring ×3
netflix-zuul ×2
amazon-sqs ×1
netflix ×1
sonarqube ×1
spring-mvc ×1