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

  1. 修改配置文件application.yml
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/

  1. 主启动类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
@EnableEurekaServer
public class EurekaMain7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7001.class,args);
    }
}

记得添加@EnableEurekaServer注解

测试:在浏览器输入http://localhost:7001/
在这里插入图片描述
出现这个界面说明成功了
5. 这时没有服务提供者我们去修改一下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-payment800application.yml添加一下配置
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

在这里插入图片描述
7. 再主启动类上添加@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
@EnableEurekaClient
public class PaymentMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class,args);
    }
}

  1. 启动测试,先要启动EurekaServer,打开浏览器输入:http://localhost:7001/

在这里插入图片描述
可以看到CLOUD-PAYMENT-SERVICE注册了进来
在这里插入图片描述
在这里插入图片描述

  • cloud-consumer-order80注册进7001
  1. 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
 
  1. 在主启动类上添加@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服务
在这里插入图片描述
可以看到这个服务已经注册进来了。
浏览器输入:http://localhost/consumer/order/get/1
在这里插入图片描述
成功拿到了数据.!

2、 集群版eureka

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

  2. 修改windows的域名映射
    在这里插入图片描述
    在这里插入图片描述

  3. 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

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);
    }
}

  1. 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

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测试
在这里插入图片描述
在这里插入图片描述

  • 把将支付服务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可以直接调用服务而不用再关心地址和端口号,且该服务还有负载功能了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值