美文网首页
Spring Cloud Eureka搭建注册中心

Spring Cloud Eureka搭建注册中心

作者: 32a08e1b7af0 | 来源:发表于2019-05-24 21:42 被阅读0次

    Eureka是Netflix开源微服务框架中的一系列项目中的一个,Spring Cloud对其进行了二次封装,形成了Spring Cloud Netflix子项目,但未对 Netflix微服务实现原理进行更改,只是进行了Spring Boot化,使开发更容易使用和融合,

    在微服务方案中,Eureka对于服务治理有3个重点

    服务治理服务器(Eureka服务器): 服务注册中心,负责服务列表的注册,维护和查询等功能,
    服务注册代理(服务提供者): 如果一个微服务是服务提供者,可以通过服务注册代理将服务配置信息注册到服务治理服务器上。服务注册代理可以理解为一个Eureka客户端,负责将微服务所提供的服务向Eureka服务器进行注册,续约和销毁等,以使服务消费者可以发现服务并进行消费。在服务注册时需要向服务治理服务器提供服务名称,宿主服务器Ip地址,服务端口号,域名等主要数据。
    服务发现者(服务消费者): 也是一个Eureka客户端,它在启动时默认从所有服务治理服务器中获取所有的服务注册信息,并通过所获取的服务注册列表信息来消费相应的服务。

    服务注册中心搭建(Eureka)

    创建maven工程,导入相应的包
    创建application.yml文件,修改相应的值
    编写SpringBoot启动类

    pom.xml包依赖

    <dependencies>
    
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-web</artifactId>
               <version>1.5.16.RELEASE</version>
           </dependency>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>Dalston.RELEASE</version>
           </dependency>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-starter-eureka-server</artifactId>
               <version>1.3.1.RELEASE</version>
           </dependency>
       </dependencies>
    <build>
           <plugins>
               <plugin>
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-compiler-plugin</artifactId>
                   <version>3.8.0</version>
                   <configuration>
                       <source>1.8</source>
                       <target>1.8</target>
                   </configuration>
               </plugin>
               <plugin>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-maven-plugin</artifactId>
                   <configuration>
                       <!-- 指定该Main Class为全局的唯一入口 -->
                       <mainClass>com.study.EureKaApplication</mainClass>
                       <layout>ZIP</layout>
                   </configuration>
                   <executions>
                       <execution>
                           <goals>
                               <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
                           </goals>
                       </execution>
                   </executions>
               </plugin>
           </plugins>
       </build>
    

    创建application.yml文件

    spring:
      profiles:
        active: eu
    
    info:
      port: ${server.port}
    
    ---
    server:
      port: 7001
    spring:
      profiles: eu
    
    eureka:
      instance:
        hostname: eureka-server.com #eureka服务端的实例名称
      client:
        register-with-eureka: false #false表示不向注册中心注册自己。
        fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
        service-url:
          defaultZone: http://eureka-client1.com:7002/eureka/,http://eureka-client2.com:7003/eureka/
    
    ---
    server:
      port: 7002
    
    spring:
      profiles: eu_c1
    
    eureka:
      instance:
        hostname: eureka-client1.com #eureka服务端的实例名称
      client:
        register-with-eureka: false #false表示不向注册中心注册自己。
        fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
        service-url:
          defaultZone: http://eureka-server.com:7001/eureka/,http://eureka-client2.com:7003/eureka/
    
    ---
    server:
      port: 7003
    spring:
      profiles: eu_c2
    
    
    eureka:
      instance:
        hostname: eureka-client2.com #eureka服务端的实例名称
        prefer-ip-address: true
        instance-id: eureka-client2.com
      client:
        register-with-eureka: false #false表示不向注册中心注册自己。
        fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
        service-url:
          defaultZone: http://eureka-server.com:7001/eureka/,http://eureka-client1.com:7002/eureka/
    
    

    创建SpringBoot启动类

    @SpringBootApplication
    @EnableEurekaServer**开启Eureka服务**
    public class EureKaApplication {
    
       public static void main(String[] args) {
           SpringApplication.run(EureKaApplication.class, args);
       }
    }
    
    

    以上我搭建了三台Eureka服务器,进行相互注册,使用了域名引向,需要在C:\Windows\System32\drivers\etc\hosts文件中加入以下配置

    127.0.0.1 eureka-client1.com
    127.0.0.1 eureka-client2.com
    127.0.0.1 eureka-server.com

    在application.yml中有三个spring.profiles.active,如果使用Idea可以通过配置荐构建三个不同的应该启动
    -Dspring.profiles.active=eu -Xmx100m -Xmn100m
    -Dspring.profiles.active=eu_c1 -Xmx100m -Xmn100m
    -Dspring.profiles.active=eu_c2 -Xmx100m -Xmn100m

    image.png
    image.png

    也可以在项目中target下找到对应的jar包运行

    启动成功后,访问相应的应用,如下图表示成功。

    image.png

    几个重要参数:

    preferIpAddress: 是否优先使用IP地址作为主机名的标识,默认值:false
    leaseRenewwalIntervalInSeconds: Eureka客户端向服务端发送心跳的时间间隔,单位为秒,默认值:30
    leaseExpirationDurationInSeconds: Eureka服务端收到最后一次心跳之后等待的时间上限,单位为秒,超过该时间之后服务端将该服务实例从服务清单中剔除,从而禁止服务请求调用被发送到该实例上,默认值:90
    nonSecurePort: 非安全的通信端口号,默认值:80
    securePort: 安全的通信端口号,默认值:443
    nonSecurePortEnable: 是否启用非安全的通信端口号,默认值:true
    securePortEnable: 是否启用安全的通信端口号,默认值:false
    appname: 服务名,默认取spring.application.name的配置值,如果没有则为unkown
    hostname: 主机名,不配置的时候将根据操作系统的主机名来获取

    如有疑问,请添加QQ:2646409029,备注:简书,谢谢!

    相关文章

      网友评论

          本文标题:Spring Cloud Eureka搭建注册中心

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