springcloud(一)注册中心eureka

1,603 阅读2分钟

eureka介绍

eureka属于springcloud中的一个组件,叫注册中心,什么叫注册中心?它是用来干什么的呢?

作用:服务注册与发现,在分布式环境中,一个完整的系统可能是由几个、几十、或者几百个服务组合在一起的一套架构, eureka就是将这些毫不相关的服务管理到一起。

服务发现是基于微服务的体系结构的主要宗旨之一。尝试手动配置每个客户端或某种形式的约定可能很困难并且很脆弱。Eureka是Netflix Service Discovery服务器和客户端。可以将服务器配置和部署为高可用性,每个服务器将有关已注册服务的状态复制到其他服务器。

搭建eureka

1.新建工程:

2.引入依赖


        1.8
        Hoxton.RC2

            org.springframework.cloud
            spring-cloud-starter

            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server

            org.springframework.boot
            spring-boot-starter-test
            test

                    org.junit.vintage
                    junit-vintage-engine

                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import

                org.springframework.boot
                spring-boot-maven-plugin

            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone

这是cloud必须的依赖

cloud版本,cloud的版本都喜欢以城市名命名。

其中eureka的依赖只有一个:

3.编写配置文件

我将application.perproties文件修改为了bootstrap.yml文件,因为cloud支持这种格式,也推荐这种格式。

server:
  port: 8761
eureka:
  client:
    service-url:
      defaultZone: http://localhost:${server.port}/eureka/
    register-with-eureka: false
register-with-eureka:  是否把自己作为服务注册到其他服务注册中心

4.开启eureka 开启eureka只需要在启动类中加入一个注解即可:

package com.ymy;

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

@SpringBootApplication
@EnableEurekaServer //开启eureka的注解,一定要加这个注解,否者没有效果
public class CouldEurekaApplication {

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

}

到这里eureka的配置就基本完成了,下面启动项目

启动成功,由于eureka提供了窗口界面,在浏览器中输入:localhost:8761

看到这个界面代表eureka已经被启动成功,服务可以正常注册了。

如果启动失败,报配置文件的错误的话,可以引入下面的依赖:


            org.springframework.boot
            spring-boot-starter-web

新建客户端服务一:cloud-user

1.引入依赖


        1.8
        Hoxton.RC2

            org.springframework.boot
            spring-boot-starter-web

            org.springframework.cloud
            spring-cloud-starter

            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client

            org.springframework.boot
            spring-boot-starter-test
            test

                    org.junit.vintage
                    junit-vintage-engine

                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import

                org.springframework.boot
                spring-boot-maven-plugin

            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone

2.编写配置文件

server:
  port: 8800
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: cloud-user

3.在启动类中开启eureka客户端注解

package com.ymy.coulduser;

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

@SpringBootApplication
@EnableDiscoveryClient   //开启eureka客户端注解
public class CouldUserApplication {

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

}

新建客户端二:cloud-order

依赖和cloud-user一样,请参考cloud-user,配置文件如下:

server:
  port: 8900
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: cloud-order

其实也是一样的,就是服务名和端口不一样,在启动类中也要加入启动eureka客户端的注解。

启动两个项目

我们在打开eureka的管理界面:localhost:8761

可以看到cloud-user、cloud-order服务已经成功的注册到注册中心了。

由于eureka2.x不在进行维护了,所以选用eureka做注册中心的小伙伴最好将版本降到1.9.x,或者选择其他的注册中心,如:consul、zookeeper等等都是不错的选择。