Sprng Cloud入门之Eureka(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40369944/article/details/84988696

目录

1.前言

2.介绍

3.搭建注册中心

3.1 创建Spring Boot项目

3.2导入依赖

3.3 application.yml 配置参数

3.4开启注册中心功能

4.实战演练

4.1 user-api 项目部分代码(服务提供)

4.1.1添加依赖

4.1.2配置参数

4.1.3服务接口

4.1.4开启服务注册功能

4.2user-web 项目部分代码(服务消费)

4.2.1添加依赖

4.2.2配置参数

4.2.3客户端

4.2.4开启服务发现功能

源码地址


1.前言

Spring Cloud 是一系列框架的有序集合。它利用 Spring Boot 的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用 Spring Boot 的开发风格做到一键启动和部署。

2.介绍

Eureka 是 Netflix 的子模块,它是一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移。

服务注册和发现对于微服务架构而言,是非常重要的。有了服务发现和注册,只需要使用服务的标识符就可以访问到服务,而不需要修改服务调用的配置文件。该功能类似于 Dubbo 的注册中心,比如 Zookeeper。

Eureka 采用了 CS 的设计架构。Eureka Server 作为服务注册功能的服务端,它是服务注册中心。而系统中其他微服务则使用 Eureka 的客户端连接到 Eureka Server 并维持心跳连接。

其运行原理如下图:

由图可知,Eureka 的运行原理和 Dubbo 大同小异, Eureka 包含两个组件: Eureka Server 和 Eureka Client。

Eureka Server 提供服务的注册服务。各个服务节点启动后会在 Eureka Server 中注册服务,Eureka Server 中的服务注册表会存储所有可用的服务节点信息。

Eureka Client 是一个 Java 客户端,用于简化 Eureka Server 的交互,客户端同时也具备一个内置的、使用轮询负载算法的负载均衡器。在应用启动后,向 Eureka Server 发送心跳(默认周期 30 秒)。如果 Eureka Server 在多个心跳周期内没有接收到某个节点的心跳,Eureka Server 会从服务注册表中将该服务节点信息移除。

3.搭建注册中心

3.1 创建Spring Boot项目

3.2导入依赖

(注意Spring Boot 与 SpringCloud 有版本兼容关系,如果引用版本不对应,项目启动会报错)



        <!-- eureka 服务端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>1.5.9.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
   

3.3 application.yml 配置参数

server:
  port: 9000

eureka:
  instance:
    hostname: localhost   # eureka 实例名称
  client:
    register-with-eureka: false # 不向注册中心注册自己
    fetch-registry: false       # 是否检索服务
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/  # 注册中心访问地址

3.4开启注册中心功能

在启动类上添加 @EnableEurekaServer 注解。

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}

启动项目,访问http://localhost:9000/ ,可看到 Eureka 服务监控界面,如下:

4.实战演练

了解 Eureka 的环境搭建后,我们需要进行实战直观的感受 Eureka 的真正作用,这样才能清楚掌握和学习 Eureka 。我们再创建两个 Spring Boot 项目,一个名为 user-api ,用于提供接口服务,另一个名为 user-web,用于调用 user-api 接口获取数据与浏览器交互。

服务实例 端口 描述
eureka 9000 注册中心(Eureka 服务端)
user-api 8081 服务提供者(Eureka 客户端)
user-web 80 服务消费者,与浏览器端交互(Eureka 客户端)

4.1 user-api 项目部分代码(服务提供)

4.1.1添加依赖

同样主要版本问题

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- eureka 客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.3.1.RELEASE</version>
        </dependency>

4.1.2配置参数

server:
  port: 8081

spring:
  application:
    name: user-api

eureka:
  instance:
    instance-id: user-api-8081
    prefer-ip-address: true # 访问路径可以显示 IP
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/  # 注册中心访问地址

注意:http://localhost:9000/eureka/ 就是注册中心的地址。

4.1.3服务接口

public interface UserService {
    String getUserName();
}
@Service
public class UserServiceImpl implements UserService {

    @Override
    public String getUserName() {
        return "张三";
    }
}
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getname")
    public String get() {
        return this.userService.getUserName();
    }
}

注意:该 controller 是给 user-web 使用的(内部服务),不是给浏览器端调用的。

4.1.4开启服务注册功能

在启动类上添加 @EnableEurekaClient 注解。

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

}

启动项目完成后,浏览器访问 http://localhost:9000 查看 Eureka 服务监控界面 ,如下图:

从图可知,user 相关服务信息已经注册到 Eureka 服务中了。

补充:在上图中,我们还看到一串红色的字体,那是因为 Eureka 启动了自我保护的机制。当 EurekaServer 在短时间内丢失过多客户端时(可能发生了网络故障),EurekaServer 将进入自我保护模式。进入该模式后,EurekaServer 会保护服务注册表中的信息不被删除。当网络故障恢复后,EurekaServer 会自动退出自我保护模式。

4.2user-web 项目部分代码(服务消费)

4.2.1添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- eureka 客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.3.1.RELEASE</version>
        </dependency>

4.2.2配置参数

server:
  port: 80

spring:
  application:
    name: user-web

eureka:
  client:
    register-with-eureka: false # 不向注册中心注册自己
    fetch-registry: true        # 是否检索服务
    service-url:
      defaultZone: http://localhost:9000/eureka/  # 注册中心访问地址

4.2.3客户端

@Configuration
public class RestConfiguration {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @Resource
    private DiscoveryClient client;

    @RequestMapping("/getname")
    public User get() throws Exception {
        //getForObject() 发送一个HTTP GET请求,返回的请求体将映射为一个对象
        //user-api 为调用的uri(服务提供者(Eureka 客户端)地址)在eureka上注册的application.name
        return restTemplate.getForObject( "http://user-api/user/getname",User.class);

    }

}

4.2.4开启服务发现功能

在启动类上添加 @EnableDiscoveryClient 注解。

@EnableDiscoveryClient
@SpringBootApplication
public class UserWebApplication {

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

}

启动项目后,使用浏览器访问 user-web 项目接口,运行结果如下:

数据来源:

user-api 8081 服务提供者(Eureka 客户端)

源码地址

https://github.com/Uncle-LiuY/eureka-server

https://github.com/Uncle-LiuY/user-api

https://github.com/Uncle-LiuY/user-web

以上为本人实际测试写下的,如有不足地方请指出。

猜你喜欢

转载自blog.csdn.net/qq_40369944/article/details/84988696