Eureka注册中心

 上一篇博文中我们搭建了一个简单的SpringCloud微服务工程,其中包含服务提供者和服务消费者。

Eureka

 Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。

结构图

在这里插入图片描述
 通过服务注册中心,单一系统将会被拆分成多个功能板块,拆分后每个功能板块可以作为一个独立的子系统提供气质责任范围内的功能,相互通信,互不影响,之间的耦合度也会降到最低。但是,每个功能板块又相互通信,这个时候就需要服务注册中心将他们整合到一起。


下面我们在原项目的基础上加入Eureka注册中心功能。

============================================================================

搭建注册中心

注册中心(springcloud-eureka-7001)

在父工程中创建一个新的Module,命名为springcloud-eureka-7001,结构如图所示:
在这里插入图片描述
 这同样也是一个springboot项目,在application.yml文件中进行总配置:

application.yml

server:
  port: 7001

#Eureka配置
eureka:
  instance:
    hostname: localhost #Eureka服务端的实例名称
  client:
    register-with-eureka: false #表示是否向eureka注册中心注册自己
    fetch-registry: false #fetch-registry为false,表示自己为注册中心
    service-url:  #监控页面~
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 该项目通过EurekaServer_7001进行启动:

EurekaServer_7001

package com.cloud.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

//启动之后,访问http://localhost:7001/
@SpringBootApplication
@EnableEurekaServer //EnableEurekaServer 服务端的启动类,可以接受别人注册进来
public class EurekaServer_7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_7001.class,args);
    }
}

============================================================================

将服务注册到Eureka注册中心

springcloud-provider-dept-8001的application.yml

server:
  port: 8001

#mybatis配置
mybatis:
  type-aliases-package: com.cloud.springcloud.pojo
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

#spring配置
spring:
  application:
    name: springcloud-provider-dept
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://121.36.16.236:3306/DB01?characterEncoding=UTF-8&useUnicode=true&useSSL=false&serverTimezone=UTC
    username: myuser
    password: mypassword

#Eureka的配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: springcloud-provider-dept8001 #修改eureka上的默认描述信息!

#info配置
info:
  app.name: Leung-springcloud
  company.name: org.Laucloud.com

 测试时先启动Eureka,再启动springcloud-provider-dept-8001。

结束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值