SpringCloud入门第二节:服务注册与发现Eureka

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的一行表示服务已经注册上了
    实验结果
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值