springcloud(三)--服务注册与发现Eureka

如题,本篇我们来引入SpringCloud中服务注册与发现组件--Eureka,然后将SIM服务提供者向Eureka服务器进行注册。 

一、Eureka 是什么

Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。
Eureka包含两个组件:Eureka Server和Eureka Client。
Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。
Eureka Client是一个java客户端,用于简化与Eureka Server的交互,客户端同时也就别一个内置的、使用轮询(round-robin)负载算法的负载均衡器。
在应用启动后,将会向Eureka Server发送心跳,默认周期为30秒,如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移除(默认90秒)。
Eureka Server之间通过复制的方式完成数据的同步,Eureka还提供了客户端缓存机制,即使所有的Eureka Server都挂掉,客户端依然可以利用缓存中的信息消费其他服务的API。综上,Eureka通过心跳检查、客户端缓存等机制,确保了系统的高可用性、灵活性和可伸缩性。

二、搭建Eureka服务器

新建一个新的maven工程,命名为sim-eureka

pom.xml 

<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>
 
  <artifactId>sim-eureka</artifactId>
  <packaging>jar</packaging>
 
  <name>sim-eureka</name>
  <url>http://maven.apache.org</url>
 
   
 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <!-- <version>3.8.1</version> -->
      <scope>test</scope>
    </dependency>
     
          
         
         
        <!-- starer-eureka-server  starter 自动化配置  -->
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <!--  <version>1.4.5.RELEASE</version> -->
        </dependency>
          
         
       <!--devtool  修改后立即生效,热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>provided</scope>
        </dependency>
     
  </dependencies>
  <parent>
    <groupId>com.tingcream.sim</groupId>
    <artifactId>sim-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../sim/pom.xml</relativePath>
  </parent>
</project>

springboot入口类 SimEurekaServerApp.java

package com.tingcream.simEureka;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
 
@SpringBootApplication
@EnableEurekaServer
public class SimEurekaServerApp  {
     public static void main(String[] args) {
            SpringApplication.run(SimEurekaServerApp.class, args);
     }
}

application.yml


server:
  port: 8761

spring:
  application:
    name: sim-eureka

eureka: 
  instance:
    hostname: localhost
    prefer-ip-address: true
  client:
    register-with-eureka: false  #默认true,单机环境设置为false,表示不向注册中心注册自己
    fetch-registry: false     #默认true,单机环境设置为false,表示不需要向注册中心检索自己
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      

启动SimEurekaServerApp ,浏览器访问http://localhost:8761/ 进入Eureka的后台界面

可以看到,现在Eureka服务器上还没有应用在上面注册,下面我们尝试把sim-provider 往Eureka服务器上进行注册。

在sim-provider工程中,进行修改

pom.xml 中添加 spring-cloud-starter-eureka 和 spring-boot-starter-actuator 起步依赖。

<!-- eureka 客户端starter  -->
       <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-eureka</artifactId>
      </dependency>
      <!-- actuator监控引入 -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-actuator</artifactId>
      </dependency>

application.yml

#服务器基本配置
server:
  port: 7001
  context-path: /
  

#devtool 热加载工具
spring:
  devtools:
    restart:
      enabled: true
      exclude: resources/**
#spring应用名称  
  application:
    name: sim-provider
    
    
eureka: 
  instance: 
    hostname: localhost  #eureka客户端主机实例名称
  client: 
    service-url: 
      defaultZone: http://localhost:8761/eureka   #把服务注册到eureka注册中心    

info: 
   groupId: com.tingcream.sim
   artifactId: sim-provider
   version: 0.0.1-SNAPSHOT
   developer: 张三
   email: [email protected]

注意,其中spring.application.name 是个很重要的配置参数,eureka 客户端默认会使用这个参数值作为注册的应用名称。

SimProvderApp 启动类上添加注解@EnableEurekaClient

package com.tingcream.simProvider;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.aop.AopAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
 
 
 
@SpringBootConfiguration
@EnableAutoConfiguration(exclude={   //排除自动配置
        DataSourceAutoConfiguration.class,//数据库 数据源
        DataSourceTransactionManagerAutoConfiguration.class,//数据库事务
        JdbcTemplateAutoConfiguration.class,//jdbcTemplate
        AopAutoConfiguration.class
        })
@ComponentScan
@ImportResource({"classpath:/spring.xml"})
@PropertySource("classpath:/jdbc.properties")
@EnableEurekaClient
public class SimProviderApp extends SpringBootServletInitializer{
   
    @Override
    protected SpringApplicationBuilder configure(
            SpringApplicationBuilder builder) {
             
         return builder.sources(SimProviderApp.class);
    }
 
    public static void main(String[] args) {
        SpringApplication.run(SimProviderApp.class, args);
    }
     
}

ok,我们现在再启动下SimProviderApp ,再到访问下Eureka管理后台看下。

我们发现 Instances currently registered with Eureka (当前Eureka中注册的实例) 中出现了一条记录,是刚刚我们启动并注册的sim-provider应用。

注意上边还有一串红色的警告语,大意是说Eureka可能未被正确地声明,为了安全起见,实例不会过期。 这个是Eureka的一个自我保护机制,我们开发过程中可能会时常遇到这个,现在我们可以不用管它,直接忽略即可。我们再点击下 localhost:sim-provider:7001 查看详情,展示了我们在application.yml中配置的info参数信息。

ok,到这里我们的Eureka服务器就搭建成功了,并且成功地把sim-provider服务向Eureka服务器进行了注册。 

猜你喜欢

转载自blog.csdn.net/jasnet_u/article/details/81807862