如何解决Spring boot Web Client:找不到主机
我正在尝试使用 netflix-eureka 和 webflux 运行 spring-bot 微服务,但遇到以下问题:
当我尝试在我的其他微服务之一上使用 webClient 启动请求时,它失败了,但它可以与 rest 模板一起使用。
我的 pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.my.company.invoise</groupId>
<artifactId>invoice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>invoice</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2020.0.0</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5</artifactId>
</dependency>
<dependency>
<groupId>com.mycompany.invoise</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
我的运行文件(InvoiceApplication.java):
@SpringBootApplication
@EntityScan("com.mycompany.invoise.core.entity.invoice")
public class InvoiceApplication {
public static void main(String[] args) {
SpringApplication.run(InvoiceApplication.class,args);
}
@Bean
public Hibernate5Module dataTypeHibernateModule(){
Hibernate5Module module = new Hibernate5Module();
module.disable(Hibernate5Module.Feature.USE_TRANSIENT_ANNOTATION);
module.enable(Hibernate5Module.Feature.SERIALIZE_IDENTIFIER_FOR_LAZY_NOT_LOADED_OBJECTS);
return module;
}
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@Bean
@LoadBalanced
public WebClient.Builder getWebClientBuilder(){
return WebClient.builder();
}
}
我的 API (InvoiceRessource.java) :
@RestController
@RequestMapping("/invoice")
public class InvoiceResource {
@Autowired
private IInvoiceService invoiceService ;
@Autowired
private RestTemplate restTemplate ;
@Autowired
private WebClient.Builder wBuilder ;
public IInvoiceService getInvoiceService() {
return invoiceService;
}
public void setInvoiceService(IInvoiceService invoiceService) {
this.invoiceService = invoiceService;
}
@PostMapping("")
public Invoice create(@RequestBody Invoice invoice){
invoiceService.createInvoice(invoice);
return invoice ;
}
@GetMapping("")
public ParallelFlux<Invoice> list(){
System.out.println("You just called list method");
Iterable<Invoice> invoices = invoiceService.getInvoiceList();
List<Mono<Invoice>> invoiceMonos = new ArrayList<>();
final WebClient webClient = wBuilder.baseUrl("http://customer-service").build();
invoices.forEach(invoice ->
//{
invoiceMonos.add(webClient.get()
.uri("/customer/" + invoice.getIdCustomer())
.retrieve()
.bodyToMono(Customer.class)
.map(customer -> {
invoice.setCustomer(customer);
return invoice ;
}))
// invoice.setCustomer(restTemplate.getForObject(
// "http://customer-service/customer/" + invoice.getIdCustomer(),// Customer.class
// ));}
// This version works !
);
Flux<Invoice> invoiceFlux = Flux.concat(invoiceMonos);
return invoiceFlux.parallel().runOn(Schedulers.boundedElastic());
//return invoices ;
}
@GetMapping("/{id}")
public @ResponseBody Invoice displayInvoice(@PathVariable("id") String number){
System.out.println("displayInvoice had been called");
Invoice invoice = invoiceService.getInvoiceByNumber(number);
Customer customer = restTemplate.getForObject(
"http://customer-service/customer/" + invoice.getIdCustomer(),Customer.class);
customer.setAddress(restTemplate.getForObject(
"http://customer-service/address/" + customer.getAddress().getId(),Address.class));
invoice.setCustomer(customer);
return invoice ;
}
public RestTemplate getRestTemplate() {
return restTemplate;
}
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public WebClient.Builder getwBuilder() {
return wBuilder;
}
public void setwBuilder(WebClient.Builder wBuilder) {
this.wBuilder = wBuilder;
}
}
实体文件(Customer.java):
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false,length = 50)
private String name;
@OnetoOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL,orphanRemoval = true,optional = false)
@JoinColumn(name="ID_ADDRESS")
private Address address;
public Customer(String name) {
this.name = name;
}
public Customer() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
日志(已激活调试模式):
2021-01-23 23:07:22.886 DEBUG 2244 --- [p-nio-80-exec-9] o.s.web.servlet.dispatcherServlet :
GET "/home",parameters={}
2021-01-23 23:07:22.886 DEBUG 2244 --- [p-nio-80-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.my.company.invoise.invoice.controller.InvoiceControllerWeb#displayWhom(Model)
You just called displayWhom
2021-01-23 23:07:22.886 DEBUG 2244 --- [p-nio-80-exec-9] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html,application/xhtml+xml,image/webp,application/xml;q=0.9,*/*;q=0.8]
2021-01-23 23:07:22.886 DEBUG 2244 --- [p-nio-80-exec-9] o.s.web.servlet.dispatcherServlet : Completed 200 OK
2021-01-23 23:07:22.933 DEBUG 2244 --- [-nio-80-exec-10] o.s.web.servlet.dispatcherServlet : GET "/js/script.js",parameters={}
2021-01-23 23:07:22.933 DEBUG 2244 --- [-nio-80-exec-10] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [class path resource [meta-inf/resources/],class path resource [resources/],class path resource [static/],class path resource [public/],ServletContext resource [/]]
2021-01-23 23:07:22.933 DEBUG 2244 --- [-nio-80-exec-10] o.s.web.servlet.dispatcherServlet : Completed 304 NOT_MODIFIED
2021-01-23 23:07:22.964 DEBUG 2244 --- [p-nio-80-exec-1] o.s.web.servlet.dispatcherServlet : GET "/invoice",parameters={}
2021-01-23 23:07:22.964 DEBUG 2244 --- [p-nio-80-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.my.company.invoise.invoice.api.InvoiceResource#list()
You just called list method
Hibernate:
select
invoice0_.invoice_number as invoice_1_0_,invoice0_.id_customer as id_custo2_0_,invoice0_.order_number as order_nu3_0_
from
invoice invoice0_
2021-01-23 23:07:22.980 DEBUG 2244 --- [p-nio-80-exec-1] o.s.w.r.f.client.ExchangeFunctions : [1150dd84] HTTP GET http://MSI:8090/customer/1
2021-01-23 23:07:22.980 DEBUG 2244 --- [p-nio-80-exec-1] o.s.w.c.request.async.WebAsyncManager : Started async request
2021-01-23 23:07:22.980 DEBUG 2244 --- [p-nio-80-exec-1] o.s.web.servlet.dispatcherServlet : Exiting but response remains open for further handling
2021-01-23 23:07:28.012 DEBUG 2244 --- [oundedElastic-2] o.s.w.c.request.async.WebAsyncManager : Async error,dispatch to /invoice
2021-01-23 23:07:28.012 DEBUG 2244 --- [p-nio-80-exec-2] o.s.web.servlet.dispatcherServlet : "ASYNC" dispatch for GET "/invoice",parameters={}
2021-01-23 23:07:28.012 DEBUG 2244 --- [p-nio-80-exec-2] s.w.s.m.m.a.RequestMappingHandlerAdapter : Resume with async result [org.springframework.web.reactive.function.client.WebClientRequestException: Failed to resolve 'MSI' (truncated)...]
2021-01-23 23:07:28.012 DEBUG 2244 --- [p-nio-80-exec-2] o.s.web.servlet.dispatcherServlet : Unresolved failure from "ASYNC" dispatch: org.springframework.web.reactive.function.client.WebClientRequestException: Failed to resolve 'MSI' after 2 queries ; nested exception is java.net.UnkNownHostException: Failed to resolve 'MSI' after 2 queries
2021-01-23 23:07:28.027 ERROR 2244 --- [p-nio-80-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] threw exception
io.netty.resolver.dns.DnsNameResolverTimeoutException: [/192.168.0.254:53] query via UDP timed out after 5000 milliseconds (no stack trace available)
2021-01-23 23:07:28.027 ERROR 2244 --- [p-nio-80-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing Failed; nested exception is org.springframework.web.reactive.function.client.WebClientRequestException: Failed to resolve 'MSI' after 2 queries ; nested exception is java.net.UnkNownHostException: Failed to resolve 'MSI' after 2 queries ] with root cause
io.netty.resolver.dns.DnsNameResolverTimeoutException: [/192.168.0.254:53] query via UDP timed out after 5000 milliseconds (no stack trace available)
2021-01-23 23:07:28.027 DEBUG 2244 --- [p-nio-80-exec-2] o.s.web.servlet.dispatcherServlet : "ERROR" dispatch for GET "/error",parameters={}
2021-01-23 23:07:28.027 DEBUG 2244 --- [p-nio-80-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
2021-01-23 23:07:28.027 DEBUG 2244 --- [p-nio-80-exec-2] o.s.w.s.m.m.a.httpentityMethodProcessor : Using 'application/json',given [*/*] and supported [application/json,application/*+json,application/json,application/*+json]
2021-01-23 23:07:28.027 DEBUG 2244 --- [p-nio-80-exec-2] o.s.w.s.m.m.a.httpentityMethodProcessor : Writing [{timestamp=Sat Jan 23 23:07:28 CET 2021,status=500,error=Internal Server Error,message=,path=/in (truncated)...]
2021-01-23 23:07:28.027 DEBUG 2244 --- [p-nio-80-exec-2] o.s.web.servlet.dispatcherServlet : Exiting from "ERROR" dispatch,status 500
2021-01-23 23:07:45.856 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : HTTP GET http://localhost:8761/eureka/apps/delta
2021-01-23 23:07:45.856 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Accept=[application/json,application/*+json]
2021-01-23 23:07:45.856 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Response 200 OK
2021-01-23 23:07:45.856 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Reading to [org.springframework.cloud.netflix.eureka.http.EurekaApplications]
2021-01-23 23:07:45.856 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : HTTP PUT http://localhost:8761/eureka/apps/INVOICE-SERVICE/MSI:invoice-service:80?status=UP&lastDirtyTimestamp=1611439605837
2021-01-23 23:07:45.856 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : Accept=[application/json,application/*+json]
2021-01-23 23:07:45.872 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : Response 200 OK
2021-01-23 23:08:15.869 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : HTTP GET http://localhost:8761/eureka/apps/delta
2021-01-23 23:08:15.869 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Accept=[application/json,application/*+json]
2021-01-23 23:08:15.869 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Response 200 OK
2021-01-23 23:08:15.869 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Reading to [org.springframework.cloud.netflix.eureka.http.EurekaApplications]
2021-01-23 23:08:15.869 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : HTTP PUT http://localhost:8761/eureka/apps/INVOICE-SERVICE/MSI:invoice-service:80?status=UP&lastDirtyTimestamp=1611439605837
2021-01-23 23:08:15.869 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : Accept=[application/json,application/*+json]
2021-01-23 23:08:15.869 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : Response 200 OK
2021-01-23 23:08:45.885 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : HTTP GET http://localhost:8761/eureka/apps/delta
2021-01-23 23:08:45.885 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : HTTP PUT http://localhost:8761/eureka/apps/INVOICE-SERVICE/MSI:invoice-service:80?status=UP&lastDirtyTimestamp=1611439605837
2021-01-23 23:08:45.885 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Accept=[application/json,application/*+json]
2021-01-23 23:08:45.885 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : Accept=[application/json,application/*+json]
2021-01-23 23:08:45.885 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Response 200 OK
2021-01-23 23:08:45.885 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Reading to [org.springframework.cloud.netflix.eureka.http.EurekaApplications]
2021-01-23 23:08:45.885 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : Response 200 OK
2021-01-23 23:09:15.908 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : HTTP PUT http://localhost:8761/eureka/apps/INVOICE-SERVICE/MSI:invoice-service:80?status=UP&lastDirtyTimestamp=1611439605837
2021-01-23 23:09:15.908 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : HTTP GET http://localhost:8761/eureka/apps/delta
2021-01-23 23:09:15.908 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : Accept=[application/json,application/*+json]
2021-01-23 23:09:15.908 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Accept=[application/json,application/*+json]
2021-01-23 23:09:15.908 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Response 200 OK
2021-01-23 23:09:15.908 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : Response 200 OK
2021-01-23 23:09:15.908 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Reading to [org.springframework.cloud.netflix.eureka.http.EurekaApplications]
2021-01-23 23:09:45.915 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : HTTP GET http://localhost:8761/eureka/apps/delta
2021-01-23 23:09:45.915 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : HTTP PUT http://localhost:8761/eureka/apps/INVOICE-SERVICE/MSI:invoice-service:80?status=UP&lastDirtyTimestamp=1611439605837
2021-01-23 23:09:45.915 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Accept=[application/json,application/*+json]
2021-01-23 23:09:45.915 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : Accept=[application/json,application/*+json]
2021-01-23 23:09:45.915 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : Response 200 OK
2021-01-23 23:09:45.915 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Response 200 OK
2021-01-23 23:09:45.915 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Reading to [org.springframework.cloud.netflix.eureka.http.EurekaApplications]
2021-01-23 23:10:15.925 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : HTTP PUT http://localhost:8761/eureka/apps/INVOICE-SERVICE/MSI:invoice-service:80?status=UP&lastDirtyTimestamp=1611439605837
2021-01-23 23:10:15.925 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : Accept=[application/json,application/*+json]
2021-01-23 23:10:15.925 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : Response 200 OK
2021-01-23 23:10:15.925 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : HTTP GET http://localhost:8761/eureka/apps/delta
2021-01-23 23:10:15.925 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Accept=[application/json,application/*+json]
2021-01-23 23:10:15.925 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Response 200 OK
2021-01-23 23:10:15.925 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Reading to [org.springframework.cloud.netflix.eureka.http.EurekaApplications]
2021-01-23 23:10:45.944 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : HTTP GET http://localhost:8761/eureka/apps/delta
2021-01-23 23:10:45.944 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : HTTP PUT http://localhost:8761/eureka/apps/INVOICE-SERVICE/MSI:invoice-service:80?status=UP&lastDirtyTimestamp=1611439605837
2021-01-23 23:10:45.944 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Accept=[application/json,application/*+json]
2021-01-23 23:10:45.944 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : Accept=[application/json,application/*+json]
2021-01-23 23:10:45.944 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Response 200 OK
2021-01-23 23:10:45.944 DEBUG 2244 --- [freshExecutor-0] o.s.web.client.RestTemplate : Reading to [org.springframework.cloud.netflix.eureka.http.EurekaApplications]
2021-01-23 23:10:45.944 DEBUG 2244 --- [tbeatExecutor-0] o.s.web.client.RestTemplate : Response 200 OK
有趣的想法是这个网址:“http://MSI:8090/customer/1”是正确的,并给我发回正确的 json 给客户:
{"id":1,"name":"John Doe","address":{"id":1}}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。