美文网首页
二、SpringCloud 之 Eureka 框架入门

二、SpringCloud 之 Eureka 框架入门

作者: cqzhangjian | 来源:发表于2018-02-02 11:49 被阅读0次

Eureka 提供了基于 REST 的服务,在集群中主要用于服务管理。

  • Eureka 服务器端对于注册到服务端的服务组件,不提供后台储存,通过心跳保存最新状态,整个过程在内存中执行,并保存在内存中的注册中心。Eureka 客户端(服务调用者和服务提供者) 也提供相同的 内存注册中心机制,这种机制提升了 Eureka 组件的性能,每次服务请求不必经过服务端注册中心进行访问。

  • Eureka 服务提供者

向服务器注册服务;发送心跳给服务器;向服务器获取注册列表;当注册到服务器,它会把自己相关的信息发送给服务端,诸如主机、端口、健康检查连接等。

  • Eureka 服务调用者

服务器发布服务,服务调用者对其进行服务查找和调用。

  • 构建 Eureka 应用

1. 构建服务器

1.1 配置 POM

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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>Edgware.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-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>

1.2 Eureka 服务器启动类编写

@SpringBootApplication
@EnableEurekaServer
public class DemoEkServerApplication {

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

1.3 properties 文件 服务器端口配置,默认为 8080

server.port=8761
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false

1.4 启动 通过 URL :http://127.0.0.1:8761/

图片.png

2. 构建服务提供者

2.1 配置 POM 文件

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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>Edgware.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</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>

2.2 properties 配置文件

spring.application.name=demo-ek-server-provider
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

2.3 配置 服务提供者启动类

@SpringBootApplication
@EnableEurekaClient
public class DemoEkServerProviderApplication {

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

2.4 编写一个 REST 服务

@RestController
public class ApiDemoController {

    @RequestMapping(value = "/person/{pid}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Person findPerson(@PathVariable("pid") Long id) {

        Person person = new Person();
        person.setId(id);
        person.setLogname("张剑");
        person.setPassword("6000");

        return person;
    }

}

2.5 测试,访问 eureka 服务地址:

图片.png

服务提供者成功注册到了 eureka 服务器中

3.构建服务调用者

3.1 配置 POM 文件

    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

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

3.2 properties 配置文件

server.port=8888
spring.application.name=demo-ek-server-invoker
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

3.3 配置 服务调用者启动类

@SpringBootApplication
@EnableDiscoveryClient
public class DemoEkServerInvokerApplication {

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

3.4 配置 服务调用者对外暴露接口

@Configuration
@RestController
public class InvokerController {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {

        return new RestTemplate();
    }

    @RequestMapping(value = "/pubServer", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public String pubServer() {

        RestTemplate resTemple = getRestTemplate();
        String json = resTemple.getForObject("http://demo-ek-server-provider/person/1", String.class);
        return json;
    }

}

3.5测试

图片.png

4. Eureka 集群搭建

由于之前工作忙加上身体不舒服,没有进行更新,现在SpringCloud 版本最新已经是2.x 版本了,搭建Eureka 服务发现服务器采用新版本来搭建集群

4.1 项目整体结构(平行)

image.png

4.2 服务端程序 配置文件修改为

server:
  port: 9999
spring:
  profiles: slave1
  application:
    name: cluster
eureka:
  instance:
    hostname: slave1
  client:
    registerWithEureka: false
    fetchRegistry: false
    service-url:
      defaultZone: http://slave2:9999/eureka/
---
      
server:
  port: 9998
spring:
  profiles: slave2
  application:
    name: cluster
eureka:
  instance:
    hostname: slave2
  client:
    registerWithEureka: false
    fetchRegistry: false
    service-url:
      defaultZone: http://slave1:9998/eureka/

注意:通过profiles 方式 在本地一台主机上模拟两台主机,通过修改 hosts 来实现,hostname(slave1/slave2) 都绑定在 127.0.0.1 ip 上。这样 两个服务端就可以相互注册

image.png image.png

4.3 服务提供者模拟两台 配置文件修改为

spring:
  application:
    name: sc1
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://slave1:9999/eureka/,http://slave2:9998/eureka/

注意: 模拟两台 服务提供者采用 端口号来区别,在启动服务提供者定义 server.port 端口号


image.png

4.4 服务调用者 配置文件改造

spring:
  application:
    name: sc2
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://slave1:9999/eureka/,http://slave2:9998/eureka/
server:
  port: 7999

注意 服务调用者使用了 负载均衡 调用集群中的 服务提供者提供的服务。

image.png

4.5 使用 Postman 测试 服务调用者提供的接口

image.png image.png

通过上面测试来看,调用接口相同,但是提供的服务的程序不同。

  • Erueka 学习案例源代码下载 github 地址:

https://github.com/cqzhangjian/eureka.git

5. 服务实例健康自检

Eureka 的客户端会默认每隔大约 30 秒会发送一次心跳给服务器端。这样来告诉我们的服务器端,自己还活着。但是在某些情况下客户端是活着的,但是却因为某些原因不能提供服务,这种情况,客户端也能告诉服务器端当前的状态,就可以使用 Eureka 的健康自检功能

开发步骤:

  • 在服务提供客户端添加依赖:
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  • 实现健康指示器接口:HealthIndicator 接口
  • 实现健康检查处理器:HealthCheckHandler 接口

相关文章

网友评论

      本文标题:二、SpringCloud 之 Eureka 框架入门

      本文链接:https://www.haomeiwen.com/subject/jnwszxtx.html