Spring Cloud Feign 声明式服务调用
< 返回列表时间: 2020-07-05来源:OSCHINA
引入依赖 <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!--注册中心eureka/zull/nacos/zookeeper都是可以的任选一种--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--feign依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> </dependencies> 开启Feign @EnableFeignClients开启远程调用 @EnableDiscoveryClient 服务注册发现 所有的注册中心都可以使用该注解 /推荐常用 @EnableDiscoveryClient @EnableFeignClients @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } 控制器: 服务提供者Service-Provider @RestController @RequestMapping("user") public class UserController { private Logger log = LoggerFactory.getLogger(this.getClass()); @GetMapping("/{id:\\d+}") public User get(@PathVariable Long id) { log.info("获取用户id为 " + id + "的信息"); return new User(id, "yh", "123456"); } @GetMapping public List<User> get() { List<User> list = new ArrayList<>(); list.add(new User(1L, "yh", "123456")); list.add(new User(2L, "scott", "123456")); log.info("获取用户信息 " + list); return list; } @PostMapping public void add(@RequestBody User user) { log.info("新增用户成功 " + user); } @PutMapping public void update(@RequestBody User user) { log.info("更新用户成功 " + user); } @DeleteMapping("/{id:\\d+}") public void delete(@PathVariable Long id) { log.info("删除用户成功 " + id); } } 远程调用接口: @FeignClient("Server-Provider") public interface UserClient { @GetMapping("user/{id}") public User get(@PathVariable("id") Long id); @GetMapping("user") public List<User> get(); @PostMapping("user") public void add(@RequestBody User user); @PutMapping("user") public void update(@RequestBody User user); @DeleteMapping("user/{id}") public void delete(@PathVariable("id") Long id); } 服务消费者:Service-Consumer @RestController public class ConsumerController { @Autowired private UserClient userClient; @GetMapping("user/{id}") public User getUser(@PathVariable Long id) { return userClient.get(id); } @GetMapping("user") public List<User> getUsers() { return userClient.get(); } @PostMapping("user") public void addUser() { User user = new User(1L, "yh", "123456"); userClient.add(user); } @PutMapping("user") public void updateUser() { User user = new User(1L, "yh", "123456"); userClient.update(user); } @DeleteMapping("user/{id}") public void deleteUser(@PathVariable Long id) { userClient.delete(id); } } application.yml配置(消费者) server: port: 9000 spring: application: name: Server-Consumer eureka: client: serviceUrl: defaultZone: eureka注册中心地址/或者集群地址
此时可以通过localhost:9000/user/1 来访问通过UserClient来远程调用服务提供者即可 负载均衡 (消费者端配置:配置之后 远程调用就会走均衡负载) @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } Hystrix 服务容错机制 开启Hystrix feign: hystrix: enabled: true
定义服务降级方法的类: @Component public class UserClientFallback implements UserClient { private Logger log = LoggerFactory.getLogger(this.getClass()); @Override public User get(Long id) { return new User(-1L, "default", "123456"); } @Override public List<User> get() { return null; } @Override public void add(User user) { log.info("test fallback"); } @Override public void update(User user) { log.info("test fallback"); } @Override public void delete(Long id) { log.info("test fallback"); } }
在修改远程调用接口: fallback=实现类.class @FeignClient(value = "Server-Provider", fallback = UserServiceFallback.class) public interface UserService { ... }
容错机制配置成功,重启服务即可! 日志配置 Feign提供了日志打印的功能,Feign的日志级别分为四种: NONE: 不记录任何信息。 BASIC: 仅记录请求方法、URL以及响应状态码和执行时间。 HEADERS: 除了记录BASIC级别的信息之外,还会记录请求和响应的头信息。 FULL: 记录所有请求与响应的明细,包括头信息、请求体、元数据等。
日志级别默认为NONE,要改变级别可以在入口类中定义一个日志配置Bean: @EnableDiscoveryClient @EnableFeignClients @SpringBootApplication public class DemoApplication { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
配置Feign客户端的日志级别为debug,Feign日志记录仅响应debug级别: logging: level: com: example: demo: service: UserClient: debug
重启项目访问即可!
热门排行