Eureka

Eureka入门案例:

在这里插入图片描述

案例Gitee地址:https://gitee.com/tianyinetwork/hei-ma-learn.git

搭建总流程:

1.搭建 Provider 和 Consumer 服务。

2.使用 RestTemplate 完成远程调用。

3.搭建 Eureka Server 服务。

4.改造 Provider 和 Consumer 称为 Eureka Client。

5.Consumer 服务 通过从 Eureka Server 中抓取 Provider 地址 完成 远程调用

RestTemplate :
  • Spring提供的一种简单便捷的模板类,用于在 java 代码里访问 restful 服务。
  • 其功能与 HttpClient 类似,但是 RestTemplate 实现更优雅,使用更方便。

总结:

  1. Eureka的使用:

    • 添加pom依赖,服务端就加server的,客户端就加client的

    • 编辑application.yml配置文件(注意间隔,文档不能保证格式一定正确哈!注意点,一般没问题)

      • 服务器端:

        eureka:
          instance:
            hostname: localhost   #主机名
          client:
            service-url:
        #      defaultZone: http://localhost:8761/eureka   # eureka服务端地址,将来客户端使用该地址和Eureka进行通信
              defaultZone: http://${eureka.instance.hostname}:${server.port}}/eureka   # eureka服务端地址,将来客户端使用该地址和Eureka进行通信
        #     注意:这个不是访问地址哈,这个是用在eureka-Client上的通信地址,让eureka-Client通过这个地址连接到eureka-server
            register-with-eureka: false   #是否将自己的路径注册到Eureka上。Eureka-Server是不需要的,Eureka的客户端需要
            fetch-registry: false         #是否需要从Eureka中抓取路径,Eureka-Server不需要的,Eureka-client才需要
          server:
            enable-self-preservation: false #关闭自我保护机制
            eviction-interval-timer-in-ms: 3000 #检查服务的时间间隔
        
      • 客户端:

        eureka:
          instance:
            hostname: localhost   #主机名
          client:
            service-url:
              defaultZone: http://localhost:8761/eureka   #eureka的地址,通过这个地址可以和Eureka-Server端取得通信
            register-with-eureka: true  #默认就是true   ,意思将自己的路径注册到Eureka上
            fetch-registry: true        #默认就是true   , 意思是需要从Eureka上拉取路径
        spring:
          application:
            name: eureka-consumer   #设置当前应用的名称。将来会在eureka中Application显示。将来需要使用该名称来获取路径
            #这里省略了两个属性哦
        
    • 启动类:

      • 服务器端:添加注解:@EnableEurekaServer
      • 客户端:添加注解:@EnableDiscoveryClient
    • 这样就完成了!

  2. RestTemplate的使用

    • 需要在Spring容器中注入一个Bean对象

      @Configuration
      public class RestTemplateConfig {
          @Bean
          public RestTemplate restTemplate(){
              return new RestTemplate();
          }
      }
      
    • 在类中注入

      @Autowired
      private RestTemplate restTemplate;
      
    • 使用最简单的方法getForObject(String,Class)方法来发起RestFul风格的请求并接收返回对象

      String url="http://"+host+":"+port+"/goods/findOne/"+id;
      Goods goods = restTemplate.getForObject(url, Goods.class);
      //第一个参数是请求地址,第二个参数是接收的返回值类型
      
  3. 通过Eureka获取服务地址

    • 在类中注入DiscoveryClient对象

      @Autowired
      private DiscoveryClient discoveryClient;
      
    • 通过DiscoveryClient类的getInstances(“服务名称”)获取服务集合

      List<ServiceInstance> instances = discoveryClient.getInstances("EUREKA-PROVIDE");
      //为什么是集合?
      //因为,我们在实际使用中都是做了高可用处理的,一个服务可能会有多个实例,所以要用集合
      
    • 需要判断下是否有数据

      if(instances==null||instances.size()==0){
          //集合没有数据
          return null;
      }
      //为什么要判断null和长度呢?
      //我们虽然通过方法获取了服务的服务对象的集合,但是,服务是有可能宕机的呀,所以这里要判断一下服务是否有数据
      
    • 获取集合中的服务对象

      ServiceInstance serviceInstance = instances.get(0);
      //因为我们这个案例只有一个服务对象,所以就获取集合的第一个喽!
      
    • 通过服务对象获取IP地址和端口号信息

      tring host = serviceInstance.getHost();
      System.out.println("IP地址是:"+host);
      int port = serviceInstance.getPort();
      System.out.println("服务的端口号是:"+port);
      
    • 得到了这些信息后,直接使用就行!例如:字符串拼接啥的

      String url="http://"+host+":"+port+"/goods/findOne/"+id;
      Goods goods = restTemplate.getForObject(url, Goods.class);
      
  4. Eureka的客户端常用配置:

    eureka:
      instance:
        hostname: localhost # 主机名
        prefer-ip-address: true # 将当前实例的ip注册到eureka server 中。默认是false 注册主机名
        ip-address: 127.0.0.1 # 设置当前实例的ip
        instance-id: ${eureka.instance.ip-address}:${spring.application.name}:${server.port} # 设置web控制台显示的 实例id
        lease-renewal-interval-in-seconds: 3 # 每隔3 秒发一次心跳包
        lease-expiration-duration-in-seconds: 9 # 如果9秒没有发心跳包,服务器呀,你把我干掉吧~
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka # eureka服务端地址,将来客户端使用该地址和eureka进行通信
    spring:
      application:
        name: eureka-provider # 设置当前应用的名称。将来会在eureka中Application显示。将来需要使用该名称来获取路径
    
  5. Eureka高可用

    • 需要配置系统host文件哦!

    • pom配置文件:

      • eureka-server1:

        server:
          port: 8761
        eureka:
          instance:
            hostname: eureka-server1   #主机名
          client:
            service-url:
        #      defaultZone: http://localhost:8761/eureka   # eureka服务端地址,将来客户端使用该地址和Eureka进行通信
              defaultZone: http://eureka2:8762/eureka   # eureka服务端地址,将来客户端使用该地址和Eureka进行通信
        #     注意:这个不是访问地址哈,这个是用在eureka-Client上的通信地址,让eureka-Client通过这个地址连接到eureka-server
            register-with-eureka: true   #是否将自己的路径注册到Eureka上。Eureka-Server是不需要的,Eureka的客户端需要
            fetch-registry: true         #是否需要从Eureka中抓取路径,Eureka-Server不需要的,Eureka-client才需要
          server:
            enable-self-preservation: false #关闭自我保护机制
            eviction-interval-timer-in-ms: 3000 #检查服务的时间间隔
        spring:
          application:
            name: eureka-server1
        
      • eureka-server2:

        server:
          port: 8762
        eureka:
          instance:
            hostname: eureka-server2   #主机名
          client:
            service-url:
        #      defaultZone: http://localhost:8761/eureka   # eureka服务端地址,将来客户端使用该地址和Eureka进行通信
              defaultZone: http://eureka1:8761/eureka   # eureka服务端地址,将来客户端使用该地址和Eureka进行通信
        #     注意:这个不是访问地址哈,这个是用在eureka-Client上的通信地址,让eureka-Client通过这个地址连接到eureka-server
            register-with-eureka: true   #是否将自己的路径注册到Eureka上。Eureka-Server是不需要的,Eureka的客户端需要
            fetch-registry: true         #是否需要从Eureka中抓取路径,Eureka-Server不需要的,Eureka-client才需要
          server:
            enable-self-preservation: false #关闭自我保护机制
            eviction-interval-timer-in-ms: 3000 #检查服务的时间间隔
        spring:
          application:
            name: eureka-server2
        

附录1:pom.xml:(乱序哦!有点是不能放到一起的,找自己需要的)

<!--SpringBoot的父依赖-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <!--JDK 版本-->
    <java.version>1.8</java.version>
    <!--spring cloud 版本-->
    <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>

<!--引入Spring Cloud 依赖-->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <!-- SpringMVC 依赖 -->
    <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>
    
    <!-- eureka-client -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    
    <!-- lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.20</version>
    </dependency>
</dependencies>

附录2:Eureka的全部配置:
Eureka的全部配置

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值