java 将 spring-cloud 与 netflix Eureka 一起使用时,如何让 Discovery Client 工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28990517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the Discovery Client working when using spring-cloud together with netflix Eureka?
提问by nesohc
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. 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. But I can't get either to work.
我正在尝试使用 Spring Cloud 和 Netflix 插件(如 Hystrix、Eureka 和 Ribbon)制作一个基本项目,以了解其工作原理。我正在尝试制作的项目是一个简单的消息服务器,它将保留消息。还有一个消息客户端,它只会向服务器询问消息,我想为此使用自动发现客户端,或 RestTemplate 发现。但我不能去工作。
I have the following structure:
我有以下结构:
- message-client (eureka client)
- message-server (eureka client)
- 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 详细信息公开给所有这些“应用程序/客户端”:
- config-service\src\main\resources\config\appname.yml
- app\src\main\resources\bootstrap.yml (contains the appname and url to cloud config)
- 配置服务\src\main\resources\config\appname.yml
- app\src\main\resources\bootstrap.yml(包含应用名称和云配置的 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. 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.
这工作得很好,我的应用程序在它们从配置服务器接收的端口上启动,并且它们都连接到我的 eureka 服务器,并且它们都在那里可见。除了 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 注释)中使用 @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:
消息客户端 - 消息客户端:
@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:
正如我所说,我的服务类找不到任何东西..这导致了我所有的问题:
- What is required to actually use ribbon load balancer?
- Do I have to use ribbon to be able to use the "auto discovery", I thought not but now I'm just confused.
- From what I've understood, when using EnableEurekaClient I should not need to use the EnableDiscoveryClient as well?
- Can I change the yml files on my config-server for the clients in runtime and just have to reboot the client?
- 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.
- Does anyone have a good link to where I can read more about all the properties that is being set in my yml files? 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?
- Do I need specific properties to enable my apps/clients to find other apps/clients?
- 实际使用功能区负载平衡器需要什么?
- 我是否必须使用功能区才能使用“自动发现”,我认为不是,但现在我很困惑。
- 据我了解,在使用 EnableEurekaClient 时,我不应该也需要使用 EnableDiscoveryClient 吗?
- 我可以在运行时为客户端更改配置服务器上的 yml 文件,而只需要重新启动客户端吗?
- 配置服务器真正要共享多少配置,因为目前我所有的客户端都只包含一个超级基本的 bootstrap.yml 文件。
- 有没有人有一个很好的链接,我可以在这里阅读有关在我的 yml 文件中设置的所有属性的更多信息?既有关于存在的属性的实际作用的文档,也有关于如何将它们与 Spring Cloud 结合使用的文档?
- 我是否需要特定属性才能让我的应用程序/客户端找到其他应用程序/客户端?
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.
那么我有两个新问题。
- How do I override this eureka server property?
- Why does my discovery client stop working when I set this value, what is it that it actually does?
- 如何覆盖此尤里卡服务器属性?
- 为什么当我设置这个值时我的发现客户端停止工作,它实际上是做什么的?
回答by Dave Syer
What is required to actually use ribbon load balancer?
实际使用功能区负载平衡器需要什么?
The ribbon-loadbalancer must be on the classpath (e.g. via "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
).
ribbon-loadbalancer 必须在类路径上(例如通过“spring-cloud-starter-ribbon”)。然后你可以注入一个作为 aLoadBalancerClient
或者你可以注入一个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).
什么是“自动发现”?您不需要使用 Ribbon 来使用DiscoveryClient
(Ribbon 是负载均衡器,而不是服务注册表)。
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? 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?
有没有人有一个很好的链接,我可以在这里阅读有关在我的 yml 文件中设置的所有属性的更多信息?既有关于存在的属性的实际作用的文档,也有关于如何将它们与 Spring Cloud 结合使用的文档?
Spring Boot and Spring Cloud have autogenerated metadata for externalized properties. 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
Spring Boot 和 Spring Cloud 为外化属性自动生成元数据。新一代 IDE 理解它们(因此获得 STS 3.6.4 或 IDEA 14.1),它们列在http://docs.spring.io/spring-boot/docs下的用户指南中(至少对于 Spring Boot)/current/reference/htmlsingle/#common-application-properties
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). If you are using Eureka and your clients have registered then that is enough.
您需要能够找到您的服务注册中心(在本例中为 Eureka)。如果您正在使用 Eureka 并且您的客户已经注册,那就足够了。