美文网首页
二.服务注册中心Eureka

二.服务注册中心Eureka

作者: 会飞的鱼_0cf5 | 来源:发表于2018-12-06 14:07 被阅读0次

    Eureka是Netflix开发的服务发现组件,本身是一个基于REST的服务。Spring Cloud将它集成在其子项目spring-cloud-netflix中,以实现Spring Cloud的服务发现功能

    服务注册

    在服务治理框架中, 通常都会构建一个注册中心, 每个服务单元向注册中心登记自己提供的服务, 将主机与端口号、 版本号、 通信协议等一些附加信息告知注册中心, 注册中心按服务名分类组织服务清单。

    服务发现

    由于在服务治理框架下运作, 服务间的调用不再通过指定具体的实例地址来实现, 而是通过向服务名发起请求调用实现。 所以, 服务调用方在调用服务提供方接口的时候, 并不知道具体的服务实例位置。 因此, 调用方需要向服务注册中心咨询服务, 并获取所有服务的实例清单, 以实现对具体服务实例的访问。

    1.创建服务注册中心(IntelliJ IDEA)
    File->New->Module->Spring Initializr

    image.png
    Next->Cloud Discovery->Eureka Server->Next->Finish
    image.png
    pom.xml引入了必要的依赖
    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
     </dependency>
    

    依赖项注意点:
    https://github.com/spring-projects/spring-cloud/wiki/Spring-Cloud-Edgware-Release-Notes
    spring-cloud-starter-eureka-server要替换为spring-cloud-starter-netflix-eureka-server,要不然会报错,找不到jar

    通过@EnableEurekaServer注解启动服务注册中心,这个注解需要在springboot工程的启动application类上加:

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

    在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,只需在 application.properties 中增加如下配置

    server.port=8761
    
    eureka.instance.hostname=localhost
    eureka.client.register-with-eureka=false
    eureka.client.fetch-registry=false
    eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
    
    • eureka.client.register-with-eureka: 由于该应用为注册中心,所以设置为 false, 代表不向注册中心注册自己
    • eureka.client.fetch-registry:由于注册中心的职责就是维护服务实例,它并不需要去检索服务, 所以也设置为 false。
      eureka server 是有界面的,启动工程,打开浏览器访问:http://localhost:8761界面如下:
      image.png
      可以看到如下图所示的 Eureka 信息面板, 其中 Instances currently registered with Eureka 栏是
    No application available 
    

    没有服务被发现,因为没有注册服务当然不可能有服务被发现了。

    1. 创建服务提供者 (eureka client)
      主要处理服务的注册与发现。客户端服务通过注解和参数配置的方式,嵌入在客户端应用程序的代码中,在应用程序运行时,Eureka客户端向注册中心注册自身提供的服务并周期性地发送心跳来更新它的服务租约,如果心跳超时,则通常将该实例从注册server中删除。同时,它也能从服务端查询当前注册的服务信息并把它们缓存到本地并周期性地刷新服务状态.。

    创建过程同服务注册中心类似
    pom.xml中依赖如下

    <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>
    </dependency>
    

    通过注解@EnableEurekaClient或@EnableDiscoveryClient 表明自己是一个eurekaclient.
    @EnableEurekaClient与@EnableDiscoveryClient的区别
    https://stackoverflow.com/questions/31976236/whats-the-difference-between-enableeurekaclient-and-enablediscoveryclient
    There are multiple implementations of "Discovery Service" (eureka, consul, zookeeper).@EnableDiscoveryClient lives in spring-cloud-commons and picks the implementation on the classpath. @EnableEurekaClient lives in spring-cloud-netflix and only works for eureka. If eureka is on your classpath, they are effectively the same.
    因我们的注册中心是Eureka,所以使用@EnableEurekaClient,其他注册中心则使用@EnableDiscoveryClient

    @SpringBootApplication
    @EnableEurekaClient
    @RestController
    public class HiApplication implements ApplicationListener<WebServerInitializedEvent> {
    
        public static void main(String[] args) {
            SpringApplication.run(HiApplication.class, args);
        }
    
        private int serverPort;
        @RequestMapping("/hi")
        public String home(@RequestParam String name) {
            return "hi " + name + ",serverPort:" + serverPort;
        }
    
        @Override
        public void onApplicationEvent(WebServerInitializedEvent event) {
            this.serverPort = event.getWebServer().getPort();
        }
    }
    

    还需要在 application.properties配置文件中,通过 spring.application.name属性来为服务命名,比如命名为hello-service。 再通过eureka.client.service-url.defaultZone属性来指定服务注册中心的地址

    eureka.client.service-url.defaultZone= http://localhost:8761/eureka/
    server.port=8762
    spring.application.name=hello-service
    

    服务之间相互调用一般是根据spring.application.name。

    分别启动服务注册中心,以及hello-service,打开浏览器访问:http://localhost:8761界面如下

    image.png
    可以看到一个服务已经注册在服务中了,服务名为hello-service,端口为8762
    1. 高可用注册中心
      Eureka Server的高可用实际上就是将自己作为服务向其他服务注册中心注册自己,这样就可以形成一组互相注册的服务注册中心, 以实现服务清单的互相同步, 达到高可用的效果
    • 2.1 在刚创建的Eureka 服务注册中心,resources文件夹下,创建配置文件application-peer1.properties,作为peer1服务中心的配置,并将serviceurl指向peer2
    spring.application.name=eureka-server
    server.port=8761
    eureka.instance.hostname=peer1
    eureka.client.serviceUrl.defaultZone=http://peer2:8861/eureka/
    
    • 2.2.创建另外一个配置文件application-peer2.properties,将serviceUrl指向peer1`
    spring.application.name=eureka-server
    server.port=8861
    eureka.instance.hostname=peer2
    eureka.client.serviceUrl.defaultZone=http://peer1:8761/eureka/
    
    • 2.3 在/etc/hosts文件中添加对peerl 和peer2的转换, 让上面配置的host形式的serviceUrl能在本地正确访间到; Windows系统路径为C:\Windows\System32\drivers\etc\hosts。添加如下两行
    127.0.0.1 peer1
    127.0.0.1 peer2
    
    • 2.4Maven Projects->对应server项目->Lifecycle->clean,package,来生成相应的jar包,命令窗口cd到包的路径,通过spring.profiles.active属性来分别启动peerl和peer2:
    java -jar  server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
    java -jar  server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
     
    

    此时访问注册中心http://peer1:8761/,可以看到已经有peer2节点的eureka-server了,同样http://peer2:8861/,也可以看到peer1节点的eureka-server

    image.png
    • 2.5 在设置了多节点的服务注册中心之后,再修改hello-service的application.properties 的配置
    eureka.client.serviceUrl.defaultZone= http://peer1:8761/eureka/,http://peer2:8861/eureka/
    server.port=8762
    spring.application.name=hello-service
    

    将eureka.client.serviceUrl.defaultZone指向了peer1跟peer2,启动服务后,在http://peer1:8761/http://peer2:8861/,可以看到都注册了hello-service

    相关文章

      网友评论

          本文标题:二.服务注册中心Eureka

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