Spring Cloud 2-Feign 声明式服务调用(三)

 

 

简化RestTemplate调用形式

1. pom.xml

<!-- feign 声明式服务调用 -->
<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2. application.yml

spring:
  application:
    name: feign-client
    
server:
  port: 8801

3. FeginClientApplication.java

@SpringBootApplication
@EnableFeignClients
public class FeginClientApplication {

 public static void main(String[] args) {
  SpringApplication.run(FeginClientApplication.class, args);
 }

}

@EnableFeignClients 启动Feign客户端

4. HelloServiceClient.java

@Service
@FeignClient("hello-service")
public interface HelloServiceClient {
  
  @RequestMapping(method = RequestMethod.GET, value = "/hello")
  String hi();
  
}
  • @FeignClient("hello-service") 指定注册服务的名字
  • /hello 服务请求地址
注意:
@RequestMapping(method = RequestMethod.GET, value = "/hello")

1. 不能使用GetMapping
2. 需通过value指定请求路径

访问: http://localhost:8801/fegin/hi

Hello World!

猜你喜欢

转载自www.cnblogs.com/linyufeng/p/10187927.html