版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zc_ad/article/details/85328858
Eureka是基于 REST 的服务,来实现服务的发现与注册。不多说,直接提供代码。
1.maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xichuan.dev</groupId>
<artifactId>xichuan-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>xichuan-eureka</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.application.yml配置
spring:
application:
name: xichuan-eureka
server:
port: 2001
eureka:
client:
register-with-eureka: false
fetch-registry: false
registryFetchIntervalSeconds: 5
serviceUrl:
#eureka服务的地址
defaultZone: http://localhost:${server.port}/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
#不使用Https
management:
security:
enabled: false
3.在启动类上加上注释
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //添加此注解,表示是eureka服务
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
eureka的搭建非常简单,主要还是看你是如何使用了.如果直接访问:http://localhost:2001/ ,就可以看到在eureka注册的服务有哪些。
可以看下大佬的博客,大佬写spring cloud的博客很详细:https://blog.csdn.net/forezp/article/details/70148833