Code Monkey home page Code Monkey logo

spring-cloud-demo-simple's Introduction

spring-cloud-demo-simple

导入eclipse时选择普通导入,导入成功右键项目转为gradle

配置文件以下为必填项: 注册中心

spring:
  application:
    name: discovery-service
eureka:
  client:
    register-with-eureka: false # 不注册eureka服务
    fetch-registry: false # 不同步注册表

非注册中心

spring:
  application:
    name: discovery-service
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

@SpringCloudApplication 表明程序启动入口,包含如下

  • @SpringBootApplication 表明该类为程序启动类
  • @EnableDiscoveryClient 表明开启服务发现,Eureka
  • @EnableCircuitBreaker 表明开启Hystrix

远程调用接口

1、添加配置 @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } 2、使用RestTemplate调用,下面仅是举例,还有其他。 @Autowired private RestTemplate restTemplate; restTemplate.getForObject("http://DEMO-SERVICE/getMe", String.class);

3、熔断机制使用: 在service加上@HystrixCommand

    @Override
    @HystrixCommand(fallbackMethod = "error")
    public String getMe(String param) {
        return restTemplate.getForObject("http://DEMO-SERVICE/getMe", String.class);
    }
    // 此处Throwable throwable可选
    public String error(String param, Throwable throwable) {
        
        return "error " + param;
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.