SpringCloud组件-服务注册中心Eureka

服务注册中心

在传统的rpc远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。

Dubbo架构图在这里插入图片描述

Eureka

Eureka采用了CS的设计架构,Eureka Server 作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务使用Eureka的客户端连接到Eureka Server并维持心跳连接。这样系统的维护人员就可以通过Eureka Server 来监控系统中各个微服务是否正常运行。

在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把当前自己服务器的信息比如服务地址通讯地址等以别名方式注册到注册中心上。另一方(消费者 | 服务提供者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地RPC调用RPC远程调用框架核心设计思想:在于注册中心,因为使用注册中心管理每个服务与服务之间的一个依赖关系(服务治理概念)。在任何rpc远程框架中,都会有一个注册中心(存放服务地址相关信息(接口地址))

在这里插入图片描述

依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

启动器@EnableEurekaServer

package cn.edu.guet;

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

/**
 * @author pangjian
 * @ClassName EurekaMain
 * @Description EurekaServer启动器
 * @date 2021/9/10 16:54
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain {

    public static void main(String[] args) {
        SpringApplication.run(EurekaMain.class, args);
    }

}

配置yml

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

测试

# 访问后到Eureka官网则测试通过
http://localhost:7001/

在这里插入图片描述


微服务注册

将服务消费者和服务提供者注册到EurekaService,两者成为EurekaService的EurekaClient。EurekaService相当于婚介所,服务消费者是要娶老婆的男人,服务提供者是待嫁的女人。

消费者和提供者依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

启动类加@EnableEurekaClient

package cn.edu.guet;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @author pangjian
 * @ClassName OrderMain
 * @Description TODO
 * @date 2021/9/10 15:12
 */
@SpringBootApplication
@EnableEurekaClient
public class OrderMain {

    public static void main(String[] args) {
        SpringApplication.run(OrderMain.class, args);
    }

    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

}

配置yml

spring:
  application:
  	# 微服务名
    name: cloud-order-service

eureka:
  client:
    # 表示是否将自己注册进EurekaServer
    register-with-eureka: true
    # 是否从EurekaServer抓取已有的注册信息,集群必须设置true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

测试成功注册

在这里插入图片描述


Eureka集群搭建

修改配置

C:\Windows\System32\drivers\etc路径下的hosts文件,添加下面配置

127.0.0.1       eureka7001.com
127.0.0.1       eureka7002.com

修改yml配置

EurekaServer相互注册,就可以搭建高可用集群

7002号EurekaServer

server:
  port: 7002

eureka:
  instance:
    # eureka服务端的实例名称
    hostname: eureka7002.com
  client:
    # false 表示不向注册中心注册自己
    register-with-eureka: false
    # false 表示自己端就是注册中心,为了维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
      # 将7002注册进7001
      defaultZone: http://eureka7001.com:7001/eureka/

7001号EurekaServer

server:
  port: 7001

eureka:
  instance:
    # eureka服务端的实例名称
    hostname: eureka7001.com
  client:
    # false 表示不向注册中心注册自己
    register-with-eureka: false
    # false 表示自己端就是注册中心,为了维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
      # 将7001注册进7002
      defaultZone: http://eureka7002.com:7002/eureka/

服务提供者集群搭建

在这里插入图片描述

修改yml配置

server:
  port: 8002

spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    druid:
      test-while-idle: false


mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: cn.edu.guet.pojo


eureka:
  client:
    # 表示是否将自己注册进EurekaServer
    register-with-eureka: true
    # 是否从EurekaServer抓取已有的注册信息,集群必须设置true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

基本就是复制一份服务提供者的代码,修改一下端口号

在这里插入图片描述

可以看到有两个服务提供者,并且当前eureka注册中心也注册进了7002,实现高可用


负载均衡

现在我们服务消费者调用服务是通过端口号和ip进行调用的,就一直只会调用一个服务,集群就不生效了,我们可以通过微服务名称和负载均衡去对服务进行访问。

package cn.edu.guet.controller;

import cn.edu.guet.pojo.Payment;
import cn.edu.guet.response.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author pangjian
 * @ClassName OrderController
 * @Description RestTemplate提供了多种便捷访问远程Http服务的方法,
 *          是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集
 * @date 2021/9/10 15:16
 */
@RestController
@Slf4j
public class OrderController {

    // public static final String PAYMENT_URL = "http://localhost:8001";
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT_SERVICE";

    @Autowired
    private RestTemplate restTemplate;

    /**
     * @Description: 通过RestTemplate调用8001端口下的服务
     * @Param payment:
     * @return cn.edu.guet.response.CommonResult<cn.edu.guet.pojo.Payment>
     * @date 2021/9/10 15:25
    */
    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment) {
        return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
    }


    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
    }


}

开启负载均衡 @LoadBalanced

@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
    return new RestTemplate();
}

服务发现

开启注解@EnableDiscoveryClient

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class PaymentMain {

    public static void main(String[] args) {
        SpringApplication.run(PaymentMain.class, args);
    }

}

在服务里面注入discoveryClient并提供一个restful接口给外界访问

/**
 * @author pangjian
 * @ClassName PaymentController
 * @Description TODO
 * @date 2021/9/10 14:36
 */
@RestController
@Slf4j
public class PaymentController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/payment/getInfo")
    public Object discovery() {
        List<String> services = discoveryClient.getServices(); // 得到所有微服务
        for (String ele:
                  services) {
            log.info("ele:" + ele);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PROVIDER-SERVICE");
        for (ServiceInstance instance : instances) {
            log.info(instance.getInstanceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
        }
        return this.discoveryClient;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值