Java服务需要Bean(接口)
有没有人知道为什么该服务要求豆子?我正在用文章和作者服务构建一个小型微服务。这里的问题在哪里?
我正在为服务之间的通信使用Feign-Reactor-spring-cloud-starter-starter依赖性。
服务:
@Slf4j
@Service
@RequiredArgsConstructor
public class ArticleService{
private final IArticleRepository articleRepository;
private final IAuthorResponse authorResponse;
public Mono<ArticleRequest> createArticle(
ArticleRequest articleRequest,
String authorEmail
){
Mono<AuthorResponse> requestedAuthor = authorResponse
.getAuthorByEmail(authorEmail)
.switchIfEmpty(Mono.error(new BadRequestException(valueOf(AUTHOR_NOT_FOUND))))
.map(author -> new AuthorResponse(
author.firstName(),
author.lastName(),
author.email()))
.log();
Article articleModel = Article.builder()
.articleId(UUID.randomUUID())
.articleTitle(articleRequest.title())
.articleContent(articleRequest.content())
.publishedAt(IDateTimeCreator.createDateTime())
.authorEmail(requestedAuthor.map(AuthorResponse::email).block()) //TODO: implement current loggedIn AuthorEmail
.build();
return articleRepository
.existsArticleByAuthorEmailAndAndArticleTitle(
articleModel.getAuthorEmail(),
articleModel.getArticleTitle())
.flatMap(exists -> {
if (exists) {
return Mono.error(
new BadRequestException(valueOf(ARTICLE_ALREADY_EXISTS)));
} else {
return articleRepository
.save(articleModel)
.map(article -> new ArticleRequest(
article.getArticleTitle(),
article.getArticleContent()))
.log();
}
});
}
}
接口:
@ReactiveFeignClient(name = "authorservice", configuration = ReactiveFeignConfig.class)
public interface IAuthorResponse {
@GetMapping(path = "/api/v1/author/{authorEmail}")
Mono<AuthorResponse> getAuthorByEmail(@PathVariable("authorEmail") String authorEmail);
}
配置
@Configuration
public class ReactiveFeignConfig {
@Bean
public ReactiveLoggerListener loggerListener(){
return new DefaultReactiveLogger(Clock.systemUTC(), LoggerFactory.getLogger("articleservice"));
}
// Here we specify our Exception between the articleservice and authorservice
@Bean
public ReactiveStatusHandler reactiveStatusHandler() {
return ReactiveStatusHandlers.throwOnStatus(
(status) -> (status == 500),
(methodKey, response) ->
new RetryableException(
response.status(),
"Failed. The ArticleService couldn't reach the Authorservice. Please try again!",
null,
Date.from(Instant.EPOCH),
null));
}
// In case of an error such as a simple timeout occurred, we've the opportunity with this methode to try again.
@Bean
public ReactiveRetryPolicy reactiveRetryPolicy() {
return BasicReactiveRetryPolicy.retryWithBackoff(3, 2500);
}
}
这是我的pom.xml的一部分:
<dependency>
<groupId>com.salenaluu</groupId>
<artifactId>clients</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
接口位于没有主类的单独模块中。
主级:
@EnableReactiveFeignClients(basePackages = "com.salenaluu.clients}")
@EnableEurekaClient
@SpringBootApplication
public class ArticleServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ArticleServiceApplication.class,args);
}
}
消息:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 1 of constructor in ...ArticleService required a bean of type '....IAuthorResponse' that could not be found.
Action:
Consider defining a bean of type '....IAuthorResponse' in your configuration.
Process finished with exit code 0
have anyone a idea why the Service is asking for a bean ? I'm building a small microservice with a Article- and Authorservice. Where is the issue here ?
I'm using feign-reactor-spring-cloud-starter dependency for the communication between the services.
Service:
@Slf4j
@Service
@RequiredArgsConstructor
public class ArticleService{
private final IArticleRepository articleRepository;
private final IAuthorResponse authorResponse;
public Mono<ArticleRequest> createArticle(
ArticleRequest articleRequest,
String authorEmail
){
Mono<AuthorResponse> requestedAuthor = authorResponse
.getAuthorByEmail(authorEmail)
.switchIfEmpty(Mono.error(new BadRequestException(valueOf(AUTHOR_NOT_FOUND))))
.map(author -> new AuthorResponse(
author.firstName(),
author.lastName(),
author.email()))
.log();
Article articleModel = Article.builder()
.articleId(UUID.randomUUID())
.articleTitle(articleRequest.title())
.articleContent(articleRequest.content())
.publishedAt(IDateTimeCreator.createDateTime())
.authorEmail(requestedAuthor.map(AuthorResponse::email).block()) //TODO: implement current loggedIn AuthorEmail
.build();
return articleRepository
.existsArticleByAuthorEmailAndAndArticleTitle(
articleModel.getAuthorEmail(),
articleModel.getArticleTitle())
.flatMap(exists -> {
if (exists) {
return Mono.error(
new BadRequestException(valueOf(ARTICLE_ALREADY_EXISTS)));
} else {
return articleRepository
.save(articleModel)
.map(article -> new ArticleRequest(
article.getArticleTitle(),
article.getArticleContent()))
.log();
}
});
}
}
Interface:
@ReactiveFeignClient(name = "authorservice", configuration = ReactiveFeignConfig.class)
public interface IAuthorResponse {
@GetMapping(path = "/api/v1/author/{authorEmail}")
Mono<AuthorResponse> getAuthorByEmail(@PathVariable("authorEmail") String authorEmail);
}
Config
@Configuration
public class ReactiveFeignConfig {
@Bean
public ReactiveLoggerListener loggerListener(){
return new DefaultReactiveLogger(Clock.systemUTC(), LoggerFactory.getLogger("articleservice"));
}
// Here we specify our Exception between the articleservice and authorservice
@Bean
public ReactiveStatusHandler reactiveStatusHandler() {
return ReactiveStatusHandlers.throwOnStatus(
(status) -> (status == 500),
(methodKey, response) ->
new RetryableException(
response.status(),
"Failed. The ArticleService couldn't reach the Authorservice. Please try again!",
null,
Date.from(Instant.EPOCH),
null));
}
// In case of an error such as a simple timeout occurred, we've the opportunity with this methode to try again.
@Bean
public ReactiveRetryPolicy reactiveRetryPolicy() {
return BasicReactiveRetryPolicy.retryWithBackoff(3, 2500);
}
}
This is a part of my pom.xml:
<dependency>
<groupId>com.salenaluu</groupId>
<artifactId>clients</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
The Interface is in a seperate module with no main class.
Mainclass:
@EnableReactiveFeignClients(basePackages = "com.salenaluu.clients}")
@EnableEurekaClient
@SpringBootApplication
public class ArticleServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ArticleServiceApplication.class,args);
}
}
Message:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 1 of constructor in ...ArticleService required a bean of type '....IAuthorResponse' that could not be found.
Action:
Consider defining a bean of type '....IAuthorResponse' in your configuration.
Process finished with exit code 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论