创建一个Eureka 是一个很简单的事,这点感谢Spring的一帮大神们,只需要写几个注解就OK了。
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class, args);
}
}
看到没,就这么简单 Eureka Server已经完成了。
不过测试的时候的注意一些事项。官网是这样说的:
When the registry starts up it will complain, with a stacktrace, that there are no replica nodes for the registry to connect to. In a production environment, you will want more than one instance of the registry. For our simple purposes, however, it sufficies to disable the relevant logging.
By default, the registry will also attempt to register itself, so you’ll need to disable that, as well.
It’s a good convention to put this registry on a separate port when using it locally.
Add some properties to your eureka-service/src/main/resources/application.properties to handle all of these requirements.
大致意思是说:当服务注册和发现服务启动后,他自己也会注册到,生产环境中我们的Eureka是多个,那没什么问题,开发坏境没必要启动多个实例,只需要启动一个,所以我们要在属性配置文件(application.properties)中做一些配置。
下一章节 Eueark 客户端# 当前服务启动后的服务端口
server.port=8761
#是否需要注册到注册中心,因为该项目本身作为服务注册中心,所以为false eureka.client.register-with-eureka=false
#是否需要从注册中心获取服务列表,原因同上,为false eureka.client.fetch-registry=false logging.level.com.netflix.eureka=OFF logging.level.com.netflix.discovery=OFF