搭建步骤
1.搭建pay-server工程
2.导入相关依赖,注册到EurekaServer (拷贝order-server相关代码进行修改)
3.导入Feign的依赖
4.pay-server的主配置类开启Feign : @EnableFeignClients
5.pay-server编写Fiegn的客户端接口:UserFeignClient
6.pay-server编写controller注入UserFeignClient调用服务
1.什么是Feign
1.1.Ribbon有什么问题
前面的可以发现当我们通过RestTemplate调用其它服务的API时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,拼接请求字符串就会效率低下。
1.2.什么是Feign
- Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单。Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解,就可以定义好HTTP请求的参数、格式、地址等信息。而Feign则会完全代理HTTP请求,我们只需要像调用方法一样调用它就可以完成服务请求及相关处理。Feign整合了Ribbon和Hystrix(关于Hystrix我们后面再讲),可以让我们不再需要显式地使用这两个组件。
2.搭建新支付服务注册到Eureka
2.1.创建工程
1.创建工程 springcloud-pay-server-1040,用来集成Feign
2.2.导入依赖
- 我们先让支付服务注册到注册中心后再考虑集成Feign , pom.xml如下
<dependencies>
<!--1.导入EurekaClient的包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--web包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--user-common-->
<dependency>
<groupId>cn.itsource.springboot</groupId>
<artifactId>springcloud-user-common</artifactId>
</dependency>
</dependencies>
2.3.配置类如下
/**
* 支付的启动类
* @EnableFeignClients :开启Feign支持
*/
@SpringBootApplication
@EnableEurekaClient
public class PayServerApplication1040
{
public static void main( String[] args )
{
SpringApplication.run(PayServerApplication1040.class);
}
}
2.4.配置文件application.yml
#注册到EurekaServer
eureka:
client:
serviceUrl:
defaultZone: http://peer1:1010/eureka/,http://peer2:1011/eureka/,http://peer3:1012/eureka/
#使用ip地址进行注册
instance:
prefer-ip-address: true
#要指定服务的实例ID
instance-id: pay-server:1040
server:
port: 1040
spring:
application:
name: pay-server #服务名
3.集成Feign
3.1.加入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
3.2.开启Feign
主配置类增加@EnableFeignClients标签
/**
* 支付的启动类
* @EnableFeignClients :开启Feign支持
*/
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients("cn.itsource.springboot.feignclient")
public class PayServerApplication1040
{
public static void main( String[] args )
{
SpringApplication.run(PayServerApplication1040.class);
}
}
3.3.编写Feign的客户端接口
写一个UserFeignClient 类(用来调用User服务的)
@FeignClient("user-server")
public interface UserFeignClient {
//订单服务来调用这个方法 http://localhost:1020/user/10
// @GetMapping(value = "/user/{id}" )
@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
User getById(@PathVariable("id")Long id);
}
- 解释
UserFeignClient : 这个接口就是用来调用 user-server这个服务的Feign的客户端接口
@FeignClient(“user-server”) : user-server是服务的名字,我们要掉用户服务
feign根据服务名能够找到 目标服务 ,根据方法上的 RequestMapping的值可以找到目标服务的controller的方法
- 千万注意:
1.服务名一定不要写错
2.@RequestMapping 一定要和目标服务的controller的方法的requestMapping一样
3.方法的参数一定要和目标服务的controller的方法的参数一样
4.方法的返回值一定要和目标服务的controller的方法的返回值一样
- 建议
服务名直接去目标服务配置中拷贝
方法直接从目标服务controller中拷贝
3.4.编写Controller
- 通过注入UserFeignClient ,直接发起调用
//支付服务的controller
@RestController
public class PayController{
@Autowired
private UserFeignClient userFeignClient;
//浏览器来掉
@RequestMapping("/pay/{id}")
public User getById(@PathVariable("id")Long id){
//使用Feign调用用户服务获取User
return userFeignClient.getById(id);
}
}
3.5.测试
- 通过浏览器访问pay-server的controller