springcloud之eureka

什么是Eureka?

  • Eureka是基于REST(Representational State Transfer)服务,主要以AWS云服务为支撑,提供服务发现并实现负载均衡和故障转移。我们称此服务为Eureka服务。Eureka提供了Java客户端组件,Eureka Client,方便与服务端的交互。客户端内置了基于round-robin实现的简单负载均衡。在Netflix,为Eureka提供更为复杂的负载均衡方案进行封装,以实现高可用,它包括基于流量、资源利用率以及请求返回状态的加权负载均衡。
    在这里插入图片描述

Eureka的依赖

  • Eureka自己的依赖
 		 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
  • 服务提供方的依赖
	<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
  • 服务消费方不需要任何操作
  • 完善监控信息的依赖
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

Eureka的配置

  • Eureka自身的配置
server:
  port: 7001   #Eureka的服务端口


eureka:
  instance:# 实例
    hostname: localhost01 #eureka的名称
  client:
    fetch-registry: false #是否将自己作为erueka的注册中心
    register-with-eureka: false #是否将自己注册到eureka中
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/,http://localhost:7002,http://localhost:7003
       #集群搭建和客户端访问地址
       #http://${eureka.instance.hostname}:${server.port}/eureka/,http://localhost:7002,http://localhost:7003
  server:
    # 关闭自我保护机制,保证不可用的服务被及时剔除
    enable-self-preservation: false
    # 如果2秒内没有收到某个微服务的心跳,那就剔除该微服务,单位为毫秒
    eviction-interval-timer-in-ms: 2000
  • Eureka客户端的配置(生产者)
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/
  instance:
    instance-id: springcloud-provide-dept8001
    # Eureka客户端向服务心跳的时间间隔是1s,默认是30秒
    lease-renewal-interval-in-seconds: 1
    # Eureka服务端在收到一次心跳后等待时间上限是2s,默认是90s,超时将剔除该微服务,其实Eureka服务端已经设置了,可能就是也告诉服务提供者一下
    lease-expiration-duration-in-seconds: 2
    
# 在添加了spring-boot-starter-actuator依赖之后对基本信息进行添加
info:
  app.name: xxxxxxxx-springcloud_eureka
  company.name: xxxxxx
  actor: 张三
  • springcloud和springboot的依赖:
			<dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--            spring-boot-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

微服务基本信息查看

  • 在生产者的Controller中添加如下方法:
    @GetMapping("dept/discover")
    public Object discover() {
        List<String> services = discoveryClient.getServices();
        System.out.println("微服务信息" + services);

        List<ServiceInstance> instances = discoveryClient.getInstances("springcloud-provider-dept");
        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost() + "\t" +
                            instance.getUri() + "\t" +
                            instance.getPort() + "\t" +
                            instance.getServiceId()
            );

        }
        return discoveryClient;
    }

效果和在添加了spring-boot-starter-actuator依赖之后配置相关信息的作用是一致的。

  • 必须要在springbootapplication上添加@EnableDiscoveryClient注解
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值