我们最近从版本 2.0.0 升级到,在调用使用并且也具有Content-Typespring-cloud-starter-openfeign: 2.2.2 的 REST API 时遇到以下异常:consumes@RequestHeader
feign.FeignException$UnsupportedMediaType: [415] during [POST] to [http://localhost:8080/test]
[TestFeign#test(TestRequest,String)]: [{"timestamp":"2021-01-03T13:13:58.192+0000",
"status":415,"error":"Unsupported Media Type",
"message":"Invalid mime type \"application/json, application/json\":
Invalid token character ',' in token \"json, application/json\"","path":"/test"}]
Run Code Online (Sandbox Code Playgroud)
@RequestMapping(value = "/test",method = RequestMethod.POST,consumes= {"application/json"},
produces={"application/json"})
void test(@RequestBody TestRequest request,@RequestHeader("Content-Type") String contentType);
Run Code Online (Sandbox Code Playgroud)
我们进行了调查并发现,在这种情况下,两个值都按如下方式连接:
Content-Type: "application/json, application/json"
Run Code Online (Sandbox Code Playgroud)
使用时spring-cloud-starter-openfeign:2.0.0不会出现此问题。
我在 Spring Boot 2.3.4 中有以下工作代码
public Route.AsyncBuilder apply(PredicateSpec route) {
return route
.path("/api/external/**")
.filters(f -> f
.removeRequestHeader("Cookie")
.filter(loggingGatewayFilter)
.filter(oauth2GatewayFilter)
.filter(this::filter)
)
.uri("lb://external-url");
}
Run Code Online (Sandbox Code Playgroud)
配置:
external-url:
ribbon:
listOfServers: "http://external01.com,http://external02.com"
NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
Run Code Online (Sandbox Code Playgroud)
当我将 Spring Boot 版本升级到 2.4.2 时,代码必须更改为以下内容。
public Buildable<Route> apply(PredicateSpec route) {
return route
.path("/api/external/**")
.filters(f -> f
.removeRequestHeader("Cookie")
.filter(loggingGatewayFilter)
.filter(oauth2GatewayFilter)
.filter(this::filter)
)
.uri("lb://external-url");
}
Run Code Online (Sandbox Code Playgroud)
由于 netflix-ribbon 已在 Spring Boot 2.4.2 中删除,我必须添加这些依赖项。在路由中它给出以下警告。这意味着负载均衡器未初始化。
o.s.c.l.core.RoundRobinLoadBalancer : No servers available for service: external-url
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题?我可以尝试将其迁移到弹簧负载平衡器中吗?
升级后的依赖树:
[INFO] +- org.apache.commons:commons-lang3:jar:3.9:compile
[INFO] +- com.internetitem:logback-elasticsearch-appender:jar:1.6:compile …Run Code Online (Sandbox Code Playgroud) spring-boot spring-cloud spring-cloud-netflix spring-cloud-gateway spring-cloud-loadbalancer
我有一个应用程序,我们正在迁移到最新的 Spring Boot(2.4.2) 和 Spring Cloud(2020.0.1) 版本。该应用程序使用云配置服务器来获取配置,并且刷新是通过调用的预定作业来完成的RefreshEndpoint.refresh()。
这曾经工作得很好,但对于上面的版本,我无法让它工作。
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in io.divinedragon.spring.boot2.ConfigRefreshJob required a bean of type
'org.springframework.cloud.endpoint.RefreshEndpoint' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.cloud.endpoint.RefreshEndpoint' in your configuration.
Run Code Online (Sandbox Code Playgroud)
这是具有以下配置的项目。
pom.xml***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in io.divinedragon.spring.boot2.ConfigRefreshJob required a bean of type
'org.springframework.cloud.endpoint.RefreshEndpoint' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.cloud.endpoint.RefreshEndpoint' in …Run Code Online (Sandbox Code Playgroud) 我正在尝试调试我的应用程序,但调试器没有命中此代码块。这是我的自定义网关过滤器。
@RefreshScope
@Component
public class AuthorizationHeaderFilter extends
AbstractGatewayFilterFactory<AuthorizationHeaderFilter.Config> {
@Autowired
Environment environment;
public AuthorizationHeaderFilter () {
super(Config.class);
}
public static class Config {
}
@Override
public GatewayFilter apply(Config config) {
return ((exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest();
if(!request.getHeaders().containsKey(HttpHeaders.AUTHORIZATION)) {
return onError(exchange, "No authorization header", HttpStatus.UNAUTHORIZED);
}
String token = Objects.requireNonNull(request.getHeaders().get(HttpHeaders.AUTHORIZATION))
.get(0).replace("Bearer","");
if(isJwtValid(token))
return onError(exchange, "JWT Token is not valid", HttpStatus.UNAUTHORIZED);
return chain.filter(exchange);
});
}
private Mono<Void> onError(ServerWebExchange exchange, String error, HttpStatus status) {
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(status); …Run Code Online (Sandbox Code Playgroud) 我正在使用 spring boot 实现 SQS 订阅者,经过在互联网上的一些研究,我找到了该项目spring-cloud。
使用注释@SqsListener看起来很容易从主题接收消息,但我想将其实现为长池,而不是短池,后者在每条消息到达时都会接收消息。
@SqsListener(
value = ["queue"],
deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS
)
fun subscribeToSSmsg: String) {
....
}
Run Code Online (Sandbox Code Playgroud)
这工作顺利,但我想使用长池接收消息。有什么办法可以使用 spring-cloud 来做到这一点吗?
我有一个应用程序,当我调用其服务之一时,它会返回以下 JSON:
{
"suppliers": [
{
"name": "Joaquim",
"email": "joaquim@email.com",
},
{
"name": "Manoel",
"email": "manoel@email.com",
}
]
}
Run Code Online (Sandbox Code Playgroud)
为了访问这些数据,我使用具有以下方法的 Feign 客户端:
@FeignClient(name = "suppliersClient", url = "www.url-example.com.br")
// ...
List<SuppliersDTO> searchSuppliers(@RequestParam("city") String city);
Run Code Online (Sandbox Code Playgroud)
根据城市的不同,它会返回供应商列表。这段代码是一个简单的例子,向你解释我想要做什么。
除了和之外,我的 DTO Supplys 类还具有name和字段。emailgetterssetters
public class SuppliersDTO {
String name;
String email;
// GETTERS AND SETTERS
Run Code Online (Sandbox Code Playgroud)
使用这样的代码,当使用 Feign 客户端时,我收到 500 内部服务器错误,其中包含以下信息:
"Error while extracting response for type [java.util.List<com.domain.store.dto.SuppliersDTO>] and content type [application/json;charset=utf-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot …Run Code Online (Sandbox Code Playgroud) 我想在我的配置服务器中使用私有 git 存储库。这是我的application.yml:
server:
port: 100
spring:
application:
name: smth-config-server
cloud:
config:
server:
git:
uri: https://github.com/smth/smth
default-label: main
username: smth
password: smth
host-key-algorithm: ssh-rsa
ignore-local-ssh-settings: true
host-key: ssh-rsa smth== github.com
private-key: -----BEGIN RSA PRIVATE KEY-----
smth
-----END RSA PRIVATE KEY-----
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Caused by: org.eclipse.jgit.errors.TransportException: https://github.com/smth/smth: not authorized
at org.eclipse.jgit.transport.TransportHttp.connect(TransportHttp.java:544)
Run Code Online (Sandbox Code Playgroud)
我应该如何修复它?
git spring spring-boot spring-cloud spring-cloud-config-server
我有一个流处理器,用于处理从 KafkaInputTopic到OutputTopic. 此外,我有多个租户需要进行此处理。我们将它们称为租户 A 和租户 B,但应用程序可能需要处理十多个租户。输入和输出主题遵循命名约定:A-输入,B-输入,...和A-输出,B-输出...
函数定义如下:
@Configuration
public class StreamProcessorConfig {
@Bean
public Function<KStream<String, InputType>, KStream<String, OutputType>> myfunctiondefinition() {
return inputTypeStream -> inputTypeStream.map((String k, InputType v) -> {
return KeyValue.pair(k, OutputType.createFrom(v));
});
}
}
Run Code Online (Sandbox Code Playgroud)
我的 application.yaml 现在为租户 A 配置流应用程序:
tenant: A
spring.cloud.function.definition: myfunctiondefinition
spring.cloud.stream.kafka.streams.binder.functions.myfunctiondefinition:
applicationId: ${spring.application.name}-myfunctiondefinition
spring.cloud.stream.bindings.myfunctiondefinition-in-0:
destination: ${tenant}-input
spring.cloud.stream.bindings.myfunctiondefinition-out-0:
destination: ${tenant}-output
Run Code Online (Sandbox Code Playgroud)
如何修改配置为租户B添加实例?当然,我可以复制 myfunctiondefinition() 以及所有配置键,但我正在寻找一种仅通过配置即可快速且干净地动态添加租户的方法。这可能吗?
注意:遗憾的是,无法为租户 B 和其他租户运行应用程序的另一个实例。
java spring-cloud spring-cloud-stream apache-kafka-streams spring-kafka
我想使用一个PropertySourceLocator必须在bootstrap期间注册的假装客户端.我有以下内容spring.factories:
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
test.PropertiesConfiguration
Run Code Online (Sandbox Code Playgroud)
同
@Configuration
@EnableFeignClients
public class PropertiesConfiguration {
@Bean
public PropertySourceLocator propertySourceLocator() {
return new MyPropertySourceLocator();
}
}
Run Code Online (Sandbox Code Playgroud)
假装客户端自动装入MyPropertySourceLocator.但是,虽然同一个客户端在常规bean中工作,但它在这里不起作用.所有请求都因此异常而失败:
feign.codec.DecodeException: No qualifying bean of type [org.springframework.boot.autoconfigure.web.HttpMessageConverters] 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)}
at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:173)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:141)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:96)
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:92)
at com.sun.proxy.$Proxy23.get(Unknown Source)
at test.MyPropertySourceLocator$1.getProperty(MyPropertySourceLocator.java:32)
at org.springframework.core.env.CompositePropertySource.getProperty(CompositePropertySource.java:59)
at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:84)
at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:60)
at org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:511)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:135)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:132)
at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:84) …Run Code Online (Sandbox Code Playgroud) 我正在使用微服务架构构建Rest api.我有多个api用户,我们已经进入多个项目.除了我无法将面向用户的用户映射到zuul中的应用程序URL之外,我还准备好了其他所有内容.
面向网址的用户是:user/v1/accountholders/{id}/cards,我的应用程序的实际网址是/ user-cards/v1/accountholders/{id}/cards.
这里的id是路径变量.下面是其他类似的api url,所以如果有一种方法在zuul中一般配置它们.此外,应用程序URL的上下文根也是Eureka中的项目名称.
Other similar urls are:
client side:- /user/v1/accountholders/{id}/cards/{cardid}
application:- /user-cards/v1/accountholders/{id}/cards/{cardid}
client side:- /user/v1/accountholders
application:- /user-cardholder/v1/accountholder
client side:- /user/v1/accountholders
application:- /user-cardholder/v1/accountholder
client side:- /user/v1/accountholders/{id}
application:- /user-cardholder/v1/accountholders/{id}
client side:- /user/v1/accountholders/{id}/accounts
application:- /user-accounts/v1/accountholders/{id}/accounts
client side:- /user/v1/accountholders/{id}/accounts/{accid}
application:- /user-accounts/v1/accountholders/{id}/accounts/{accid}
Run Code Online (Sandbox Code Playgroud)
需要一些帮助来在zuul的属性或yml文件中进行设置.我还没有能够在映射中取得任何进展.任何输入都会有所帮助.
解决: - 从@Daniel获得输入后(这是接受的答案)这是我在zuul配置中使用的: -
zuul:
routes:
User-Cards:
path: /user/v1/accountholders/*/cards/**
url: http://desktop-uvkv1ed:9999/user-cards/v1/accountholders
User-Transactions1:
path: /user/v1/accountholders/*/transactions
url: http://desktop-uvkv1ed:5555/user-transactions/v1/accountholders
service-id: User-Transactions
User-Transactions2:
path: /user/v1/accountholders/*/accounts/*/transactions
url: http://desktop-uvkv1ed:5555/user-transactions/v1/accountholders
service-id: User-Transactions
User-Accounts:
path: /user/v1/accountholders/*/accounts/**
url: http://desktop-uvkv1ed:7777/user-accounts/v1/accountholders
User-Cardholders:
path: /user/v1/accountholders/**
url: http://desktop-uvkv1ed:8888/user-cardholders/v1/accountholders
Run Code Online (Sandbox Code Playgroud) spring-boot microservices spring-cloud netflix-eureka netflix-zuul
spring-cloud ×10
spring-boot ×7
java ×4
spring ×4
amazon-sqs ×1
feign ×1
git ×1
netflix-zuul ×1
openfeign ×1
spring-kafka ×1
spring-mvc ×1