***
feign
feign使用
1.添加pom文件
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
2.在启动类上添加开feign注解
@EnableFeignClients
3.定义接口
@FeignClient("yidiankt-user") //指定服务的地址
public interface FeignService {
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
// 指定请求地址 和请求方法
String getUser(@PathVariable("id") int id);
}
4.使用feign
@FeignClient("yidiankt-user") //指定服务的地址
public interface FeignService {
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
// 指定请求地址 和请求方法
String getUser(@PathVariable("id") int id);
}
@RestController
public class OrderController {
@Autowired
OrderService orderService;
@Autowired
FeignService feignService;
@RequestMapping("/order") //不使用feign的
public String addOrder(String name,int id){
String result = orderService.getUser(id);
return "商品"+name+"生成订单:"+result;
}
@RequestMapping("/order2")//使用feign的
public String addOrder2(String name,int id){
String result = feignService.getUser(id);
return "商品"+name+"生成订单:"+result;
}
}
总结:
Ribbon : 需要写URL啊 参数啊 id啊 之类的
Service
@Service
public class OrderService {
@Autowired
RestTemplate restTemplate;
public String getUser(int id){
/**
* yidiankt-user erueka里面服务的名字
*/
String url = "http://yidiankt-user/user/{id}";
/**
* 通过这个方法来调用其他的服务 获取信息
*/
String info = restTemplate.getForObject(url,String.class,id);
return info;
}
}
feign:
只需要写一个接口就可以了的
@FeignClient("yidiankt-user") //指定服务的地址
public interface FeignService {
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
// 指定请求地址 和请求方法
String getUser(@PathVariable("id") int id);
}
调用的话也是同样的调用