SpringCloud学习笔记

服务注册中心Eureka

Eureka配置

http://www.ityouknow.com/springcloud/2017/05/10/springcloud-eureka.html
具体配置大佬的文章都会有,这里只做踩坑记录。

PS:版本不同,注解也会不同

@EnableEurekaServer
之前版本@EnableEurekaServer中会包含@EnableDiscoveryClient注解
在这里插入图片描述
@EnableEurekaServer注解详解 https://www.jianshu.com/p/3a3a1a5891ec
客户端配置:
@EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient 可以是其他注册中心。

PS:软件包名更新

spring-cloud-starter-eureka
spring-cloud-starter-eureka-server

分别改为

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

PS 注册到eureka实例名称问题

application.yml配置

eureka:
  client:
  service-url:
    defaultZone: http://localhost:8761/eureka/
    register-with-eureka: false
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${spring.application.instance_id:${server.port}}

配置之后可能无法正确显示 ,需要引入依赖

		 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-commons</artifactId>
        </dependency>

在这里插入图片描述
原因参考

eureka.instance.instance-id必须写在application.properties或application.yml中。不能写在bootstrap.properties和bootstrap.yml中。可以去了解下springboot加载默认配置文件的顺序,bootstrap是在application之前的。写在bootstrap里的话,自定义的instance-id并不会在eureka界面生效,原理没研究过。

Spring Cloud Config配置中心

坑一:配置发现

把配置中心作为服务注册到Eureka中心实现高可用后,客户端获取配置的配置可以从URL改为配置中心的application.name

在这里插入图片描述
service-id名称是config项目的application.name(即config项目eureka界面注册的名称),config.name是你git地址上配置的名称如。
在这里插入图片描述
选择的配置文件的名称。这里容易混淆。

坑二:Spring Cloud Bus消息总线自动刷新

Hystrix熔断器

熔断器参数

这篇文章写得很详细

坑:feign的bug

package com.example.eurekaclient.web;

import com.example.eurekaclient.common.HelloRemote;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author : yy
 * @date :Created in 19-8-21 下午2:16
 * @description:
 * @modified By:
 * @version: $
 */
@RestController
public class HystrixTestController {

    @Autowired
    private HelloRemote HelloRemote;
    private RestTemplate restTemplate = new RestTemplate();
    @GetMapping("/hello")
    public String hello(){
        return HelloRemote.hello("66666");
    }

    @GetMapping("/sleep3s")
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "4000")
    })
    /**
     * 使用feign熔断器会失效,即使配置了feign.hystrix.enabled= true 也不会生效,,,使用restTemplate就不会有问题
     */
    public String sleep3s(){
        return HelloRemote.sleep3s();
    }

    @GetMapping("/sleep3sRest")
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "4000")
    })
    public String sleep3s2(){

        String result = restTemplate.getForObject("http://localhost:8081/sleep3s",String.class);
        return  result;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值