使用feign作为客户端来消费服务,提供负载均衡和断路器支持

pom文件如下:

<?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>

	<groupId>com.example</groupId>
	<artifactId>service-feign</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>service-feign</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Dalston.SR3</spring-cloud.version>
	</properties>

	<dependencies>
		<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>
		<!--让feign支持断路器添加hystrix依赖-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<!--监控hystrix需要添加actuator依赖-->
		<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>


</project>

application.yml配置:

server:
  port: 8300
spring:
  application:
    name: service-feign
management:
  security:
    enabled: false
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/
feign:
  hystrix:
    enabled: true #开启feign的hystrix支持,这个属性idea智能补全提示不出来...

注意这里如果要让feign客户端支持fallback的话,必须开启feign.hystrix.enabled:true,不然不会执行熔断保护逻辑


启动类:

package com.example.servicefeign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker //要能监控hystrix必须声明该注解,并添加spring-boot-starter-actuator依赖
public class ServiceFeignApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceFeignApplication.class, args);
	}
}


service 类:

package com.example.servicefeign.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Service //强迫症范了,加个注解让controller中的自动注入不会出现红线提示无法自动注入,不加也是可以运行的
@FeignClient(name = "EUREKA-CLIENT-PRODUCER", fallback = MyServiceFallback.class)
public interface MyService {

    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    String test(@RequestParam("username") String username, @RequestParam("password") String password);

    @RequestMapping(value = "/hi2/{username}", method = RequestMethod.GET)
    String test2(@PathVariable("username") String username);
}

@FeignClient注解表名该接口是一个声明式的feign客户端,name是前面注册的服务提供者的服务名(serviceId),在eureka注册中心可以看到,GET请求如果有多个参数的话,必须用@RequestParam分别声明,不能用一个对象来封装所有参数后将该对象作为请求参数,这样虽然你声明的是GET请求,但是最终feign会以post请求发送参数,因为这个参数会放到requestBody中,只要request body中有内容,feign就会以post方式请求了。所以如果你的服务提供发限定了只能以GET方式请求的话,就会报错了。

fallback开启熔断保护,熔断实现由MyServiceFallback类提供,该类实现了MyService接口,同名方法实现即为对应的方法调用的fallback方法:

package com.example.servicefeign.service;

import org.springframework.stereotype.Component;

//fallback实现必须声明为spring 的bean,不然会提示fallback实现没有被实例化的错误
@Component
public class MyServiceFallback implements MyService {

    @Override
    public String test(String username, String password) {
        return "feign fallback:" + username + "," + password;
    }

    @Override
    public String test2(String username) {
        return "feign fallback:" + username;
    }
}

MyServiceFallback类必须是一个spring bean,这里用@Component注解了该类,组件扫描会自动将该类实例化为一个spring bean,不然触发fallback时会报错提示fallback类没有实例化.


最后controller类:

package com.example.servicefeign.controller;

import com.example.servicefeign.model.UserInfo;
import com.example.servicefeign.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @Autowired
    private MyService myService;

    @RequestMapping("/test")
    public String test(UserInfo userInfo) {
        return myService.test(userInfo.getUsername(), userInfo.getPassword());
    }

    @RequestMapping("/test2/{username}")
    public String test2(@PathVariable("username") String username) {
        return myService.test2(username);
    }
}











版权声明:本文为xiao_jun_0820原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。