我有两个客户注册到同一个Eureka服务器,当我检查服务器UI时,我发现两个客户端都注册正常。但是当我尝试从另一个客户端调用一个客户端时,我得到IO异常:ResponseEntity<LoginInformationDto> quoteResponse = restTemplate.exchange("http://lirs-security/api/userSecurity/validateUserToken/" + token, HttpMethod.GET,
null, new ParameterizedTypeReference<LoginInformationDto>() {
});
/** ResponseEntity<LoginInformationDto> quoteResponse = restTemplate.exchange("http://localhost:8103/api/userSecurity/validateUserToken/" + token, HttpMethod.GET,
null, new ParameterizedTypeReference<LoginInformationDto>() {
}); */
。当我直接访问其他服务时,它可以工作。错误:org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://lirs-security/cbf69488-5624-4181-9254-ff423afa7620": lirs-security; nested exception is java.net.UnknownHostException: lirs-security
这就是我的控制台的样子:
我已经和我斗争了好几天,但我已经没想完了。我真的需要我能得到的所有帮助@这一点。
`#Server application.yml file
spring:
application:
name: lirs-gateway
server:
port: 8101
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
#server:
#waitTimeInMsWhenSyncEmpty: 0
serverUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#security client yml file
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8101/eureka/
instance:
hostname: localhost
#Security client properties file
spring.application.name=lirs-security
server.port=8103
spring.datasource.url=jdbc:mysql://localhost:3306/*****
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
spring.jpa.show-sql=true
spring.jpa.hibernate.naming-
strategy=org.hibernate.cfg.ImprovedNamingStrategy
#tax payers client yml
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8101/eureka/
instance:
hostname: localhost
# client properties file
spring.application.name=tax-payers
server.port=8102
spring.datasource.url=jdbc:mysql://localhost:3306/****
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
spring.jpa.show-sql=true
spring.jpa.hibernate.naming-
strategy=org.hibernate.cfg.ImprovedNamingStrategy
security.oauth2.client.clientId=myID
security.oauth2.client.scope=bigScope
simpleProp=this is the value`
`@EnableDiscoveryClient
//@EnableEurekaClient
@SpringBootApplication
public class TaxpayersApplication {
public static void main(String[] args) {
SpringApplication.run(TaxpayersApplication.class, args);
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
return source;
}
}
@EnableDiscoveryClient
//@EnableEurekaClient
@SpringBootApplication
public class LirsSecurityApplication {
public static void main(String[] args) {
SpringApplication.run(LirsSecurityApplication.class, args);
}
}`
答案 0 :(得分:3)
Eureka旨在用于定位服务。这意味着您不应该在客户端中对任何URL进行硬编码。两个eureka客户端集成的基本示例应如下所示:
1)Eureka服务器:
spring:
application:
name: SERVER1
server:
port: 8761
eureka:
server:
hostname: localhost
2)首次申请:
spring:
application:
name: app_1
server:
port: 0
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka
3)第二次申请:
spring:
application:
name: app_2
server:
port: 0
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka
因此我们在eureka服务器上注册了两个应用程序。
现在的想法是从eureka服务器请求所需的应用程序URL并使用它。
第一个应用代码:
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ClientApp {
public static void main(String[] args) {
SpringApplication.run(ClientApp.class, args);
}
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
@Autowired
private EurekaClient discoveryClient;
@RequestMapping("/greetingTest")
public String greetingTest() {
String url = discoveryClient.getNextServerFromEureka("APP_2", false).getHomePageUrl();
return restTemplate().getForEntity(url + "/greeting", String.class).getBody();
}
@RequestMapping("/greeting")
public String greeting() {
return "I'm first app";
}
}
除此行外,第二个应用程序具有相同的代码:
discoveryClient.getNextServerFromEureka("APP_2", false)
它将使用APP_1作为服务名称。所以基本上第一个应用程序请求第二个应用程序URL,反之亦然。
因此,使用/greetingTest
路径呼叫第一个APP将导致 - “我是第二个应用”,而使用/greetingTest
路径呼叫第二个APP将导致 - “我是第一个应用”,这意味着它们已成功整合。
如需进一步阅读,您可以使用spring cloud docs。