Feign默认契约时SpringMvcContract,因此Feign可以使用spring MVC注解,
自定义Feign让其使用Feign自带注解。
一、创建项目 导入Feign依赖
二、properties文件
server.port=8889
spring.datasource.url=jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=sx96411
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#jpa
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
#eureka
spring.application.name=microservice-consumer-movie
eureka.client.serviceUrl.defaultZone=http://suxing:suxing123@localhost:8000/eureka/
eureka.instance.prefer-ip-address=true
#Ribbon
#ribbon.eureka.enabled=false
#ribbon.eager-load.enabled=true
#ribbon.eager-load.clients=client1
三、创建Feign配置类,将契约改为Feign原生契约
该类可以不写@Configuration注解,如果添加了@Configuration注解,那么,该类不应该放在主应用程序上下文的@ComponentScan中。
package com.vainglory.feignConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.Contract;
/**
* 该类为Feign的配置类
* 注意:该不应该在主应用程序上下文的@ComponentScan中。
* @author 周立
*/
@Configuration
public class FeignConfiguration {
/**
* 将契约改为feign原生的默认契约。这样就可以使用feign自带的注解了。
* @return 默认的feign契约
*/
@Bean
public Contract feignContract() {
return new feign.Contract.Default();
}
}
四、用户Feign接口
使用@FeignClient注解的configuration属性指定配置类,同时,使用Feign自带注解@RequestLine和@param注解。
可以自定义Feign的编码器、解码器、日志打印以及为Feign添加拦截器。
package com.vainglory.stySpringCloud.feign;
import org.springframework.cloud.openfeign.FeignClient;
import com.vainglory.feignConfig.FeignConfiguration;
import com.vainglory.stySpringCloud.entity.User;
import feign.Param;
import feign.RequestLine;
/**
* 使用@FeignClient的configuration属性,指定feign的配置类。
* @author 周立
*/
@FeignClient(name = "microservice-provider-user", configuration = FeignConfiguration.class)
public interface UserFeignClient {
/**
* 使用feign自带的注解@RequestLine
* @see https://github.com/OpenFeign/feign
* @param id 用户id
* @return 用户信息
*/
@RequestLine("GET /{id}")
public User findById(@Param("id") Long id);
}
五、Controller
package com.vainglory.stySpringCloud.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.vainglory.stySpringCloud.entity.User;
import com.vainglory.stySpringCloud.feign.UserFeignClient;
@RestController
public class MovieController {
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("/user/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
}
}
六、Entity Bean
package com.vainglory.stySpringCloud.entity;
import java.math.BigDecimal;
public class User {
private Long id;
private String username;
private String name;
private Integer age;
private BigDecimal balance;
//省略getter和setter
}
七、启动类
package com.vainglory.stySpringCloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class MicroserviceConsumerMovieFrignCustomizingApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceConsumerMovieFrignCustomizingApplication.class, args);
}
}