SpringCloud之Eureka实战

目录

一. Eureka简介

二. 实战

1.Eureka 服务端

2.服务提供者

3.服务消费者


一. Eureka简介

Eureka由Netflix开源,集成到了SpringCloud体系中,它是基于RestfulAPI风格的服务注册发现组件。

常用 服务注册发现组件还有Zookeeper、Consul、Nacos

Eureka包含2个组件,Eureka Server和Eureka Client,Eureka  Client为客户端,用于和服务端Eureka  Server进行交互;Eureka  Server为客户端,服务启动后通过Eureka  Client向Eureka  Server注册自己的信息。以便于Eureka  Client进行调用。每个生产者和每个消费者都属于Eureka  Client。

服务启动后会向Eureka Server发送心跳,默认每隔30s进行续约自己的信息;

Eureka Server在一定时间内没有收到心跳,默认为90s,则Eureka Server会注销掉自己该服务节点;

Eureka  Client每次调用服务都会缓存Eureka Server信息,当Eureka Server发生宕机时,服务依旧可以被调用。

二. 实战

本次实战新建2个Eureka Server、2个Eureka Client分别为

ShopEurekaServer8000

ShopEurekaServer8001

ShopProvider8100

ShopConsumer8200

1.Eureka 服务端

1.新建父工程 File ->New -> Project 

在父工程下的pom文件中添加依赖

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

2.在父工程下添加module 例如ShopEurekaServer8000,在pom文件中导入依赖

    <dependencies>
        <!--Eureka server依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

3.添加application.yml配置文件

server:
  port:
      8000
spring:
  application:
    name: ShopEurekaServer8000

eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

    #是否需要想服务中心注册自己
    register-with-eureka: false
    fetch-registry: false

4.添加Apllication启动类,并且需要添加@EnableEurekaServer注解,标识此类为Eureka Server

package com.zz;

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

@SpringBootApplication
@EnableEurekaServer
public class ShopEurekaServerApplication {

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

5.启动ShopEurekaServerApplication主类,可能会报错如下错误

 The following method did not exist:

    com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder;

The method's class, com.google.gson.GsonBuilder, is available from the following locations:

    jar:file:/C:jaja/.m2/repository/com/google/code/gson/gson/2.1/gson-2.1.jar!/com/google/gson/GsonBuilder.class

这是因为包冲突了,找到maven仓库下的jar包/C:/Users/jaja/.m2/repository/com/google/code/gson/gson ,直接删除对应报错的2.1即可

不报错的不理会

如果每次都启动项目都报错,可以在父pom下面,添加gson包,指定版本

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>

6.启动成功访问 localhost:8000

一个实例的Eureka Server服务就搭好了。单个注册中心如果挂掉,可能会对系统造成影响,所以生产环境中通常会配置Eureka Server集群来实现高可用。

高可用实现

偷懒模式,直接复制一份

1.复制一份ShopEurekaServer8000 ,改名为ShopEurekaServer8001

2.修改pom 中的artifactId 为ShopEurekaServer8001

3.修改父pom 文件,添加 module

4.分别修改配置文件

先修改ShopEurekaServer8000 的application.yml文件

server:
  port:
      8000
spring:
  application:
    name: ShopEurekaServer8000

eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/

    #是否需要想服务中心注册自己
    register-with-eureka: true
    fetch-registry: true

再修改先修改ShopEurekaServer8001 的application.yml文件

server:
  port:
      8001
spring:
  application:
    name: ShopEurekaServer8001

eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://localhost:8000/eureka/
    #是否需要想服务中心注册自己
    register-with-eureka: true
    #要不要去注册中心获取其他服务的地址
    fetch-registry: true

defaultZone 配置为互相指向对方,register-with-eureka 设为 true 并且在注册中心注册自己。fetch-registry 设为 true 要去注册中心获取其他服务的地址

5.分别重新启动,然后分别访问localhost:8000 或者 localhost:8001 

2.服务提供者

1.新建一个module,ShopProvider8100

2.在pom中引入如下依赖

        <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-web</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

3.修改配置文件application.yml

server:
  port:
      8100
spring:
  application:
    name: ShopProvider8100

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8000/eureka/

defaultZone 设置为 defaultZone: http://localhost:8000/eureka/,http://localhost:8000/eureka/ 也是可以的 ,因为两个服务端已经构成集群,所以,只填一个,另一个也是可以获取到的。

4.主类上添加注解 @EnableEurekaClient   或者  @EnableDiscoveryClient

5.顺便创建一个controller,为之后的消费者调用提供方法。

6.启动主程序类访问 localhost:8080

3.服务消费者

1.新建一个module,ShopConsumer8200

2.在pom中引入如下依赖

        <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-web</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.5.RELEASE</version>
            <scope>compile</scope>
        </dependency>

3.编写配置文件

server:
  port:
      8200
spring:
  application:
    name: ShopConsumer8200

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8000/eureka/

4.在主类上添加注解@EnableDiscoveryClient 并且引入RestTemplate Bean

package com.zz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class ShopConsumerApplication8200 {

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

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

5.启动ShopConsumerApplication8200主类,并访问

以上就是SpringCloud 中Eureka的简单使用,并且添加了 服务提供者和服务消费者,来模式现实场景的调用链路。

文章中有错误的地方,希望各位多多指正,共同进步。

猜你喜欢

转载自blog.csdn.net/jaja_zz/article/details/113695339