目录
2.3、创建一个service,并使用Feign实现微服务调用
2.4、修改controller调用服务的代码,启动应用进行验证
1、什么是Feign
Feign是Spring Cloud提供的一个声明式的伪http客户端,它使得调用远程服务就像调用本地服务一样简单,只需要创建一个接口,并添加一个注解即可。
Nacos很好的兼容了Feign,Feign默认集成了Ribbon。所以在Nacas下使用Feign默认就实现了负载均衡的效果。
2、Feign的使用
2.1、加入Feign的依赖
<!--Feign组件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.2、在主类上添加Feign的注解
package cn.jack;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients // 开启Feign
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
2.3、创建一个service,并使用Feign实现微服务调用
package cn.jack.service;
import cn.jack.domain.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(value = "service-product") // value是nacos下的服务名
public interface ProductService {
@RequestMapping(value = "/product/{pid}") // @FeignClient + @RequestMapping 就是一个完整的请求路径: http://service-product/product/{pid}
Product findByPid(@PathVariable("pid") Integer pid);
}
2.4、修改controller调用服务的代码,启动应用进行验证
package cn.jack.controller;
import cn.jack.domain.Order;
import cn.jack.domain.Product;
import cn.jack.service.OrderService;
import cn.jack.service.ProductService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Random;
@RestController
@Slf4j
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private OrderService orderService;
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private ProductService productService;
/**
* 下单 -- 基于Feign实现服务调用
* @param pid 商品id
* @return
*/
@RequestMapping("/order/prod/{pid}")
@Deprecated
public Order order(@PathVariable("pid") Long pid) {
log.info("收到下单请求,准备查询商品信息。pid={}", pid);
// 通过Feign调用商品微服务,查询商品信息
Product product = this.productService.findByPid(pid);
log.info("商品信息查询成功。内容为:{}", JSON.toJSONString(product));
// 生成商品信息保存
Order order = new Order();
order.setNumber(1);
order.setPid(pid);
order.setPname(product.getPname());
order.setPprice(product.getPprice());
order.setUid(1);
order.setUsername("测试用户");
this.orderService.createOrder(order);
log.info("订单信息保存成功。内容为:{}", JSON.toJSONString(order));
return order;
}
}
至此,基于Feign实现了微服务调用。关于负载均衡策略的修改,参考https://blog.csdn.net/qq_31155349/article/details/108458378