继续来看,上篇记到这次用到的环境,那接下来首先就是要建一个父项目,父项目里不写代码,一般作为组件版本号控制用,截止到写完注册中心,父项目结构如下

1.PNG

然后就开始写注册中心了,注册中心的作用是对整个微服务系统的服务注册和服务发现,以及对服务健康状态的监控和管理功能,他自己本身不写业务类代码。

image20200709124952525.png

springcloud集成的比较火的几个服务注册中心组件分别是 eureka、nacos、consul、zk,先从eureka说起

在这里写服务注册中心的Server和Client的例子比较好写,其实就三步,引依赖,写配置,加注解,然后注册中心就起来了

<?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">
    <parent>
        <artifactId>springcloud_parent</artifactId>
        <groupId>com.jin</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud_01eureka_server</artifactId>

    <dependencies>
        <!--引入springbootweb-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--引入 eureka server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>


</project>

这里的配置文件写的比较全了,把去立即注册和集群也加上了

#eureka server 端口号  默认就是8761
server.port=8761
#指定服务名称 唯一标识 注意:服务名不能出现下划线  默认服务名不区分大小写  推荐服务名大写
spring.application.name=eurekaserver
#指定服务注册中心 暴露服务地址             集群
eureka.client.service-url.defaultZone=http://localhost:8761/eureka,http://localhost:8762/eureka
#图形化地址直接访问http://localhost:8761

#启动时的报错是因为eureka在启动时也会作为client去启动注册,因为同时也作为server
#在启动,默认启动时立即注册,所以client会找不到server然后报错

#关闭eureka client立即注册
eureka.client.fetch-registry=false
#让当前应用仅仅是服务注册中心
eureka.client.register-with-eureka=false
package com.jin;

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

/**
 * @author jinyunlong
 * @date 2021/7/6 16:07
 * @profession ICBC锅炉房保安
 */
@SpringBootApplication
@EnableEurekaServer  //开启当前应用是一个服务注册中心
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class,args);
    }
}

如此三步,注册中心就搭建起来了:

2.PNG

注:我这里启动不会报错是因为加了关闭自己注册自己的注解了

为啥启动会出现报错是因为eureka组件包含 eurekaserver 和 eurekaclient。server是一个服务注册中心,用来接受客户端的注册。client的特性会让当前启动的服务把自己作为eureka的客户端进行服务中心的注册,当项目启动时服务注册中心还没有创建好,所以找不到服务的客户端组件就直接报错了。我的配置文件注释里也有写,加最下头的配置参数关闭立即注册就行了

然后就是开发client端,流程和server是一样的,就是引入的依赖和注解及配置项有细微差异,贴一下:

<!--引入eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

#指定服务名称
spring.application.name=EUREKACLIENT

#指定服务注册中心地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka

@EnableEurekaClient   //让当前微服务作为一个eureka serve客户端 进行服务注册

这里我的配置项也是做了指定的三个集群地址,还有在配置文件里如果加入

#用来修改eureka server默认接受心跳的最大时间 默认是90s
#eureka.instance.lease-expiration-duration-in-seconds=10

#指定客户端多久向eureka server发送一次心跳 默认是30s
#eureka.instance.lease-renewal-interval-in-seconds=5

会修改心跳检测时间,不建议生产这么用,因为eureka的自我保护机制的原理就是"宁可信其有,不可信其无"

3.PNG

不谈自我保护机制,我也没去刻意试,client就算写完了,启动client可以看到他已经被注册中心发现

这里有一个意外收获是,因为我是写完代码后写的文章,代码里server端已经做了集群,然后现在只是启动了一个server,所以这个client一开始启动时并没有注册到服务中心,而是过了几秒才注册到,感觉应该去轮询了。

4.PNG
server的集群就是相同的项目用不同的端口启动,然后实现client共享,这里用jvm命令启动的话会覆盖配置文件写的默认端口,好处是不用多复制相同的项目,但是麻烦的一点就是下头那行要来回改(比如现在就是以8763端口启动server,同时作为client要去注册到8761和8762的server上)

#指定服务注册中心 暴露服务地址             集群
eureka.client.service-url.defaultZone=http://localhost:8761/eureka,http://localhost:8762/eureka

5.PNG

多拉几个相同的项目也无所谓啦其实。

然后耐非公司就说eureka2.x停止维护了,只有1.x会继续活跃。好家伙,那赶紧看看别的组件:

Consul是谷歌家基于golang写的组件,也很好用,下载下来是个exe,必须用cmd启动,启动后就充当server的作用了,不用重写这块的代码确实比较方便。

启动exe指令(不加环境变量的话在cmd consul.exe所在路径下 consul agent -dev)

同样也是三步,引依赖、写配置、加注解:

<?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">
    <parent>
        <artifactId>springcloud_parent</artifactId>
        <groupId>com.jin</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud_03consul_client</artifactId>

    <dependencies>
        <!--引入springbootweb-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--引入consul依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <!-- 这个包是用做健康度监控的-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

    </dependencies>


</project>
#指定服务端口
server.port=8082
#指定服务名称
spring.application.name=CONSULCLIENT

#consul server 服务注册地址
spring.cloud.consul.discovery.hostname=localhost
spring.cloud.consul.port=8500
#执行注册当前服务的服务名称 默认:${spring.application.name}
spring.cloud.consul.discovery.service-name=${spring.application.name}

#关闭健康检查 注意:在生产情况下不推荐关闭健康检查
#spring.cloud.consul.discovery.register-health-check=false

package com.jin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author jinyunlong
 * @date 2021/7/7 19:18
 * @profession ICBC锅炉房保安
 */
@SpringBootApplication   //代表这是一个springboot入口应用
@EnableDiscoveryClient      //代表这是一个consul client  @EnableDiscoveryClient很形象的注解,针对不同的服务注册客户端的通用注解 代表 consulclient zk client nacos client
public class ConsulClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsulClientApplication.class,args);
    }
}

有几点要注意:1、依赖里的健康检查包要加一下,要不然在可视化注册中心的这个client下健康检测一直是红叉

2、配置文件主机端口分开写,健康检查不要随便关 3、启动类@EnableDiscoveryClient注解是通用注解
然后就能看到client成功注册到服务中心了,并且健康检查通过了

6.PNG

好像consul相比eureka,少了自我保护机制

总结下这两个服务注册中心的要点

eureka:1、写server和client都是三步 引依赖、写配置、加注解 2、改配置文件对自我保护机制和自己注册自己进行优化 3、集群的写法和jvm参数覆盖 4、2.x不再维护,1.x可以继续用

consul:1、exe的启动方式(不用自己手写server代码) 2、client的一些写法和注意事项

注:这里的所有client仅是针对于服务注册中心的server,它可以称作client,其实到了真正的业务逻辑里,它们也是一个个拆分微服务后的sever。


标题:再战SpringCloud(2)
作者:jyl
地址:http://jinyunlong.xyz/articles/2021/07/07/1625671248349.html