Feign服务端
1.添加Maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.启动类配置
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeignServerApplication {
private static final Logger log = LoggerFactory.getLogger(FeignServerApplication.class);
public static void main(String[] args) {
SpringApplication.run(FeignServerApplication.class, args);
log.info("========== feign server start success =====");
}
}
3.开发控制层
@RestController
@RequestMapping("hello")
public class HelloFeignService {
@PostMapping("add")
public String add(@RequestBody String body) {
return body;
}
}
Feign客户端
1.添加maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.添加feign相关配置
feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic
hystrix:
enabled: true
# feign client 开始日志
logging:
level:
com.lmn.DemoClient: debug
3.启动类启动FeignClient
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeignClientApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(FeignClientApplication.class);
public static void main(String[] args) {
SpringApplication.run(FeignClientApplication.class, args);
LOGGER.info("======= start success ====");
}
}
4.定义FeignClient接口
@FeignClient(name = "feign-server")
public interface DemoClient {
@PostMapping("/hello/add")
String add(String body);
}
5.调用定义的FeignClient接口
@RestController
public class ControllerTest {
private static final Logger LOG = LoggerFactory.getLogger(ControllerTest.class);
@Autowired
private DemoClient demoClient;
@GetMapping("/hello/add")
void add(){
String resposne = demoClient.add("test test");
LOG.info("结果:" + resposne);
}
}