这一章节讲fegin的使用.

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

这里Artifact填写feginclient, 再次next, 选择内容如下的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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xum</groupId>
<artifactId>feign-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>feign-client</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories> </project>

fegin-client项目结构如下:

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

package com.xum.feignclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan; @SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@ComponentScan(basePackages = { "com.xum.feignclient.controller", "com.xum.feignclient.server" })
public class FeignClientApplication { public static void main(String[] args) {
SpringApplication.run(FeignClientApplication.class, args);
} }

2. application.yml内容如下:

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8772
spring:
application:
name: fegin-client
feign:
hystrix:
enabled: true
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: '*'

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

package com.xum.feignclient.controller;

import com.xum.feignclient.server.HelloService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping(value = "/feign")
public class FeignConsumerController { private static final Logger LOG = LoggerFactory.getLogger(FeignConsumerController.class); @Autowired
HelloService helloService; @RequestMapping(value = "/feignconsumer")
public String helloConsumer(){
LOG.info("FeginConsumerController=>helloConsumer");
return helloService.hello();
} @RequestMapping(value = "/test")
public String test() {
return "test";
}
}

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

package com.xum.feignclient.server;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

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

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

package com.xum.feignclient.server;

import org.springframework.stereotype.Service;

@Service
public class HystrixService implements HelloService {
// 这里就是当调用api失败后, 对应的处理方法.
@Override
public String hello() {
return " HelloService=>HystrixService=>eureka-client is unable ...";
} }

现在启动如下的项目:

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里的内容里, 自我保护的机制.

最新文章

  1. discuz核心函数库function_core的函数注释
  2. Drupal安装及使用问题解决列表
  3. jQuery表格操作
  4. 一级域名301重定向到www二级域名
  5. Labview中引用,属性节点,局部变量之间的区别
  6. spring基础系列--JavaConfig配置
  7. u盘安装原版win10系统1703更新
  8. Codeforces Round #328 (Div. 2)_B. The Monster and the Squirrel
  9. acm入门搜索-水池数目
  10. docker学习------centos7.5下的swarm集群可视化构建
  11. vue-router同路由$router.push不跳转一个简单解决方案
  12. Android 开发 存储目录的详解
  13. Maven中POM.XML详解
  14. python DBUtils 线程池 连接 Postgresql(多线程公用线程池,DB-API : psycopg2)
  15. cookie、sesstion、strorage
  16. jsp相关笔记(二)
  17. 帝国cms栏目别名怎样调用?栏目名称太短了
  18. bfs,dfs区别
  19. [转帖]AMD、英特尔为何争相走向胶水多核处理器?真相在此
  20. 20155203 2016-2017-2 《Java程序设计》第10周学习总结

热门文章

  1. Qt 学习之路 2(7):MainWindow 简介
  2. [Groovy]static typing
  3. 8.11zju集训日记
  4. Codeforces - 625B 贪心
  5. c#之GDI简单实现代码及其实例
  6. Python Pandas -- DataFrame
  7. 转 rac中并行 PARALLEL 的设置
  8. MySQL三大范式和反范式
  9. CentOS 同时忘记用户名和密码
  10. Mybatis学习笔记3 - 增删改查示例