之前我们在搭建Ribbon负载均衡的时候,服务调用采用的RestTemplate。
相关链接:https://blog.csdn.net/u013071014/article/details/111361176
@RequestMapping(path = "/getConsumer", method = RequestMethod.GET)
public String getConsumer() {
String result = restTemp.getForObject("http://springcloud-provider1/provider/getProvider", String.class);
return result;
}
访问URL是采用字符串拼接的方式,但是如果input 参数有很多,拼接起来就相当麻烦。
使用Feign可以简化服务调用。
1.Eureka server
https://blog.csdn.net/u013071014/article/details/111031306
2.服务提供者
https://blog.csdn.net/u013071014/article/details/111361176
1.在parent项目右键,New—> Maven Module,名称命名为springcloud-consumer2
2.在springcloud-consumer2 的pom.xml文件中新增依赖
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
dependencies>
3.配置application.yml
server:
port: 8092
4.在主启动类加上 @EnableFeignClients注解,开启Spring Cloud Feign的支持功能 。
package com.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class SpringCloudConsumerApplication {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(SpringCloudConsumerApplication.class, args);
}
}
5.创建接口,添加@FeignClient注解,name属性为服务提供者的服务名
package com.springcloud.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name = "springcloud-provider1")
public interface ProviderFeignClient {
@RequestMapping(value = "/provider/getProvider", method = RequestMethod.GET)
public String getProvider();
}
6.创建controller
package com.springcloud.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.springcloud.service.ProviderFeignClient;
@RestController
@RequestMapping(path = "/consumer")
public class ConsumerController {
@Autowired
private ProviderFeignClient providerFeignClient;
@RequestMapping(value = "/getConsumer", method = RequestMethod.GET)
public String getConsumer() {
return this.providerFeignClient.getProvider();
}
}
7.启动eureka server,三个provider和consumer2,访问api测试
多次call API访问,我们会发现返回结果以轮询的方式出现。事实如此, Feign 中本身已经集成了Ribbon依赖和自动配置,因此我们不需要额外引入依赖,也不需要再注册RestTemplate 对象。
1.在springcloud-consumer2的pom.xml文件中新增依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
2.修改springcloud-consumer2的application.yml配置文件
server:
port: 8092
# 开启Feign的Hystrix支持,默认是false
feign:
hystrix:
enabled: true
3.在主启动类添加@EnableHystrix注解
package com.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
public class SpringCloudConsumerApplication {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(SpringCloudConsumerApplication.class, args);
}
}
4.新建一个类,实现FeignClient接口或FallbackFactory接口。使用FallbackFactory的好处在于可以获取到触发断路器的异常信息。下面我们以FallbackFactory为例。
package com.springcloud.service;
import org.springframework.stereotype.Component;
import feign.hystrix.FallbackFactory;
@Component
public class ProviderFeignClientFallbackFactory implements FallbackFactory<ProviderFeignClient>{
@Override
public ProviderFeignClient create(Throwable cause) {
// TODO Auto-generated method stub
return new ProviderFeignClient() {
@Override
public String getProvider() {
// TODO Auto-generated method stub
return "发生熔断!!! 异常信息: " + cause.getMessage();
}
};
}
}
5.修改FeignClient接口,在属性中加上FallbackFactory处理类
package com.springcloud.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name = "springcloud-provider1", fallbackFactory = ProviderFeignClientFallbackFactory.class)
public interface ProviderFeignClient {
@RequestMapping(value = "/provider/getProvider", method = RequestMethod.GET)
public String getProvider();
}
6.启动eureka server,provider和consumer2,访问api返回正常
7.为了方便测试异常捕获,我们将provider 抛出一个异常,再次call API测试
package com.springcloud.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(path = "/provider")
public class ProviderController {
@RequestMapping(path = "/getProvider", method = RequestMethod.GET)
public String getProvider() throws Exception {
throw new Exception("For Test");
//return "Provider1";
}
}
https://github.com/DamonLiu666/springcloud_test