• spring cloud 消费者


    本消费者  加了 Hystrix, 为了后续监控用。

    1. 依赖:

      

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    </parent>

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    </properties>

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

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

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

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

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    </dependencies>

    <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Camden.SR7</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>

    </dependencies>
    </dependencyManagement>
    </project>

    2. 启动类

    @SpringBootApplication
    @EnableEurekaClient
    @EnableCircuitBreaker
    public class ConsumerHelloApplication {



    @Bean
    @LoadBalanced
    RestTemplate restTmeplate() {
    return new RestTemplate();
    }

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

    3. 业务逻辑

    @RestController
    public class ConsumerContrller {

    @Autowired
    private RestTemplate restTemplate;


    @RequestMapping("/consumer")
    @HystrixCommand(fallbackMethod="fallbackHelloConsumer")
    public String helloConsumer() {
    return restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class).getBody();
    }

    public String fallbackHelloConsumer() {
    return "error";
    }
    }

    4. 配置

    server:
    port: 8091
    spring:
    application:
    name: hello-consumer
    eureka:
    client:
    serviceUrl:
    defaultZone: http://admin:admin@ym-eureka-server1:8761/eureka/,http://admin:admin@ym-eureka-server2:8762/eureka/,http://admin:admin@ym-eureka-server3:8763/eureka/
    instance:
    preferIpAddress: true
    hystrix:
    command:
    default:
    execution:
    isolation:
    thread:
    timeoutInMilliseconds: 4000

  • 相关阅读:
    状压dp学习笔记
    dbcc log(转)
    日志分析 操作(转)
    Sql server 2005系统表详细说明
    c#读取并分析sqlServer据库日志(转)
    [POJ]1915 Knight Moves
    [JOY]1143 飘飘乎居士的约会
    [POJ]1164 The Castle
    win7注册ocx时出现对DllRegisterServer的调用失败,错误代码为0x80040200解决方案
    注册DLL、OCX的方法
  • 原文地址:https://www.cnblogs.com/maohuidong/p/9881103.html
Copyright © 2020-2023  润新知