Eureka
1.eureka-server
1.1 pom文件
构建eureka-server微服务时,需要先在pom文件中引入相应的坐标
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
1.2 配置文件
需要在配置文件中,对相应的参数进行配置application.yml
spring.application.name: eureka-server
server.port: 8761
eureka:
client:
fetch-registry: false
register-with-eureka: false
- spring.application.name:定义服务名称
- server.port: 定义服务端口号为8761
- eureka.client.fetch-registry: 设置为false表示不要再本地缓存注册表信息
- eureka.client.registerWithEureka 属性会告知服务,在应用程序启动时不要通过Eureka服务注册,因为它本身就是Eureka服务
- 每次服务注册需要30s的时间才能显示在eureka服务中,因为eureka需要从服务接收3次连续心跳包ping,每次心跳包ping间隔10s,然后才能使用这个服务。
1.3 添加注解
在建立eureka服务时, 需要进行的最后一项工作是在服务启动类上添加注解
@SpringBootApplication
@EnableEurekaServer ⇽--- 在Spring服务中启用Eureka服务器
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
2.eureka-client
2.1pom文件
构建eureka-client服务的时候,在pom文件中引入相应坐标
<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-web</artifactId>
</dependency>
- spring-cloud-starter-netflix-eureka-client:eureka客户端包
- spring-boot-starter-web:spring的web包,需要引入此包,才能注册成功
2.2配置文件
eureka:
instance:
prefer-ip-address: true ⇽--- 注册服务的IP,
client:
register-with-eureka: true ⇽---向Eureka注册服务
fetch-registry: true ⇽---拉取注册表的本地副本
service-url:
defaultZone: http://localhost:8761/eureka ⇽---Eureka服务的位置
2.3添加注解
@SpringBootApplication
@EnableDiscoveryClient ⇽---eureka客户端注解
public class EurekaClient1Application {
public static void main(String[] args) {
SpringApplication.run(EurekaClient1Application.class, args);
}
}