我想为我正在编写的spring cloud微服务编写一些自己的自定义zuul过滤器.一旦我编写了过滤器,我该如何集成它,以便底层的netflix zuul框架可以利用它.
我想转发带有假装客户端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云监听AWS SQS队列,如下所示:
@SqsListener(value = "${queue.name}", deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
public void queueListener(String message, @Headers Map<String, Object> sqsHeaders) {
// code
}
Run Code Online (Sandbox Code Playgroud)
Spring配置:
<aws-messaging:annotation-driven-queue-listener
max-number-of-messages="10" wait-time-out="20" visibility-timeout="3600"
amazon-sqs="awsSqsClient" />
Run Code Online (Sandbox Code Playgroud)
AwsSqsClient:
@Bean
public com.amazonaws.services.sqs.AmazonSQSAsyncClient awsSqsClient() {
ExecutorService executorService = Executors.newFixedThreadPool(10);
return new AmazonSQSAsyncClient(new DefaultAWSCredentialsProviderChain(), executorService);
}
Run Code Online (Sandbox Code Playgroud)
这很好用.
在SQS客户端中配置了10个线程来处理这些消息,如上所示.这也工作正常,在任何时间点处理最多10条消息.
问题是,我无法弄清楚控制轮询间隔的方法.默认情况下,一旦所有线程都空闲,弹出轮询.
即考虑以下示例
与此同时,大约有25条消息被送到队列.在完成之前传递的所有3条消息之前,Spring不会轮询队列.从上面的例子来看,只有20分钟之后春季民意调查,尽管有7个线程仍然免费!
知道我们如何控制这种民意调查吗?即如果有任何线程空闲,则应该开始轮询,并且不应该等到所有线程都空闲
message-queue amazon-sqs amazon-web-services aws-sdk spring-cloud
我正在使用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
我的应用程序启动时遇到问题。在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) 我正在尝试使用 Zuul 和 Consul 创建一个 Spring 云微服务应用程序。
我的项目中有 2 个组件:
使用 Zuul 的 api-gateway 微服务
Hello world 微服务(一个简单的 hello world Rest Webservice)
这是 api-gateway 的代码:
@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
pom.xml
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.M3</version>
</parent>
<properties>
<java.version>1.8</java.version>
<spring.cloud.consul.version>1.0.0.M4</spring.cloud.consul.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<dependency>
<!-- Setup Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<!-- Setup Spring MVC & REST, use Embedded Tomcat -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> …Run Code Online (Sandbox Code Playgroud) 我正在开发Spring Cloud Eureka微服务应用程序.我希望我的服务通过Eureka优先方法连接到配置服务.微服务被打包为docker容器并通过docker-compose进行部署.该申请由以下人员组成:
myapp-service-registry:使用Spring Cloud Eureka实现的服务注册服务myapp-config-service:Spring Cloud配置服务服务器myapp-service-test:一个示例微服务,它应该尝试通过Eureka优先方法连接到配置服务来获取配置数据. 与配置服务的连接失败,如下所述.首先是一些配置数据:
这里myapp-service-registry的application.yml:
server:
port: ${PORT:8761}
eureka:
client:
registerWithEureka: false
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0
Run Code Online (Sandbox Code Playgroud)
这是myapp-config-service's application.yml:
server:
port: ${MYAPP_CONFIG_SERVICE_PORT:8888}
spring:
cloud:
config:
server:
git:
uri: ${MYAPP_CONFIG_SERVICE_GIT_URI}
config:
name: myapp-config-service
# eureka service registry client
eureka:
client:
serviceUrl:
defaultZone: http://${SERVICE_REGISTRY_HOST}:${SERVICE_REGISTRY_PORT}/eureka/
instance:
preferIpAddress: true
Run Code Online (Sandbox Code Playgroud)
配置服务器和客户端初始化为https://github.com/spring-cloud-samples/tests中的样本configserver-eureka和eureka-first样本:
myapp-config-service的bootstrap.yml是:
spring:
application:
name: myapp-config-service
cloud:
config:
discovery:
enabled: …Run Code Online (Sandbox Code Playgroud) spring spring-boot microservices spring-cloud netflix-eureka
我正在尝试使用 Spring-Cloud-Contract 定义 CDC 合同,如下所示:
org.springframework.cloud.contract.spec.Contract.make {
request {
method 'GET'
url $(client(~/\/categories\?publication=[a-zA-Z-_]+?/), server('/categories?publication=DMO'))
}
response {
status 200
headers {
header('Content-Type', 'application/json;charset=UTF-8')
}
body """\
[{
"code": "${value(client('DagKrant'), server(~/[a-zA-Z0-9_-]*/))}",
"name": "${value(client('De Morgen Krant'), server(~/[a-zA-Z0-9_\- ]*/))}",
"sections" : []
},
{
"code": "${value(client('WeekendKrant'), server(~/[a-zA-Z0-9_-]*/))}",
"name": "${value(client('De Morgen Weekend'), server(~/[a-zA-Z0-9_\- ]*/))}",
"sections" : [
{
"id" : "${value(client('a984e824'), server(~/[0-9a-f]{8}/))}",
"name" : "${value(client('Binnenland'), server(~/[a-zA-Z0-9_\- ]*/))}"
}
]
}]
"""
}
}
Run Code Online (Sandbox Code Playgroud)
在生成的测试中,这会导致以下断言:
DocumentContext parsedJson = JsonPath.parse(response.getBody().asString());
assertThatJson(parsedJson).array().contains("code").matches("[a-zA-Z0-9_-]*");
assertThatJson(parsedJson).array().array("sections").contains("id").matches("([0-9a-f]{8})?");
assertThatJson(parsedJson).array().array("sections").contains("name").matches("[a-zA-Z0-9_\\- ]*");
assertThatJson(parsedJson).array().contains("name").matches("[a-zA-Z0-9_\\- ]*"); …Run Code Online (Sandbox Code Playgroud) 我对这个配置有点困惑。我的 Spring Boot 应用程序@EnableDiscoveryClient已spring.cloud.consul.host设置为localhost. 我正在运行我的引导应用程序的主机上运行 Consul 代理,但我有几个问题(似乎无法在文档中找到我的答案)。
这个配置可以接受多个值吗?
@EnableDiscoveryClient?(这也感觉不对;一方面,即使一个代理应该能够告诉集群的所有信息,这似乎也是一个单点故障;如果我无法联系到这个代理怎么办?)我正在尝试使用 spring cloud AWS 将 AWS SQS 集成到我的 springboot 应用程序中,但不断收到此错误(在下面发布),有人可以帮忙吗?
这是我的文件。
org.springframework.beans.factory.BeanCreationException:创建名为“simpleMessageListenerContainer”的 bean 在类路径资源 [org/springframework/cloud/aws/messaging/config/annotation/SqsConfiguration.class] 中定义时出错:调用 init 方法失败;嵌套异常是 java.lang.NoSuchMethodError: com.amazonaws.http.ExecutionContext.setCredentials(Lcom/amazonaws/auth/AWSCredentials;)V
@Configuration
public class AWSConfig {
@Value("${amazon.dynamodb.endpoint}")
private String amazonDynamoDBEndpoint;
@Value("${amazon.aws.accesskey}")
private String amazonAWSAccessKey;
@Value("${amazon.aws.secretkey}")
private String amazonAWSSecretKey;
@Value("${amazon.sqs.endpoint}")
private String amazonSqsEndpoint;
@Bean
@Primary
public AmazonSQSAsyncClient amazonSQSAsyncClient() {
AmazonSQSAsyncClient amazonSQSAsyncClient = new AmazonSQSAsyncClient(amazonAWSCredentials());
if (!StringUtils.isEmpty(amazonSqsEndpoint)) {
amazonSQSAsyncClient.setEndpoint(amazonSqsEndpoint);
}
return amazonSQSAsyncClient;
}
@Bean
public AWSCredentials amazonAWSCredentials() {
return new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey);
}
}
Run Code Online (Sandbox Code Playgroud)
我可以与 dynamodb 一起工作,但无法连接到 SQS。我已经在 application.properties 文件中提供了正确的访问密钥、秘密访问密钥和端点。
@Component
@EnableSqs
public …Run Code Online (Sandbox Code Playgroud) spring amazon-sqs amazon-web-services spring-boot spring-cloud
spring-cloud ×10
spring-boot ×6
spring ×3
amazon-sqs ×2
consul ×2
netflix-zuul ×2
aws-sdk ×1
java ×1