五、项目集成Eureka、Hystrix

创建Eureka注册中心(服务端)

新建一个maven项目

在基础的spring boot基础上,注入相关依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>com.tools</groupId>
    <artifactId>config</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <spring-cloud.version>Finchley.RC2</spring-cloud.version>
    </properties>
    <dependencies>
        <!-- 支持web模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 测试模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--服务中心-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
    <!--管理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>
    <!-- 使用spring-boot-maven-plugin 来对Springboot 应用进行打包,需要在项目的 pom.xml 文件中引入插件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

请注意SpringCloud中Eureka 版本 和SpringBoot版本关联:
在这里插入图片描述

配置文件application.yml

server:
  port: 8761  #服务注册中心端口号

spring:
  application:
    name: cloud-register
eureka:
  #此处设置会改变eureka控制台的显示
  datacenter: cloud
  #此处设置会改变eureka控制台的显示
  environment: prod
  dashboard:
    #控制台路径
    path: /dashboard  #访问eureka时,加上这个后缀
    #是否启用控制台
    enabled: true
  instance:
    prefer-ip-address: true #以IP地址注册到服务中心,相互注册使用IP地址
#    instance-id:  ${spring.cloud.client.ipAddress}:${server.port} #实例规则是 ip+端口号
    hostname: localhost #服务注册中心IP地址
  client:
    register-with-eureka: false #是否向服务注册中心注册自己
    fetch-registry: false #不从Eureka注册中心获取服务的注册信息
    service-url: #服务注册中心的配置内容,指定服务注册中心的位置
      defaultZone:  http://${eureka.instance.hostname}:${server.port}/eureka/ #后面的/eureka是固定后缀,访问eureka时,需要删除该后缀
  server:
#    eviction-interval-timer-in-ms: 3000  #指定EvictionTask定时任务的调度频率,用于剔除过期的实例
#    enable-self-preservation: false  #是否开启自我保护模式(开启状态下服务停掉eureka不会立即清除掉宕掉的服务,所以false)
    renewal-percent-threshold: 0.49 #定义了renews 和renews threshold的比值,默认值为0.85。当server在15分钟内,比值低于percent,即少了15%的微服务心跳,server会进入自我保护状态
#    response-cache-update-interval-ms: 3000  #设置CacheUpdateTask的调度时间间隔,用于从readWriteCacheMap更新数据到readOnlyCacheMap,仅仅在eureka.server.use-read-only-response-cache为true的时候才生效
#    expected-number-of-renews-per-min:  1 #指定每分钟需要收到的续约次数值,实际该值在其中被写死为count*2,另外也会被更新(已过时)

启动类添加注解

添加@EnableEurekaServer

package com.tools;

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

/**
 * 启动一个服务注册中心
 */
@EnableEurekaServer
@SpringBootApplication
public class ConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }
}

效果

访问地址http://127.0.0.1:8761/dashboard,打开可视化页面
在这里插入图片描述

项目集成Eureka(客户端)

注入客户端依赖

在客户端增加依赖注入(在服务端的pom基础上新增下面)

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

配置文件application.yml

# 更改Eureka更新频率将打破服务器的自我保护功能,生产环境下不建议自定义这些配置。
eureka:
  instance:
    prefer-ip-address: false #使用服务的id地址注册
    lease-expiration-duration-in-seconds: 90  # 续约到期时间(默认90秒)
    lease-renewal-interval-in-seconds: 30 # 续约更新时间间隔(默认30秒)
  client:
    healthcheck:
      enabled:  true    # 开启健康检查(需要spring-boot-starter-actuator依赖)
    register-with-eureka: true
    registry-fetch-interval-seconds: 30
    serviceUrl: #注册中心的注册地址
      defaultZone: http://127.0.0.1:8761/eureka/

启动类添加注解

添加@EnableEurekaClient

package com.springboot.demo;

import com.springboot.demo.properties.RootProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringCloudApplication
@EnableConfigurationProperties(RootProperties.class)
public class DemoApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

启动客户端服务,发现报错,hystrix类型错误

注入断路器依赖

需要在服务端与客户端都注入断路器依赖,Hystrix介绍详见此链接

        <!--断路器依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

验证

按顺序

  1. 启动服务端(Eureka)
  2. 启动客户端(Client)

效果

http://127.0.0.1:8761/dashboard
在这里插入图片描述

拓展

Eureka与zookeeper的区别

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值