1、Eureka簡介
Eureka是一個服務註冊與發現組件,主要有兩部分組成:註冊中心(Eureka-Server)和服務提供端(Eureka-Client),Eureka-Server通過一個註冊表維護了所有向Eureka-Server註冊的Eureka-Client的主機地址和端口等信息,同時Eureka-Client可以向Eureka-Server獲取該註冊表,並默認每30s向註冊中心發送一次心跳錶明自己還活着,如果Eureka-Server在一定時間內未接收某個微服務實例的心跳,EurekaServer將會註銷該實例(默認90s)。
2、創建一個SpringBoot項目作爲Eureka-Server
創建過程就不多說了,需要注意的是SpringBoot的版本號和SpringCloud的版本號要對應,不然無法找到@EnableEurekaServer和@EnableEurekaClien註解,這兩個註解是必須要用上的,我這裏springboot版本是2.0.3.RELEASE,springcloud版本是Finchley.RELEASE,本系列文章都是使用這兩個版本,pom文件如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
3、修改配置文件application.yml,該文件默認後綴是.properties,我這裏用的是yml後綴的,個人感覺層次感更好些吧
server:
port: 8000 #使用的端口
eureka:
instance:
hostname: localhost #所在機器地址
client:
registerWithEureka: false #表明是個註冊中心
fetchRegister: false #表明是個註冊中心
serviceUrl: #聲明註冊中心的地址(其實就是自己),其實每個Server都是一個Client
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eureka-server #該實例名稱
4、使用@EnableEurekaServer,並啓動
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
5、打開註冊種web界面:localhost:8000,這時候一個註冊的服務實例都沒有
6、創建一個SpringBoot項目作爲Eureka-Client
就不多廢話了,直接上代碼,其實跟Eureka-Server差不多,只是配置文件和pom文件有點區別
pom.xml:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>eureka-client1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
application.yml:
server:
port: 8001
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka
spring:
application:
name: eureka-client1
7、寫個接口並啓動
/**
* @author XuJD
* @create 2019-11-22 14:07
**/
@EnableEurekaClient
@RestController
@RequestMapping("serviceTest")
public class ServiceController {
@Value("${server.port}")
public String port;
@RequestMapping("/test")
public String test(){
return "Hello world,this is eureka-client1--"+port;
}
}
這個時候可以看到這個Eureka-Client1實例已經註冊到Eureka-Server中了
代碼地址:https://github.com/xujiangdong/SpringCloud_Study
都看到這裏了,點個star支持下吧,萬分感謝!