spring-cloud-alibaba学习实例(4)–集成FeignClient
Feign是一个声明式WebService客户端.使用Feign能让编写WebService客户端更加简单,它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解.Feign也支持可拔插式的编码器和解码器.Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
我们知道,想要开启FeignClient,首先要素就是添加@EnableFeignClients注解。其主要功能是初始化FeignClient的配置和动态执行client的请求。
环境:jdk 1.8
idea
sping-cloud-alibaba 2.2.1.RELEASE
sping-boot 2.2.5.RELEASE
sping-cloud Hoxtom.SR3
1首先建立一个三个子模块,结构如下图。其中demo-producer用来提供服务A,demo-consumer用来rpc调用服务A。
org.springframework.cloud</groupId>
spring-cloud-starter-openfeign</artifactId>
</dependency>
com.alibaba.cloud</groupId>
spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
org.springframework.boot</groupId>
spring-boot-starter-web</artifactId>
</dependency>
org.springframework.boot</groupId>
spring-boot-starter-actuator</artifactId>
</dependency>
com.alibaba.cloud</groupId>
spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
com.alibaba.cloud</groupId>
spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
com.alibaba.csp</groupId>
sentinel-datasource-nacos</artifactId>
</dependency>
3 在demo-producer中引入配置文件bootstrap.yml和application.yml
bootstrap.yml
server.port: 8080
spring.application.name: nacos-config
spring.profiles.active: develop
spring.cloud.nacos.config.server-addr: 127.0.0.1:8848
spring.cloud.nacos.config.file-extension: yaml
spring.cloud.nacos.config.refresh.enabled: true
application.yml
在这里插入代码片
4 编写服务代码ProducerService,ProducerServiceImpl,ProducerApplication
public interface ProducerService {
public String testProducer(String msg);
}
@Service
public class ProducerServiceImpl implements ProducerService {
@Override
public String testProducer(String msg) {
return "调用producer成功!! "+msg;
}
}
@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class,args);
}
}
5编写一个TestPoducerController
@RestController
public class TestPoducerController {
@Autowired
ProducerService producerService;
@GetMapping("/teseProducer")
public String testProducer(String msg){
String producer = producerService.testProducer(msg);
return producer;
}
}
7编写demo-consumer
配置文件修改 bootstrap.yml 和application.yml
bootstrap.yml
server.port: 8082
spring.application.name: demo-consumer
spring.profiles.active: develop
spring.cloud.nacos.config.server-addr: 127.0.0.1:8848
spring.cloud.nacos.config.file-extension: yaml
spring.cloud.nacos.config.refresh.enabled: true
application.yml
feign:
sentinel:
enabled: true
management:
endpoints:
web:
exposure:
include: "*"
8编写 controller 和service 和启动类
@RestController
public class TestConsumerController {
@Autowired
RpcProducerService rpcProducerService;
@GetMapping("/tesetRpc")
public String tesetRpc(String msg){
String testProducer = rpcProducerService.testProducer("我是消费者,我来调用rpc");
return testProducer;
}
}
@FeignClient(name = "demo-producer")
public interface RpcProducerService {
@GetMapping("/teseProducer")
public String testProducer(@RequestParam String msg);
}
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
说明 @FeignClient(name = “demo-producer”) 说明在这个service上面开启rpc调用,name是服务提供方的服务名称。 @GetMapping("/teseProducer")
public String testProducer(String msg);
这个必须要和服务提供方的controller接口一样
因为feigh底层是使用http协议的。
@EnableFeignClients表明项目开启了feigh功能
9启动domo-consumer 测试一下
10如何实现fallback
在demo-consumer中增加类,修改一个类
如下
@Component
public class RpcProducerServiceImp implements RpcProducerService {
@Override
public String testProducer(String msg) {
return "不好意思,调用失败";
}
}
@FeignClient(name = "demo-producer",fallback = RpcProducerServiceImp.class )
public interface RpcProducerService {
@GetMapping("/teseProducer")
public String testProducer(@RequestParam String msg);
}
@FeignClient(name = “demo-producer”,fallback = RpcProducerServiceImp.class )
其中fallback指定调用失败,会如何处理。
11测试一下,我们停掉demo-producer,制造调用失败场景
demo-producer已经被停掉了
github地址 https://github.com/userliyazhou/spirng-cloud-alibaba-project-demo/tree/master/alibaba-project-parent/demo-rpc-feigh