简体   繁体   English

与netflix Eureka一起使用spring-cloud时,如何让Discovery Client工作?

[英]How can I get the Discovery Client working when using spring-cloud together with netflix Eureka?

I'm trying to make a basic project using spring cloud with the netflix addons such as Hystrix, Eureka and Ribbon to learn how this works. 我正在尝试使用带有netflix插件(如Hystrix,Eureka和Ribbon)的spring cloud创建一个基本项目,以了解其工作原理。 The project I'm trying to make is a simple message server that will keep messages. 我正在尝试制作的项目是一个简单的消息服务器,它将保留消息。 And a message-client that will just ask the server for a message, and I want to use the auto discovery client for this, or the RestTemplate discovery. 一个消息客户端只询问服务器的消息,我想使用自动发现客户端或RestTemplate发现。 But I can't get either to work. 但我无法上班。

I have the following structure: 我有以下结构:

  • message-client (eureka client) 消息客户端(尤里卡客户端)
  • message-server (eureka client) 消息服务器(eureka客户端)
  • configuration-service (config server) 配置服务(配置服务器)
  • discovery-service (eureka server) 发现服务(尤里卡服务器)

What I currently do is I start up the configuration-service, and expose the application.yml details to all of these "apps/clients" when they are connecting by the following structure: 我目前所做的是启动配置服务,并在通过以下结构连接时将application.yml详细信息公开给所有这些“apps / clients”:

  • config-service\\src\\main\\resources\\config\\appname.yml 配置服务的\\ src \\主\\资源\\设置\\ appname.yml
  • app\\src\\main\\resources\\bootstrap.yml (contains the appname and url to cloud config) app \\ src \\ main \\ resources \\ bootstrap.yml(包含云配置的appname和url)

This is working just fine, and my apps start up on the port they receive from the config server, as well as they all connect to my eureka server, and all of them are visible there. 这工作正常,我的应用程序启动它们从配置服务器接收的端口,以及它们都连接到我的eureka服务器,并且所有这些都在那里可见。 As well as the Hystrix failover is also working, not that it is related to this but it tells me that it can't be completely wrong then. 除了Hystrix故障转移也起作用,并不是它与此相关,但它告诉我它不可能完全错误。

But here comes my confusion... When using the @Autowired annotation in my service class (@Service annotated) inside my client module, I get a discoveryClient object, but I am unable to find any other services using that one. 但是我的困惑是......在我的客户端模块中的服务类(@Service annotated)中使用@Autowired注释时,我得到了一个discoveryClient对象,但是我无法找到使用该对象的任何其他服务。

Message Client - boot class: 消息客户端 - 启动类:

@EnableAutoConfiguration
@EnableHystrix
@EnableEurekaClient
@ComponentScan("cloud.rest.resources, spring.cloud.client")
public class ClientBoot {
    public static void main(String[] args) {
        SpringApplication.run(ClientBoot.class, args);
    }
}

Message Client - REST Resource: 消息客户端 - REST资源:

@RestController
public class MessageResource {
    @Autowired
    private MessageClient messageClient;
    @RequestMapping(value = "/message/{client}", method = RequestMethod.GET)
    public Message getMessage(@PathVariable String client) {
        return messageClient.getMessage(client);
    }
}

Message Client - MessageClient: 消息客户端 - MessageClient:

@Service
public class RestMessageClient implements MessageClient {
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;
    @Override
    public Message getMessage(String client) {
        return restTemplate.getForObject(String.format("http://message-server/message/%s", client), Message.class);
    }
}

My message server boot class that is holding the messages has the same annotations as my client one. 我保留消息的消息服务器引导类与我的客户端具有相同的注释。

And as I said, my service class are unable to find anything.. Which leads me to all of my questions: 正如我所说,我的服务类无法找到任何东西..这引出了我所有的问题:

  1. What is required to actually use ribbon load balancer? 实际使用色带负载平衡器需要什么?
  2. Do I have to use ribbon to be able to use the "auto discovery", I thought not but now I'm just confused. 我是否必须使用功能区才能使用“自动发现”,我想不会,但现在我只是感到困惑。
  3. From what I've understood, when using EnableEurekaClient I should not need to use the EnableDiscoveryClient as well? 根据我的理解,当使用EnableEurekaClient时,我也不需要使用EnableDiscoveryClient?
  4. Can I change the yml files on my config-server for the clients in runtime and just have to reboot the client? 我可以在运行时为客户端更改配置服务器上的yml文件,只需重新启动客户端吗?
  5. How much configuration is really meant to be shared by the config-server, because currently all of my clients just contain a super basic bootstrap.yml file. 配置服务器实际上要分配多少配置,因为目前我的所有客户端都只包含一个超级基本的bootstrap.yml文件。
  6. Does anyone have a good link to where I can read more about all the properties that is being set in my yml files? 有没有人有一个很好的链接,我可以阅读更多关于我的yml文件中设置的所有属性? Both a documentation of what the properties that exists actually do as well as some documentation on how I can use them in combination with spring cloud? 既存在实际存在的属性的文档,也包含有关如何将它们与spring cloud结合使用的文档?
  7. Do I need specific properties to enable my apps/clients to find other apps/clients? 我是否需要特定属性才能使我的应用/客户端找到其他应用/客户端?

Edited information 编辑信息

Thank you for your quick and excellent reply, I've gone through this over and over today and I finally got my application working.. The problem (I can't understand why and was hoping you could help me understand that) is that my discovery service contains yml files for each of my other clients where I specify things like port and eureka information.. What I specified here as well was: 感谢您的快速和出色的回复,我今天经历了一遍又一遍,我终于让我的应用程序正常工作..问题(我无法理解为什么,希望你能帮我理解)是我的发现服务包含我的每个其他客户端的yml文件,我指定了端口和尤里卡信息等内容。我在这里指定的是:

eureka:
    client:
        serviceUrl:
            defaultZone: http://localhost:8761/eureka

So, when I set this value it seems to override something that makes my service discovery not working.. Even tho I can see all my applications in the eureka server, they were unable to find each other when I had this value set. 因此,当我设置此值时,它似乎覆盖了使我的服务发现无法工作的东西。即使我可以在eureka服务器中看到我的所有应用程序,但是当我设置此值时,他们无法找到彼此。

I set this value by having a message-server.yml file in my configuration service that is sent out to my message-server application after bootstrap.. 我通过在配置服务中使用message-server.yml文件来设置此值,该文件在引导程序之后发送到我的消息服务器应用程序。

So then I have two new questions. 那么我有两个新问题。

  1. How do I override this eureka server property? 如何覆盖此eureka服务器属性?
  2. Why does my discovery client stop working when I set this value, what is it that it actually does? 为什么我的发现客户端在设置此值时停止工作,它实际上做了什么?

What is required to actually use ribbon load balancer? 实际使用色带负载平衡器需要什么?

The ribbon-loadbalancer must be on the classpath (eg via "spring-cloud-starter-ribbon"). 色带负载平衡器必须位于类路径上(例如,通过“spring-cloud-starter-ribbon”)。 Then you can inject one as a LoadBalancerClient or you can inject a RestTemplate (it will be load-balancer aware if you have a LoadBalancerClient ). 然后你可以注入一个作为LoadBalancerClient或者你可以注入一个RestTemplate (如果你有一个LoadBalancerClient ,它将是负载均衡器)。

Do I have to use ribbon to be able to use the "auto discovery", I thought not but now I'm just confused. 我是否必须使用功能区才能使用“自动发现”,我想不会,但现在我只是感到困惑。

What is "auto discovery"? 什么是“自动发现”? You don't need to use Ribbon to use the DiscoveryClient (Ribbon is a load balancer, not a service registry). 您不需要使用功能区来使用DiscoveryClient (功能区是负载均衡器,而不是服务注册表)。

From what I've understood, when using EnableEurekaClient I should not need to use the EnableDiscoveryClient as well? 根据我的理解,当使用EnableEurekaClient时,我也不需要使用EnableDiscoveryClient?

Correct. 正确。 @EnableEurekaClient is annotated with @EnableDiscoveryClient so it is only there to express a preference. @EnableEurekaClient使用@EnableDiscoveryClient注释,因此它仅用于表示首选项。

Can I change the yml files on my config-server for the clients in runtime and just have to reboot the client? 我可以在运行时为客户端更改配置服务器上的yml文件,只需重新启动客户端吗?

Yes. 是。 Or you can use the /refresh or /restart endpoints (a full reboot is probably best in production, at least periodically). 或者您可以使用/ refresh或/ restart端点(完全重启可能是最好的生产,至少定期)。

How much configuration is really meant to be shared by the config-server, because currently all of my clients just contain a super basic bootstrap.yml file. 配置服务器实际上要分配多少配置,因为目前我的所有客户端都只包含一个超级基本的bootstrap.yml文件。

As much as you want. 尽你所能。 How long is a piece of string? 一段绳子有多长? If I were you I would try and keep the central config to a minimum (only the things that change between environments, or at runtime). 如果我是你,我会尝试将中央配置保持在最低限度(仅在环境之间或运行时更改的内容)。

Does anyone have a good link to where I can read more about all the properties that is being set in my yml files? 有没有人有一个很好的链接,我可以阅读更多关于我的yml文件中设置的所有属性? Both a documentation of what the properties that exists actually do as well as some documentation on how I can use them in combination with spring cloud? 既存在实际存在的属性的文档,也包含有关如何将它们与spring cloud结合使用的文档?

Spring Boot and Spring Cloud have autogenerated metadata for externalized properties. Spring Boot和Spring Cloud具有自动生成的外部化属性元数据。 The new generation of IDEs understands them (so get STS 3.6.4 or IDEA 14.1), and they are listed in the user guide (for Spring Boot at least) under http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties 新一代IDE了解它们(因此获得STS 3.6.4或IDEA 14.1),它们在http://docs.spring.io/spring-boot/docs的用户指南(至少为Spring Boot)中列出/电流/参考/ htmlsingle /#常见的应用程序的属性

Do I need specific properties to enable my apps/clients to find other apps/clients? 我是否需要特定属性才能使我的应用/客户端找到其他应用/客户端?

You need to be able to locate your service registry (Eureka in this case). 您需要能够找到您的服务注册表(在本例中为Eureka)。 If you are using Eureka and your clients have registered then that is enough. 如果您使用Eureka并且您的客户已经注册,那就足够了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在启动时通过Netflix Eureka Discovery实现Spring Cloud Config Server循环依赖 - Spring Cloud Config Server Circular Dependency With Netflix Eureka Discovery on Startup spring-cloud.netflix:无法启用 eureka 客户端? - spring-cloud-netflix: Cannot enable eureka client? 使用Spring Cloud Netflix Eureka的Node.js客户端应用程序 - Node.js client application with Spring Cloud Netflix Eureka 如何覆盖Spring Cloud Eureka默认发现客户端默认SSL上下文? - How to override Spring Cloud Eureka default discovery client default ssl context? 无法解析 org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:unknown - Cannot resolve org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:unknown 使用 spring-cloud-starter-netflix-eureka-client 依赖项在 pom.xml 中出现错误 - Getting error in pom.xml with spring-cloud-starter-netflix-eureka-client dependency 如何在Spring-Cloud中将ConsulDiscoveryClient与Zuul和Sidecar一起使用 - How do I use the ConsulDiscoveryClient with Zuul and Sidecar in Spring-Cloud 使用 Spring-Cloud Open Feign,我如何读取另一种类型的嵌套 JSON 数据? - using Spring-Cloud Open Feign, how do i read nested JSON data of another type? Spring Cloud Eureka:改变心跳和发现路径 - Spring Cloud Eureka: changing heartbeat and discovery path 如何使用Netflix / Eureka Service的发现信息在Netflix / Zuul和Netflix / Ribbon中启用自动路由? - How to enable automatic routing in Netflix/Zuul and Netflix/Ribbon with discovery information from Netflix/Eureka Service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM