2、Spring Cloud之注册中心 Eureka

前言

算是正式开始学习 spring cloud 的项目知识了,大概的知道Springcloud 是由众多的微服务组成的,因此咱们如今一个一个的来学习吧。java

注册中心,在微服务中算是核心了。全部的服务都会注册到注册中心,请求服务的时候,并不会直接去请求服务地址,而是先经过注册中心再转到目的地址。虽然Eureka 已经中止维护了,可是咱们暂时使用起来仍是没有问题的。linux

Eureka 主要有服务注册中心、服务提供者和服务消费。不少时候服务消费者也是服务提供者。因此就 Eureka 而言,分为 Eureka 服务端和Eureka 客户端,服务端就是注册中心,客户端就是服务提供者和消费者。git

单机模式

好了,咱们动手搭建一个Eureka 的服务端吧先,服务端有单机模式和集群模式,咱们先来单机模式。程序员

更具上篇文章讲的,咱们使用maven 模块化开发,咱们建立一个父级maven项目,pom.xml 文件内容以下:github

<?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.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>cn.quellanan</groupId>
    <artifactId>SpringCloud</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>
    
    <modules>
        <module>eureka-server-8000</module>
        <module>eureka-server-8001</module>
        <module>eureka-server-8002</module>
        <module>zlflovemm</module>
    </modules>

</project>

能够看到文件中指定了spring boot 和Spring cloud 等基础依赖的版本,这样保证各个模块版本的一致性。web

子模块

接下来咱们建立一个eureka-server-8000 的子模块。spring

pom.xml

pom.xml的内容以下:apache

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>cn.quellanan</groupId>
        <artifactId>SpringCloud</artifactId>
        <version>1.0.0</version>
    </parent>
    <groupId>com.quellanan.springcloud</groupId>
    <artifactId>eureka-server-8000</artifactId>
    <version>1.0.0</version>
    <name>eureka-server-8000</name>
    <description>eureka project for Spring Boot</description>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>

能够看到继承了父级pom,额外的增长了 spring-cloud-starter-netflix-eureka-server 的依赖。segmentfault

@EnableEurekaServer

在启动类中增长@EnableEurekaServer 注解,表示启用Eureka 服务端。服务器

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

application.properties

配置文件中增长以下配置

spring.application.name=spring-cloud-eureka-server-8000
server.port=8000

#表示是否将本身注册到Eureka Server,默认为true。
eureka.client.register-with-eureka=true

# 表示是否从Eureka Server获取注册信息,默认为true。
eureka.client.fetch-registry=true

#设置与Eureka Server交互的地址,查询服务和注册服务都须要依赖这个地址。多个地址可以使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

如今咱们就能够启动项目看看
在这里插入图片描述
能够看到咱们将自身注册到了服务中。

Eureka 客户端

前面说了,服务提供者和服务消费者都是客户端,其实就是咱们具体的某一业务的项目。因此咱们再建立一个子模块。我这里分开吧,咱们分别建立服务提供者和服务消费者。

服务提供者

咱们建立一个eureka-client-provider的子模块,pom 文件中引入spring-cloud-starter-netflix-eureka-client。

<parent>
        <groupId>cn.quellanan</groupId>
        <artifactId>SpringCloud</artifactId>
        <version>1.0.0</version>
    </parent>
    groupId>com.quellanan.springcloud</groupId>
    <artifactId>eureka-client-provider</artifactId>
    <version>1.0.0</version>
    <name>eureka-client-provider</name>
    <description>eureka-client-provider 服务提供者</description>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

启动类中加入@EnableEurekaClient注解或者@EnableDiscoveryClient注解均可以。

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientProviderApplication {

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

}

application.properties 中增长以下配置

server.port=9000
#服务名,在注册时所用
spring.application.name=eureka-client-provider
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

这里指定的eureka的服务中心的地址为8000。如上配置就能够将服务注册到注册中心啦。
咱们在写一个测试接口。
建立一个IndexController 类,内容以下:

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "hello world ";
    }
}

服务消费者

咱们同样的建立一个 eureka-client-consumer的模块。pom文件以下:

<parent>
        <groupId>cn.quellanan</groupId>
        <artifactId>SpringCloud</artifactId>
        <version>1.0.0</version>
    </parent>

    <groupId>com.quellanan.springcloud</groupId>
    <artifactId>eureka-client-consumer</artifactId>
    <version>1.0.0</version>
    <name>eureka-client-consumer</name>
    <description>eureka-client-consumer 服务消费者</description>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

相对于服务提供者,咱们增长了Feign 依赖,主要用来发现服务和实现客户端负载均衡,咱们这里用来发现服务就能够了。

在启动类中@EnableDiscoveryClient 用来发现服务,并注入RestTemplate 的实例bean 用来对服务提供的接口进行调用。@LoadBalanced 是开启客户端负载均衡的,最开始我没有加这个注解,可是发现不加的话,服务消费者就不能经过服务名来获取可用的服务提供者的实例。因此这里你们能够试验一下。

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientConsumerApplication {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientConsumerApplication.class, args);
    }
}

咱们接下写一个接口,调用服务消费者,咱们建立一个IndexController,内容以下:

@RestController
public class IndexController {
    private static final String applicationName = "eureka-client-provider";
    @Autowired
    private RestTemplate restTemplate;
    @RequestMapping("/index")
    public String getHello(){
        String url = "http://"+ applicationName +"/hello";
        return  restTemplate.getForObject(url,String.class);
    }
}

这里咱们能够看到applicationName 就是服务提供者的服务名。实际中,一种类型的服务可能有好几台服务器,可能物理地址和ip不同,可是保证他们的服务名同样就能够了,这样服务消费者就能够经过服务名获取可用的列表,再经过复杂均衡策略选择其中一个实例访问。

最后咱们在application中加上以下配置:

server.port=9001
#服务名,在注册时所用
spring.application.name=eureka-client-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

测试

如今咱们启动注册中心和客户端这两个项目来看下。启动后,咱们输入

http://localhost:8000

在这里插入图片描述
能够发现咱们的客户端已经注册到注册中心啦。服务提供者和服务消费者都已经注册到了注册中心啦。咱们再来调接口试试。咱们输入以下:

http://localhost:9001/index

在这里插入图片描述
能够看到其实获取了9000服务提供者的接口。这样就实现了服务的注册和发现啦,并实现远程调用。

集群模式(高可用)

上面咱们咱们搭建的注册中心只是单机模式,只有一个Eureka 服务端,单实际应用中注册中心实际上是尤其重要的,因此就须要搭建集群环境,其实Eureka 对集群模式是自然的支持的,咱们搭建也很是简单。
为何这么说呢,咱们前面能够看到只要配置了eureka.client.serviceUrl.defaultZone 就就会被对应的注册中线检测到,因此咱们代码彻底同样,只须要将eureka.client.serviceUrl.defaultZone相互指引就能够了,就就能够简单的搭建一个高可用的环境。
下面咱们来搭建一个,由于咱们就一台服务器,因此就用不一样的端口,其实代码彻底同样的,只是配置文件中配置不同,咱们分别把三个分配置文件贴出来。
8000端口的

spring.application.name=spring-cloud-eureka-server-8000
server.port=8000

#表示是否将本身注册到Eureka Server,默认为true。
eureka.client.register-with-eureka=true

# 表示是否从Eureka Server获取注册信息,默认为true。
eureka.client.fetch-registry=true

#设置与Eureka Server交互的地址,查询服务和注册服务都须要依赖这个地址。多个地址可以使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/,http://localhost:8002/eureka/

8001端口:

spring.application.name=spring-cloud-eureka-server-8001
server.port=8001
#表示是否将本身注册到Eureka Server,默认为true。
eureka.client.register-with-eureka=true
# 表示是否从Eureka Server获取注册信息,默认为true。
eureka.client.fetch-registry=true
#设置与Eureka Server交互的地址,查询服务和注册服务都须要依赖这个地址。多个地址可以使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/,http://localhost:8002/eureka/

8002 端口

spring.application.name=spring-cloud-eureka-server-8002
server.port=8002
#表示是否将本身注册到Eureka Server,默认为true。
eureka.client.register-with-eureka=true
# 表示是否从Eureka Server获取注册信息,默认为true。
eureka.client.fetch-registry=true
#设置与Eureka Server交互的地址,查询服务和注册服务都须要依赖这个地址。多个地址可以使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/,http://localhost:8001/eureka/

咱们如今分别启动这个这三个配置文件,大家能够用profile 来指向,我这为了分明,直接建立了三个模块。启动后,咱们分别访问

http://localhost:8000/
http://localhost:8001/
http://localhost:8002/

在这里插入图片描述
这里能够看到其实已经相互监控了。咱们了解一下这两个配置参数。

#定义服务续约任务的调用时间间隔,默认30s
eureka.instance.lease-renewal-interval-in-seconds=30
#定义服务失效的时间默认90s
eureka.instance.lease-expiration-duration-in-seconds=90

咱们如今再将咱们的服务提供者和服务消费者注册进来,可是这里,须要修改的地方也是eureka.client.serviceUrl.defaultZone。将服务注册到集群中。

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/,http://localhost:8001/eureka/,http://localhost:8002/eureka/

而后启动项目可,能够看到注册到了注册中心,而且能够调用服务提供者提供的接口。
在这里插入图片描述

总结

最后画了一张图来讲明整个注册中心的架构图。
在这里插入图片描述

能够看到注册服务端能够是一个集群。相互注册监控。服务消费者和服务提供者都是服务客户端,都会将服务注册到服务中心,同时这些服务也均可以使是集群或者分布式的。服务提供者会从服务端获取服务提供者可用的服务实例列表,经过负载均衡策略选择其中某一实例进行调用。这个算是Eureka 的总结吧哈哈

番外

好啦,老是是写完了,这篇文章真是是卡了我好几天,有的地方写的不是很好,欢迎你们指点。
代码上传到github:
https://github.com/QuellanAn/...

后续加油♡

欢迎你们关注我的公众号 "程序员爱酸奶"

分享各类学习资料,包含java,linux,大数据等。资料包含视频文档以及源码,同时分享本人及投递的优质技术博文。

若是你们喜欢记得关注和分享哟❤

file

相关文章
相关标签/搜索