天天看点

SpringCloud 备忘 4 - Feign 风格消费者 Consumer Feign 服务搭建

1、在父工程 “microservicecloud” 下新建一个 Module,名称为 “consumer-feign-8301”

2、修改 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>microservicecloud</artifactId>
        <groupId>com.lakey.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consumer-feign-8301</artifactId>
    <name>consumer-feign-8301</name>
    <description>Consumer With Feign 8301</description>

    <dependencies>
        <!-- Eureka 客户端依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- SpringBoot Web 容器依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Feign 依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- Hystrix 依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
    </dependencies>

</project>
           

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、目录结构截图:

SpringCloud 备忘 4 - Feign 风格消费者 Consumer Feign 服务搭建

9、程序正常运行截图:

SpringCloud 备忘 4 - Feign 风格消费者 Consumer Feign 服务搭建
SpringCloud 备忘 4 - Feign 风格消费者 Consumer Feign 服务搭建

10、程序执行异常截图(关闭 provider-8202,消费者轮询至改服务时,将会触发 Hystric 熔断处理):

SpringCloud 备忘 4 - Feign 风格消费者 Consumer Feign 服务搭建

11、参考文章:

https://github.com/forezp/SpringCloudLearning

12、码云源码:

https://gitee.com/nangongyanya/microservicecloud 

继续阅读