Eureka原理和入门案例

一、Eureka 原理

Eureka客户端会每隔30s向服务端发送心跳包以告知服务端当前客户端没有挂掉。对于Client来说,服务Server超过90s没有收到该Client的心跳数据,Server就会把该Client移出服务列表。最好不要修改30s的默认心跳间隔,因为Server会使用这个时间数值来判断是否出现了大面积故障。

二、搭建Eureka Demo 案例

1搭建注册中心 -> 2服务注册 -> 3服务发现

创建1个maven项目嵌套3个maven项目

EurekaDemo pom.xml配置添加

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.8.RELEASE</version>
        <relativePath/>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR10</spring-cloud.version>
    </properties>

    <dependencyManagement>
    <dependencies>
        <!-- 根据需要添加到Module中 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.2.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>2.2.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.2.8.RELEASE</version>
        </dependency>

    </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

1、搭建注册中心 eureka-server

(1)、eureka-server项目中,引入spring-cloud-starter-netflix-eureka-server的依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

(2)、编写启动类,添加@EnableEurekaServer注解

@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }
}

(3)、添加application.yml文件,编写下面的配置:

server:
  port: 10086
spring:
  application:
    name: eurekaserver
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka/

2、服务注册

注册service-provider

(1)、在service-provider项目引入spring-cloud-starter-netflix-eureka-client的依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

(2)、在application.yml文件,编写下面的配置:

server:
  port: 8081
spring:
  application:
    name: serviceprovider
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka/

 (2)、启动类

@SpringBootApplication
public class ProviderApplication {

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

 (3)、模拟接口

Controller层

@RestController
public class ProviderController {
    @Autowired
    private ProviderService providerService;

    @RequestMapping("user/{id}")
    public String user(@PathVariable("id") Integer id){
        return providerService.user(id);
    }
}

 Service

@Service
public class ProviderService {
    public String user(Integer id) {
        return 1 == id ? "tom" : "jerry";
    }
}

3、服务发现

(1)、在service-consumer项目引入spring-cloud-starter-netflix-eureka-client的依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

(2)、在application.yml文件,编写下面的配置:

server:
  port: 8080
spring:
  application:
    name: serviceconsumer
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka/

(3)、在启动类中给RestTemplate添加@LoadBalanced注解

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

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

(4)、模拟接口

Controller层

@RestController
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;

    @RequestMapping("order/{id}")
    public String run(@PathVariable("id") Integer id){
        return consumerService.run(id);
    }
}

Service层

@Service
public class ConsumerService {

    @Autowired
    private RestTemplate restTemplate;

    public String run(Integer id) {
        //3、使用eureka
        String url = "http://serviceprovider/user/" + id;
        String string = restTemplate.getForObject(url, String.class);
        return string;

    }
}

三、结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值