与 Eureka 客户之间的沟通

sfa*_*ade 2 client spring-boot netflix-eureka

我有两个客户端注册到同一个 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);
}
}`
Run Code Online (Sandbox Code Playgroud)

Iva*_*mar 5

Eureka 用于定位服务。这意味着您不应在客户端中硬编码任何 URL。两个 eureka 客户端集成的基本示例应如下所示:

1)尤里卡服务器:

spring:
  application:
    name: SERVER1
server:
  port: 8761
eureka:
  server:
    hostname: localhost
Run Code Online (Sandbox Code Playgroud)

2) 第一次申请:

spring:
  application:
    name: app_1
server:
  port: 0
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
Run Code Online (Sandbox Code Playgroud)

3) 第二次申请:

spring:
  application:
    name: app_2
server:
  port: 0
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
Run Code Online (Sandbox Code Playgroud)

所以我们在 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";
    }
}
Run Code Online (Sandbox Code Playgroud)

除了这一行之外,第二个应用程序具有相同的代码:

discoveryClient.getNextServerFromEureka("APP_2", false)
Run Code Online (Sandbox Code Playgroud)

它将使用 APP_1 作为服务名称。所以基本上第一个应用程序请求第二个应用程序 URL,反之亦然。

因此,使用/greetingTest路径调用第一个应用程序将导致 - “我是第二个应用程序”,而使用/greetingTest路径调用第二个应用程序将导致 - “我是第一个应用程序”,这意味着它们已成功集成。

如需进一步阅读,您可以使用 spring cloud docs