5.Eureka服务注册与发现

Eureka包含了两个组件:Eureka Server和Eureka client
1.Eureka Server提供服务注册服务:
    各个微服务节点铜鼓配置,启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中叫存储所有可用的服务节点的信息,服务节点的信息可以在界面中直接看到
    
2.Eureka Client通过注册中心访问
    是一个java客户端,用于简化Eureka Server的交互,客户端会同时具备一个内置的,使用轮训(round-robin)负载算法的负载均衡器
    在应用启动后,将会向Eureka Server发送心跳(默认周期30秒)。如果Eureka Server在多个心跳周期内没有接受到某个节点的心跳,EurekaServer会将服务注册表中的该服务节点移除掉(默认90秒)
单机搬和集群版部署学习参考地址:https://blog.csdn.net/qq_44732146/article/details/118529882

Eureka服务注册与发现

1、 单机版eureka

1.新建modulecloud-eureka-server7001

2.修改pom文件,引入以下的依赖

<dependencies>
        <!--        eureka依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.zhubayi.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
  • 1.x与2.x说明
以前的老版本(当前使用2018)

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

现在新版本(当前使用2020.2)

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

3.修改配置文件application.yml

#重点1:eureka的下述配置为false,表示不注册自己本身,并设置服务交互地址
server:
  port: 7001

eureka:
  instance:
    hostname: localhost #eureka服务端的实例名称
  client:
    #false表示不向注册中心注册自己。
    register-with-eureka: false
    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
    #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

4.主启动类EurekaMain7001

package com.zhubayi.springcloud;

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

/**
 * @author zhubayi
 */
@SpringBootApplication
//注意在eureka的服务端启动类上加上改标签,表明当前是eureka的服务端插件!
@EnableEurekaServer
public class EurekaMain7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7001.class,args);
    }
}
记得添加@EnableEurekaServer注解
测试:在浏览器输入http://localhost:7001/
出现这个界面说明成功了!



2.服务端代码(自己写的分布式服务端代码:里面包含了操作增删改查的多项业务操作)

这时没有服务提供者我们去修改一下cloud-provider-payment8001模块把它注册进7001
在cloud-provider-payment8001模块pom添加一个添加一个依赖

<!--eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
这时cloud-provider-payment8001的依赖

  1. 在cloud-provider-payment800的application.yml添加一下配置

spring:
  application:
    name: Cloud-Payment-Service
eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      #单机版
      defaultZone: http://localhost:7001/eureka
      # 集群版
      #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  instance:
    instance-id: payment8001
    #访问路径可以显示IP地址
    prefer-ip-address: true
    #Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
    #lease-renewal-interval-in-seconds: 1
    #Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
    #lease-expiration-duration-in-seconds: 2
这里有两个设置:
  1.instance-id:设置插件在eureka中的显示名称
2.prefer-ip-address: true访问路径是否显示ip
2.. 再主启动类上添加@EnableEurekaClient注解
package com.zhubayi.springcloud;

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

/**
 * @author zhubayi
 */
@SpringBootApplication
//重点1:加的是客户端@EnableEurekaClient的注解
@EnableEurekaClient
public class PaymentMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class,args);
    }
}
3.启动测试,先要启动EurekaServer,打开浏览器输入:http://localhost:7001/

注册服务调用端
  • 把cloud-consumer-order80注册进7001
  • cloud-consumer-order80在pom添加以下依赖
<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 </dependency>

 

  1. 修改cloud-consumer-order80的配置文件
server:
  port: 80

spring:
    application:
        name: cloud-order-service

eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka
2.在主启动类上添加@EnableEurekaClient
package com.zhubayi.springcloud;

import com.zhubayi.myrule.MySelfRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;

/**
 * @author zhubayi
 */
@EnableEurekaClient
@SpringBootApplication
//@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = MySelfRule.class)
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class,args);
    }
}
测试:先要启动EurekaServer,7001服务,再要启动服务提供者provider,8001服务

 

可以看到这个服务已经注册进来了。
成功拿到了数据.!

2、 集群版eureka

  1. 参考cloud-eureka-server7001,新建cloud-eureka-server7002,就把7001的拷贝一份,改改7002的配置文件.

  2. 修改windows的域名映射

 

7002的配置文件

server:
  port: 7002
eureka:
  instance:
    hostname:  eureka7002.com #eureka服务端的实例名称
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      #集群指向其它eureka:形成相互守望
      defaultZone: http://eureka7001.com:7001/eureka/
      #单机就是7001自己
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    #server:
    #关闭自我保护机制,保证不可用服务被及时踢除
    #enable-self-preservation: false
    #eviction-interval-timer-in-ms: 2000
2.7002启动类

package com.zhubayi.springcloud;

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

/**
 * @author zhubayi
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7002 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7002.class,args);
    }
}
3.7001配置文件

server:
  port: 7001
eureka:
  instance:
    hostname:  eureka7001.com #eureka服务端的实例名称
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      #集群指向其它eureka:形成相互守望!
      defaultZone: http://eureka7002.com:7002/eureka/
      #单机就是7001自己
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  #关闭自我保护机制,保证不可用服务被及时踢除
  server:
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 2000
4.7001的启动类

package com.zhubayi.springcloud;

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

/**
 * @author zhubayi
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7001.class,args);
    }
}
启动7001,7002测试
1.7001页面可以看到7002:形成相互守望

2.7001页面可以看到7001:形成相互守望

把将支付服务8001微服务发布到上面2台Eureka集群配置中
修改8001的配置文件

将订单服务80微服务发布到上面2台Eureka集群配置中

启动8001和80
7001端口

7002端口

可以看到成功注册了进来,浏览器输入:http://localhost/consumer/order/get/2

  • 成功获取到了数据。
支付服务提供者8001集群环境构建
  • 参考cloud-provider-payment8001,新建cloud-provider-payment8002,就是把8001拷贝一份,然后把8002的端口改了。
  • 配置文件
  • server:
      port: 8002
    spring:
      application:
        name: cloud-payment-service
      datasource:
        url: jdbc:mysql://127.0.0.1:3306/springcloud2020?useUnicode=true&characterEncoding=utf-8&useSSL=NO&serverTimezone=Asia/Shanghai
        type: com.alibaba.druid.pool.DruidDataSource
        username: root
        password: 123456
        driver-class-name: com.mysql.cj.jdbc.Driver
    
    mybatis:
      mapper-locations: classpath:mapper/*.xml
      type-aliases-package: com.zhubayi.springcloud.entities
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    eureka:
      client:
        #表示是否将自己注册进EurekaServer默认为true。
        register-with-eureka: true
        #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
        fetchRegistry: true
        service-url:
          #单机版
          #defaultZone: http://localhost:7001/eureka
          # 集群版
          defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
      instance:
        instance-id: payment8002
        #访问路径可以显示IP地址
        prefer-ip-address: true
        #Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
        #lease-renewal-interval-in-seconds: 1
        #Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
        #lease-expiration-duration-in-seconds: 

 

订单服务访问地址不能写死,把OrderController改一改
private static final String PAYMENT_URL="http://CLOUD-PAYMENT-SERVICE";
然后使用@LoadBalanced注解赋予RestTemplate负载均衡的能力
@LoadBalanced //开启负载均衡

启动测试:先要启动EurekaServer,7001/7002服务再要启动服务提供者provider,8001/8002服务
启动好之后浏览器输入:http://localhost/consumer/order/get/2

成功的注册了进来。

端口是8001再刷新一下

端口变成了8002
Ribbon和Eureka整合后Consumer可以直接调用服务而不用再关心地址和端口号,且该服务还有负载功能了。

posted @ 2022-05-29 16:31  努力的达子  阅读(103)  评论(0编辑  收藏  举报