在springboot的官方文档中有介绍怎么搭建EurekaClient,这里我们结合Idea来搭建EurekaClient
在文档中有一段是这样描述的:
To include the Eureka Client in your project, use the starter with a group ID of org.springframework.cloud and an artifact ID of spring-cloud-starter-netflix-eureka-client
如果要创建一个Eureka client在你的项目中,需要使用group ID是“org.springframework.cloud”和artifact ID是“spring-cloud-starter-netflix-eureka-client”的starter,因为我们项目一般是一个web项目,所以在我们搭建的时候也包含一个web的starter。
官网上有这样一段描述:
@SpringBootApplication
@RestController
public class Application {
@RequestMapping("/")
public String home() {
return "Hello world";
}
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
Note that the preceding example shows a normal Spring Boot application. By having spring-cloud-starter-netflix-eureka-client on the classpath, your application automatically registers with the Eureka Server. Configuration is required to locate the Eureka server, as shown in the following example:
注意以上示例展示的是一个正常的springboot应用程序。随着having spring-cloud-starter-netflix-eureka-client 加入包类路径下,你的应用会自动的注册到Eureka Server上。
然后需要把配置文件application.properties文件改成application.yml添加如下配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
这段配置是官网给的配置。
如果在我们测试当中仅仅加入一个这样的配置也是可以让EurekaServer发现它或者是EurekaClient也可以注册到Eureka Server上,但是我们访问注册中心管理页面看一下情况
虽然是发现了他但是感觉并不是很友好,我们把服务名称和端口加上再看看
application.yml
spring:
application:
name: product-service
server:
port: 8771
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
Having spring-cloud-starter-netflix-eureka-client on the classpath makes the app into both a Eureka “instance” (that is, it registers itself) and a “client” (it can query the registry to locate other services). The instance behaviour is driven by eureka.instance.* configuration keys, but the defaults are fine if you ensure that your application has a value for spring.application.name (this is the default for the Eureka service ID or VIP).
如果有了spring-cloud-starter-netflix-eureka-client在你的类路径下,那么你就有了俩个功能,一个是把自己注册到Eureka Server上,一个是作为一个客户端可以查找到注册到EurekaServer上的其他服务。注册到EurekaServer 上的实例表现都是来自于eureka.instance.*的配置的keys,但这些默认值的也是可以,如果你确保你的应用有了一个spring.application.name配置的应用名称(这个就是默认Eureka服务的id)。
To disable the Eureka Discovery Client, you can set eureka.client.enabled to false. Eureka Discovery Client will also be disabled when spring.cloud.discovery.enabled is set to false.
使Eureka discovery client不能使用的俩个配置,一个是你可以设置eureka.client.enabled为false,一个是设置spring.cloud.discovery.enabled 为false
亲测有效