gpt4 book ai didi

spring-boot - 与 Eureka 客户端之间的通信

转载 作者:行者123 更新时间:2023-12-05 01:17:37 24 4
gpt4 key购买 nike

我有两个客户端注册到同一个 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
这是我的控制台的样子:enter image description here我已经为此奋斗了好几天,但我已经没有想法了。在这一点上,我真的需要我能得到的所有帮助。

`#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);
}
}`

最佳答案

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 路径调用第一个应用程序将导致 - “我是第二个应用程序”,而使用 /greetingTest 路径调用第二个应用程序将导致 - “我'm first app"这意味着它们已成功集成。

要进一步阅读,您可以使用 spring cloud docs .

关于spring-boot - 与 Eureka 客户端之间的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49563547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com