SpringCloud Eureka 熔断器

=============================>Eureka中心配置
注册中心服务的配置:
POM依赖
在这里插入图片描述
YML配置文件:
server:
port: 5001

spring:
application:
name: eureka-register
eureka:
client:
serviceUrl:
defaultZone: http://com.lcg.test0:5001/eureka
registerWithEureka: false #实例是否在eureka服务器上注册自己的信息以供其他服务发现,默认为true
fetchRegistry: false #此客户端是否获取eureka服务器注册表上的注册信息,默认为true
server:
enableSelfPreservation: false #关闭Eureka的自我保护

启动类示例代码:
package com.zbf.register;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**

  • 作者:LCG
  • 创建时间:2019/3/16 21:45
  • 描述:Eureka中心启动类示例代码
    */
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaRegisterServer {
    public static void main(String[] args) {
    SpringApplication.run ( EurekaRegisterServer.class,args );
    }
    }
    本人项目截图:
    在这里插入图片描述
    ================================>服务提供配置
    Eureka服务提供方配置
    POM
    在这里插入图片描述
    YML的配置
    server:
    port: 1001 #eureka-server
    spring:
    application:
    name: service-provider
    eureka:
    client:
    instance: com.lcg.test1
    serviceUrl:
    defaultZone: http://com.lcg.test0:5001/eureka #注册中心的地址

启动类示例代码:
package com.zbf.service;

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;

/**

  • 作者:LCG

  • 创建时间:2019/3/16 21:29

  • 描述:
    */
    @SpringBootApplication
    @EnableEurekaClient
    public class EurekaServerApplication {

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

}

服务提供的Controller的示例代码
package com.zbf.service.provider;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**

  • 作者:LCG

  • 创建时间:2019/3/16 21:33

  • 描述:Eureka的测试端
    */
    @RestController
    public class TestEurekaServerController {

    @RequestMapping(“test01”)
    public String test1(HttpServletRequest request, @RequestParam(“string”) String string){
    System.out.println ("=server="+string);
    return “this is server return”;
    }

    @RequestMapping(“test11”)
    public String test11(HttpServletRequest request, @RequestParam(“string”) String string){
    System.out.println ("=server=11"+string);
    return “this is server return11”;
    }

}
项目结构截图

在这里插入图片描述
====================================>服务消费方配置
Eureka服务方消费者配置(服务端feign使用,熔断器使用)
POM
在这里插入图片描述

YML配置:
server:
port: 1010
spring:
application:
name: eureka-client
eureka:
client:
serviceUrl:
defaultZone: http://com.lcg.test0:5001/eureka
feign:
hystrix:
enabled: true #开启熔断器失败回调

启动类示例:
package com.zbf.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**

  • 作者:LCG
  • 创建时间:2019/3/16 23:49
  • 描述:
    */

@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients //feign调用远程服务
@EnableCircuitBreaker //使用熔断器
public class EurekaClient1Application {

public static void main(String[] args) {

    SpringApplication.run ( EurekaClient1Application.class,args );
}

}

消费端的示例代码:
远程接口调用类:
package com.zbf.consumer.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**

  • 作者:LCG

  • 创建时间:2019/3/16 23:55

  • 描述:使用feign远程调用服务端

  • fallback 失败回调的接口实现类
    */
    @FeignClient(name = “service-provider”,fallback = CallFailBack.class)
    @Component
    public interface TestClient {

    @RequestMapping("/test01")
    public String test01(@RequestParam(“string”) String string);

    @RequestMapping("/test11")
    public String test11(@RequestParam(“string”) String string);

}

接口调用失败的回调类示例:
package com.zbf.consumer.client;

import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

/**

  • 作者:LCG

  • 创建时间:2019/3/17 11:33

  • 描述:
    */
    @Component
    public class CallFailBack implements TestClient {
    @Override
    public String test01(String string) {
    System.out.println (“test01失败回调”);
    return null;
    }

    @Override
    public String test11(String string) {
    System.out.println (“test11的失败回调”);
    return null;
    }
    }

消费者的访问接口示例:
package com.zbf.consumer.client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**

  • 作者:LCG

  • 创建时间:2019/3/16 23:54

  • 描述:
    */
    @RestController
    @RequestMapping(“test1”)
    public class TestEurekaClient {

    @Autowired
    private TestClient testClient;

    @RequestMapping("/test02")
    public String test01(){

    return testClient.test01 ( "123456中国万岁!" );
    

    }

    @RequestMapping("/test22")
    public String test21(){
    return testClient.test11 ( “123456中国万岁!” );
    }

}
本人项目结构图:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值