这一章节讲fegin的使用.

在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Artifact等信息,

这里Artifact填写feginclient, 再次next, 选择内容如下的pom.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.3.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.xum</groupId>
  12. <artifactId>feign-client</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>feign-client</name>
  15. <description>Demo project for Spring Boot</description>
  16.  
  17. <properties>
  18. <java.version>1.8</java.version>
  19. <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
  20. </properties>
  21.  
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.cloud</groupId>
  29. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.cloud</groupId>
  33. <artifactId>spring-cloud-starter-openfeign</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.cloud</groupId>
  37. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.cloud</groupId>
  41. <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-starter-actuator</artifactId>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.springframework.boot</groupId>
  49. <artifactId>spring-boot-starter-test</artifactId>
  50. <scope>test</scope>
  51. </dependency>
  52. </dependencies>
  53.  
  54. <dependencyManagement>
  55. <dependencies>
  56. <dependency>
  57. <groupId>org.springframework.cloud</groupId>
  58. <artifactId>spring-cloud-dependencies</artifactId>
  59. <version>${spring-cloud.version}</version>
  60. <type>pom</type>
  61. <scope>import</scope>
  62. </dependency>
  63. </dependencies>
  64. </dependencyManagement>
  65.  
  66. <build>
  67. <plugins>
  68. <plugin>
  69. <groupId>org.springframework.boot</groupId>
  70. <artifactId>spring-boot-maven-plugin</artifactId>
  71. </plugin>
  72. </plugins>
  73. </build>
  74.  
  75. <repositories>
  76. <repository>
  77. <id>spring-milestones</id>
  78. <name>Spring Milestones</name>
  79. <url>https://repo.spring.io/milestone</url>
  80. </repository>
  81. </repositories>
  82.  
  83. </project>

fegin-client项目结构如下:

1. 首先FeginClientApplication.java里的内容如下:

  1. package com.xum.feignclient;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  7. import org.springframework.cloud.openfeign.EnableFeignClients;
  8. import org.springframework.context.annotation.ComponentScan;
  9.  
  10. @SpringBootApplication
  11. @EnableEurekaClient
  12. @EnableFeignClients
  13. @ComponentScan(basePackages = { "com.xum.feignclient.controller", "com.xum.feignclient.server" })
  14. public class FeignClientApplication {
  15.  
  16. public static void main(String[] args) {
  17. SpringApplication.run(FeignClientApplication.class, args);
  18. }
  19.  
  20. }

2. application.yml内容如下:

  1. eureka:
  2. client:
  3. serviceUrl:
  4. defaultZone: http://localhost:8761/eureka/
  5. server:
  6. port: 8772
  7. spring:
  8. application:
  9. name: fegin-client
  10. feign:
  11. hystrix:
  12. enabled: true
  13. management:
  14. endpoint:
  15. health:
  16. show-details: always
  17. endpoints:
  18. web:
  19. exposure:
  20. include: '*'

3. 然后FeignConsumerController.java内容如下:

  1. package com.xum.feignclient.controller;
  2.  
  3. import com.xum.feignclient.server.HelloService;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9.  
  10. @RestController
  11. @RequestMapping(value = "/feign")
  12. public class FeignConsumerController {
  13.  
  14. private static final Logger LOG = LoggerFactory.getLogger(FeignConsumerController.class);
  15.  
  16. @Autowired
  17. HelloService helloService;
  18.  
  19. @RequestMapping(value = "/feignconsumer")
  20. public String helloConsumer(){
  21. LOG.info("FeginConsumerController=>helloConsumer");
  22. return helloService.hello();
  23. }
  24.  
  25. @RequestMapping(value = "/test")
  26. public String test() {
  27. return "test";
  28. }
  29. }

4. 其次就是HelloService.java接口内容如下:

  1. package com.xum.feignclient.server;
  2.  
  3. import org.springframework.cloud.openfeign.FeignClient;
  4. import org.springframework.web.bind.annotation.RequestMapping;

  1. // 这里的FeginClient注解, value是说明调用的那个项目,就是spring.application.name对应的名字;
  1. // fallback是调用value项目api失败后, 处理的方法类, 这也是自定义的.
  2. @FeignClient(value = "eureka-client", fallback = HystrixService.class)
  3. public interface HelloService {
  4.  
  5. @RequestMapping(value = "/testone/test")
  6. String hello();
  7.  
  8. }

5. 然后是HystrixService.java内容如下:

  1. package com.xum.feignclient.server;
  2.  
  3. import org.springframework.stereotype.Service;
  4.  
  5. @Service
  6. public class HystrixService implements HelloService {
  7. // 这里就是当调用api失败后, 对应的处理方法.
  8. @Override
  9. public String hello() {
  10. return " HelloService=>HystrixService=>eureka-client is unable ...";
  11. }
  12.  
  13. }

现在启动如下的项目:

1. eureka-server

2. config-server

3. feign-client

4. eureka-client

在浏览器或则post man中输入api:http://localhost:8772/feign/feignconsumer, 显示内容如下:

说明成功调用eureka-client项目里的testone/test这条api.

现在把eureka-client项目停止, 然后再输入这条api: http://localhost:8772/feign/feignconsumer, 内容如下:

现在这里输出的是HystrixService.java里的内容里, 自我保护的机制.

SpringCloud的学习记录(6)的更多相关文章

  1. SpringCloud的学习记录(1)

    最近一段时间重新学习一边SpringCloud(有半年不用了),这里简单记录一下. 我用的是IntelliJ IDEA开发工具, SpringBoot的版本是2.1.3.RELEASE. 1. 构建M ...

  2. SPRINGCLOUD 开发学习记录

    一个简单的微服务系统:服务注册和发现,服务消费,负载均衡,断路器,智能路由,配置管理 服务注册中心: eureka是一个高可用组件,没有后端缓存,每一个实例注册后向注册中心发送心跳,默认情况下,eru ...

  3. SpringCloud的学习记录(8)

    这一章节讲zipkin-server. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Artifact等 ...

  4. SpringCloud的学习记录(7)

    这一章节讲zuul的使用. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Artifact等信息, 这里 ...

  5. SpringCloud的学习记录(5)

    这一章节讲如何使用ribbon和hystrix. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Arti ...

  6. SpringCloud的学习记录(3)

    这一章节讲搭建config-server的项目. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Arti ...

  7. SpringCloud的学习记录(2)

    这一章节主要讲如何搭建eureka-client项目. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和A ...

  8. SpringCloud的学习记录(4)

    本篇基于上一篇写的, 在git上更改配置后, eureka-client如何更新. 我们只需要在配置文件中配置 spring-cloud-starter-bus-amqp; 这就是说我们需要装rabb ...

  9. SpringCloud基础教程学习记录

    这个学习记录是学习自翟永超前辈的SpringCloud的基础教程. 自己写这个教程的目的主要是在于,想要更凝练总结一些其中的一些实用点,顺便做个汇总,这样自己在复习查看的时候更加方便,也能顺着自己的思 ...

随机推荐

  1. luogu3312 [SDOI2014]数表 (莫比乌斯反演+树状数组)

    link \(\sum_{i=1}^n\sum_{j=1}^m[s(\gcd(i,j))\le a]s(\gcd(i,j))\) \(=\sum_{p=1}^ns(p)[s(p)\le a]\sum_ ...

  2. shiro 的简单应用

    shiro   的简单应用 shiro官网:https://shiro.apache.org/ shiro 简介: Apache Shiro(日语"堡垒(Castle)"的意思)是 ...

  3. Mybatis学习笔记(七) —— 关联查询

    一.一对多查询 需求:查询所有订单信息,关联查询下单用户信息. 注意:因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询.如果从用户信息出发查询用户下的订单信息则 ...

  4. maven 更新子工程中依赖的父工程 版本

    eclipse 中: versions:update-child-modules cmd: mvn versions:update-child-modules

  5. django 异常问题总结

    1.问题1 在继承的类中: 403错误: views 添加:from django.core.context_processors import csrf def TestUEditor(reques ...

  6. P2903 [USACO08MAR]麻烦的干草打包机The Loathesome Hay Baler

    传送门 题目问的是从出发点一直跑到终点的一条链上所有齿轮的速度和 其他的不用考虑 直接搜就好了 注意求的是绝对值之和,不是和的绝对值,所以不用考虑方向问题 注意 N<=1050 数组不要只开10 ...

  7. msyql操作100题

    1.1.1 开启MySQL服务 /etc/init.d/mysqld start 使用/etc/init.d/mysqld start命令启动数据库的本质就相当于执行mysqld_safe --use ...

  8. zabbix 安装使用

    zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统管理员快速定位/解决 ...

  9. mybatis用法

    转载:https://www.cnblogs.com/xdp-gacl/p/4261895.html 一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架 ...

  10. Checkstyle的配置详解

    Checkstyle是一款检查java程序代码样式的工具,可以有效的帮助我们检视代码以便更好的遵循代码编写标准,特别适用于小组开发时彼此间的样式规范和统一.Checkstyle提供了高可配置性,以便适 ...