Spring Cloud Eureka实现服务发现

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

1. Eureka

  Eureka [jʊ’ri:kə] 是Netflix发现的一个服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS(Amazon Web Service)域中的中间件服务,以达到负载均和和中间服务故障转移的目的。Spring Cloud将其集成在自己的子项目Spring Cloud Netflix中,以实现Spring Cloud的服务发现功能。
  Eureka的服务发现包含两大组件:

  • 服务端发现组件(Eureka Server),也被称为服务注册中心,主要提供服务的注册功能。
  • 客户端发现组件(Eureka Client),主要用于处理服务的注册与发现。

在这里插入图片描述

  1. 客户端组件可以分为服务消费者和服务提供者,不过两者是相对的可以相互转换。可以既是消费者也是服务提供者。
  2. 服务提供者指的是提供服务的应用,一般用Spring Boot搭建,也可以是其他技术平台的,遵循Eureka通讯机制就可以了。提供者应用运行的时候自动将自己提供的服务注册到Eureka Server以供其他应用发现。
  3. 服务消费者会周期性地给服务注册中心发送心跳来更新服务(默认30s一周期),如果连续三次心跳都不能够发现服务,那么Eureka就会将这个服务节点从服务注册表中移除。所以一个服务关闭了,并不会立刻在服务中心不可见,需要一段时间,但是一个服务一启动注册就看到了
  4. 此外,客户端发现组件还会从服务端查询当前注册的服务信息缓存到本地,即使Eureka Server出了问题,客户端组件也可以通过缓存中的信息调研服务节点服务。各个服务之间会通过注册中心的注册信息以REST方式来实现调用,并且可以直接通过服务名调用

2. 使用Eureka搭建服务端注册中心

2.1 环境

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>xyz.cglzwz</groupId>
    <artifactId>ms-springcloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ms-springcloud</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</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>

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

</project>

在这里插入图片描述
很多传递依赖,导入Eureka Server依赖即可

2.2 配置文件 application.yml

# 服务注册中心
server:
  port: 8765  
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false  # 这是服务中心,不是要注册的服务
    fetch-registry: false           # 也不是服务发现检索
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/  # 注册中心的地址
  server:
    enable-self-preservation: false  # 关闭保护默认开启的保护机制

${} 是引用前面的属性值

2.4 项目主类添加注解

@EnableEurekaServer,声明标注类是一个Eureka Server

package xyz.cglzwz.msspringcloud;

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

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

2.5 启动服务注册中心

http://localhost:8765,现在还没有服务
在这里插入图片描述


3. 使用Eureka搭建客户端服务提供者

3.1 环境

可以直接继承刚刚父项目的依赖,也可以另外配置,这里直接继承了前面的项目为父项目。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>xyz.cglzwz</groupId>
        <artifactId>ms-springcloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>xyz.cglzwz</groupId>
    <artifactId>ms-user</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ms-user</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

</project>

3.2 配置文件

server:
  port: 8000        # Eureka 实例端口
eureka:
  instance:
    prefer-ip-address: true # 显示主机的IP
  client:
    service-url:
      defaultZone: http://localhost:8765/eureka/   # 指定服务端注册中心地址
spring:
  application:
    name: ms-user1        # 指定服务应用的名字

3.3 注解驱动

@EnableEurekaClient

package xyz.cglzwz.msuser;

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

@EnableEurekaClient
@SpringBootApplication
public class MsUserApplication {

    public static void main(String[] args) {
        SpringApplication.run(MsUserApplication.class, args);
    }
}
3.4 简单的一个服务,供消费者调用
package xyz.cglzwz.msuser.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String print() {
        return "Hello ..";
    }
}

3.5 启动运行

必须先启动服务端注册中心,不然直接启动客户端的会报错,找不到等异常
在这里插入图片描述


4. 使用Eureka搭建客户端服务消费者

4.1 pom.xml && application.yml

很服务提供者一样,pom.xml都一样,配置文件这里只是修改了端口和服务名

server:
  port: 7000
eureka:
  instance:
    prefer-ip-address: true # 显示主机的IP
  client:
    service-url:
      defaultZone: http://localhost:8765/eureka/   # 指定服务端地址
spring:
  application:
    name: ms-user2  

4.2 引导类

package xyz.cglzwz.msuser2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableEurekaClient
@SpringBootApplication
public class MsUser2Application {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

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

这里还注入了一个RestTemplate的Bean到容器,RestTemplate是Spring提供的用于访问REST服务的客户端实例,它提供多种便捷访问远程Http服务的方法,能够大大提供客户端的编写效率。

4.3 调用服务提供者提供的一个服务

package xyz.cglzwz.msuser2.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class TestController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/print")
    public String print() {
        // 调用ms-user1应用的一个显示Hello功能
        return restTemplate.getForObject("http://localhost:8000/test", String.class);
    }
}

当输入http://localhost:7000/print 的时候消费者会调用提供者的服务
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/chenbetter1996/article/details/88800438
今日推荐