springboot集成eureka实现服务注册和发现

 

原文地址:https://www.jianshu.com/p/3892df1eea7e

一:eureka是什么?

Eureka是netflix的一个子模块,以实现云端中间层服务注册和服务发现。功能类似于dubbo的注册中心,比如zookeeper。

二:为什么要用eureka来做服务注册和发现,和dubbo对比有什么优势?

eureka.png

  • 要说这两者,不得不提一下分布式架构中的CAP理论,即一个分布式框架,只能同时满足C一致性、A可用性、P网络分区容错性这三者中的两个,不可能同时兼备三者
  • Dubbo推荐的注册中心首选ZK,而ZK是一个满足CP的框架;Eureka由于其架构设计,更多专注于AP。

三:springboot集成demo(分为三模块:eurekaServer ,provider, consumer)

1.首先来看eurekaServer

  • pom依赖
<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <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>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
  • yml配置

 

server:
  port: 8761

eureka:
  client:
    registerWithEureka: false  #不进行自我注册
    fetchRegistry: false
  server:
    waitTimeInMsWhenSyncEmpty: 0
  • 启动类上加上注解
@SpringBootApplication
@EnableEurekaServer
public class MallEurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(MallEurekaApplication.class, args);
    }

}

2.在来看provider服务提供者

  • pom依赖
<properties>
     <java.version>1.8</java.version>
     <spring-cloud.version>Greenwich.SR3</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>${spring-cloud.version}</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
     </dependencies>
 </dependencyManagement>
  • pml配置
server:
  port: 8080
spring:
  application:
    name: mall-provider
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    instance-id: mall-provider
    prefer-ip-address: true
  • 启动类上加上注解
@SpringBootApplication
@EnableDiscoveryClient
public class MallProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(MallProviderApplication.class, args);
    }

}
  • 创建一个服务
@RestController
@RequestMapping("/demo")
public class ProviderController {

    @RequestMapping("/getHello")
    @ResponseBody
    public String getHello() {
        return "hello-provider-api!!!";
    }
}

3.consumer服务消费者

  • pom 依赖
<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR3</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.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</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>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
  • yml配置
server:
 port: 8081

spring:
 application:
   name: mall-consumer

eureka:
 client:
   service-url:
     defaultZone: http://localhost:8761/eureka
 instance:
   instance-id: mall-consumer
   prefer-ip-address: true
  • 主启动类上加注解
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class MallConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(MallConsumerApplication.class, args);
    }
}
  • 创建消费API
@FeignClient(value = "MALL-PROVIDER") //引用服务提供者的服务名称
public interface ProviderApi {
    @RequestMapping("/demo/getHello")
    String getHello();
}
  • controller中调用
   @Autowired
    private ProviderApi providerApi;

   // 通过fegin调用服务
    @RequestMapping("/getHello3")
    @ResponseBody
    public String getHello3() {
        return providerApi.getHello();
    }

四:总结和扩展

  • 本demo是单机模式,如果要机器模式,只需要改下配置即可
  • 服务提供者和服务注册者可以在管理页面上查看
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值