文章目录
1.Eureka介绍
Eureka 是一个用于服务注册和发现的组件,分为Eureka Server和Eureka Client,其中Eureka Server为服务注册中心,Eureka Client 为Eureka客户端
1.1 角色
- Register Service:服务注册中心,是一个Eureka Server,提供服务注册和发现功能
- Provide Service :服务提供者,是一个Eureka Client 提供服务
- Consumer Service:服务消费者,是一个Eureka Client 消费服务
2.实战
2.1 创建Eureka Server
2.1.1 依赖
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
2.1.2 配置
application.yml
server:
port: 8761 #端口地址
eureka:
instance:
hostname: locahost
client:
healthcheck: # 开启健康检查,不然会报错
enabled: true
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone:
http://${eureka.instance.hostname}:${server.port}/eureka/:
spring:
application:
name: eureka-server
2.1.3 修改启动类
@SpringBootApplication
@EnableEurekaServer //加上开启EurekaServer的注解
public class EurekaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class, args);
}
}
2.1 创建EurekaClient
2.1.1 依赖
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>~
2.1.2 配置
修改application.properties文件
eureka.client.healthcheck.enabled=true
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
server.port=8762
spring.application.name=eureka-client #客户端名称
eureka.client.decoder-name=eureka-client
2.1.3 修改启动类
@SpringBootApplication
@EnableEurekaClient //加上 启动EurekaClient的注解
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
3.结果
输入 localhost:8761 会出现以下结果
- 出现 up的一行表示服务已经注册上了