1、在父工程 “microservicecloud” 下新建一个 Module,名称为 “consumer-feign-8301”
2、修改 pom.xml 文件
microservicecloud
com.lakey.springcloud
1.0-SNAPSHOT
4.0.0
consumer-feign-8301
consumer-feign-8301
Consumer With Feign 8301
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
3、在 java 路径下创建目录 “com.lakey.springcloud” 并添加启动类 ConsumerFeign8301Application.java
package com.lakey.springcloud;
import org.springframework.boot.Banner;
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;
@SpringBootApplication
@EnableEurekaClient // 本服务启动后会自动注册进eureka服务中
@EnableDiscoveryClient // 开启服务发现,寻找服务提供者
@EnableFeignClients // 开启 Feign 服务
public class ConsumerFeign8301Application {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(ConsumerFeign8301Application.class);
application.setBannerMode(Banner.Mode.OFF);// 不输出Banner
application.run(args);
System.out.println(" _____ _____ ___ ____ _ _ ___ _____ ___ _ \n" +
" | ___| | ____| |_ _| / ___| | \\ | | ( _ ) |___ / / _ \\ / |\n" +
" | |_ | _| | | | | _ | \\| | / _ \\ |_ \\ | | | | | |\n" +
" | _| | |___ | | | |_| | | |\\ | | (_) | ___) | | |_| | | |\n" +
" |_| |_____| |___| \\____| |_| \\_| \\___/ |____/ \\___/ |_|");
}
}
4、在 resources 路径下添加配置文件 application.yml
# 服务器配置
server:
port: 8301
# Spring 配置
spring:
application:
name: consumer-feign
# Eureka 配置
eureka:
client: # 客户端注册进 Eureka 服务列表
serviceUrl:
defaultZone: http://localhost:8101/eureka/
instance:
instance-id: consumer-feign-8301 # 自定义服务名称信息
prefer-ip-address: true # 访问路径可以显示 IP 地址
# Eureka 微服务详细信息配置
info:
app.name: consumer-feign-8301
company.name: www.lakey.com
build.artifactId: microservicecloud
build.version: 1.0-SNAPSHOT
# Feign 配置
feign:
hystrix:
enabled: true # 开启断路器
5、在目录 “com.lakey.springcloud” 下创建一个服务层文件夹 “service” 并添加服务调用接口 HelloService.java 请求生产者服务接口,同时指定 Hystric 熔断异常处理
package com.lakey.springcloud.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* 服务调用
*
* @author liming
* @date 2019-05-07
*/
@FeignClient(value = "provider", fallback = HelloServiceHystric.class) // 指定调用服务 provider 和熔断返回处理类 HelloServiceHystric
public interface HelloService {
/**
* 调用 Feign 注解指定服务的 /hello 接口
*
* @param name
* @return
*/
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String sayHello(@RequestParam(value = "name") String name);
}
6、在目录 “com.lakey.springcloud.service” 下新增 HelloService 的 Hystric 熔断异常处理类 HelloServiceHystric.java
package com.lakey.springcloud.service;
import org.springframework.stereotype.Component;
/**
* HelloService 熔断异常处理类
*
* @author liming
* @date 2019-05-07
*/
@Component
public class HelloServiceHystric implements HelloService {
/**
* HelloService sayHello 方法熔断异常处理
*
* @param name
* @return
*/
public String sayHello(String name) {
return "hi " + name + " ,hystric error";
}
}
7、在目录 “com.lakey.springcloud” 下创建一个控制层文件夹 “controller” 并添加控制类 HelloController.java 对用户暴露接口
package com.lakey.springcloud.controller;
import com.lakey.springcloud.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* 测试控制器
*
* @author liming
* @date 2019-05-07
*/
@RestController
public class HelloController {
@Autowired(required = false)
private HelloService helloService;
/**
* 测试入口
*
* @param name
* @return
*/
@GetMapping(value = "/hello")
public String sayHello(@RequestParam String name) {
return helloService.sayHello(name);
}
}
8、目录结构截图:
9、程序正常运行截图:
10、程序执行异常截图(关闭 provider-8202,消费者轮询至改服务时,将会触发 Hystric 熔断处理):
11、参考文章:
https://github.com/forezp/SpringCloudLearning
12、码云源码:
https://gitee.com/nangongyanya/microservicecloud