码迷,mamicode.com
首页 > 编程语言 > 详细

SpringCloud之整合Feign

时间:2019-02-17 00:39:37      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:pid   span   编写   ant   art   body   ide   nbu   VID   

假设提供者有如下服务接口方法

@RestController
@RequestMapping("/person")
public class PersonController {
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public Person get(@PathVariable Integer id) {
        Person p = new Person();
        p.setId(id);
        p.setName("name" + id);
        return p;
    }
}

 

 

服务调用者端 pom.xml加入依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

 

 

在服务调用者端启动类开启feign

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }
}

 

在服务调用者端编写一个PersonClient.java

//声明调用的服务名称
@FeignClient("my-provider") 
public interface PersonClient {    
    @RequestMapping(method = RequestMethod.GET, value = "/person/get/{personId}")
    Person getPerson(@PathVariable("personId") Integer id);
}

 

在服务调用者端增加一个测试TestController调用提供者

@Autowired
private PersonClient personClient;

@GetMapping("/personGetFeignTest")
@ResponseBody
public Person personGetFeignTest() {
    return personClient.getPerson(100);
}

 

 

启动注册中心 、提供者、调用者

访问调用者的personGetFeignTest url 测试是否可以成功调用提供者服务

SpringCloud之整合Feign

标签:pid   span   编写   ant   art   body   ide   nbu   VID   

原文地址:https://www.cnblogs.com/zengnansheng/p/10389838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!