客户端负载均衡 Ribbon feign 讲解

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述***

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);

}

调用的话也是同样的调用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值