码迷,mamicode.com
首页 > 其他好文 > 详细

005声明式服务调用Feign

时间:2017-08-02 13:56:17      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:client   端口   bsp   中心   pad   app   默认   rcu   style   

1、POM配置

  和普通Spring Boot工程相比,添加了Eureka Client、Feign、Hystrix、Spring Boot Starter Actuator依赖和Spring Cloud依赖管理

<dependencies>
  <!--Eureka Client依赖-->   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
  <!--声明式服务消费-->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
  </dependency>
  <!-- 整合断路器hystrix,其实feign中自带了hystrix,引入该依赖主要是为了使用其中的hystrix-metrics-event-stream,用于dashboard -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
  </dependency>
  <!--Spring Boot工程健康检测-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency> </dependencies>
<!--Spring Cloud依赖版本管理--> <dependencyManagement>   <dependencies>     <dependency>       <groupId>org.springframework.cloud</groupId>       <artifactId>spring-cloud-dependencies</artifactId>       <version>Dalston.SR1</version>       <type>pom</type>       <scope>import</scope>     </dependency>   </dependencies> </dependencyManagement>

02、使能

@SpringBootApplication
@EnableFeignClients  //声明式服务消费
@EnableCircuitBreaker  //断路器
@EnableDiscoveryClient  //Eureka客户端 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }

03、src/main/resources工程配置文件 application.yml

server:
  port: 8020  #默认启动端口
spring:
  application:
    name: microservice-consumer  #服务名
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:1001/eureka/  #服务注册中心地址
  instance:
    preferIpAddress: true  #使用IP注册,而非hostname

04、声明式服务消费

@FeignClient(name = "microservice-consumer-hello", fallback = HystrixClientFallback.class)
public interface HelloFeignClient {
    @RequestMapping("/")
    public String hello();

    @Component
    static class HystrixClientFallback implements HelloFeignClient {
        @Override
        public User hello() {
            return "异常发生,断路器打开";
        }
    }
}

05、Controller

@RestController
public class FeignController {
    @Autowired
    private HelloFeignClient helloFeignClient;

    @GetMapping("feign")
    public String hello() {return this.helloFeignClient.hello();
    }
}

 

005声明式服务调用Feign

标签:client   端口   bsp   中心   pad   app   默认   rcu   style   

原文地址:http://www.cnblogs.com/geniushuangxiao/p/7219515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!