order-server ribbon restTemplate hystrix

pom.xml

<?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">
    <parent>
        <artifactId>spring_cloud_netflix</artifactId>
        <groupId>com.qxf</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-order-server-1030</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.qxf</groupId>
            <artifactId>springcloud-user-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
    </dependencies>
</project>

application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:10010/eureka/
  instance:
    prefer-ip-address: true #使用ip地址进行注册
    instance-id: order-server:10030	#实例ID
spring:
  application:
    name: order-server
server:
  port: 10030

启动类

package com.qxf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/*
 * 订单的启动类
 * @EnableEurekaClient: 标记该应用是 Eureka客户端
 */
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class OrderServerApplication1030
{

    public static void main( String[] args )
    {
        ConfigurableApplicationContext run = SpringApplication.run(OrderServerApplication1030.class);
    }
    //配置一个RestTemplate ,Spring封装的一个机遇Restful风格的http客户端 工具
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

controller

package com.qxf.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

//订单服务
@RestController
public class OrderController {

    //需要配置成Bean
    @Autowired
    private RestTemplate restTemplate ;

    //浏览器调用该方法
    @GetMapping( "/order/{id}")
    @HystrixCommand(fallbackMethod = "getByIdFallbackMethod")
    public User getById(@PathVariable("id")Long id){
        //发送http请求调用 user的服务,获取user对象 : RestTemplate
        //目标资源路径:user的ip,user的端口,user的Controller路径
        String url = "http://user-server/user/"+id;
        //发送http请求
        return restTemplate.getForObject(url, User.class);
    }
    public User getByIdFallbackMethod(@PathVariable("id")Long id){
        return new User(id,"ribbon","降级");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值