One is to create a Spring Boot project named Eureka server and introduce necessary dependencies into pom.xml. The code is as follows.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.7.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!--<dependency>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-starter-actuator</artifactId>--> <!--</dependency>--> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2. Use the @ EnableEurekaServer annotation to start a service registry to provide dialogue to other applications. You only need to add the following annotation to the Spring Boot application to enable this function.
@EnableEurekaServer @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }
3. By default, service registration will try to register itself as a client, so you need to disable its client behavior.
Add the following configuration in application.properties.
spring.application.name=eureka-server server.port=1111 eureka.instance.hostname=localhost # Close protection mechanism #eureka.server.enable-self-preservation=false eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ logging.file=${spring.application.name}.log
Explain:
eureka.client.register-with-eureka: since the app is a registry, setting it to false means that you do not register yourself with the registry.
eureka.client.fetch-registry: since the responsibility of the registry is to maintain the service instance, it does not need to retrieve the service, so it is also set to false.