在Spring Cloud Netfilx栈中,各个微服务都是以Http接口的形式暴露自身服务的,因此在调用远程服务的时候就必须使用Http客户端。我们可以使用JDK原生的URLConnection、Apache的Http Client、Netty的异步Http Client、Spring的RestTemplate,但是用起来最方便、优雅的还属Feign。
Feign是一种声明式的Http客户端,Spring Cloud应用在启动时,启动类上如果有@EnableFeignClients注解,会扫描指定路径下的拥有@FeignClient注解的接口,生成代理,并注册到Spring容器。生成代理时Feign会为每个接口方法创建一个RequestTemplate对象,该对象封装了Http请求需要的所有信息,请求参数名、请求方法等信息都是在这个过程确定的,Feign的模板化就体现在这里。
如果不使用springcloud的相关组件,调用服务需要走http,配置请求head、body,然后才能发起请求。获得响应体后,还需解析等操作,十分繁琐。 Spring Cloud的Feign组件帮我们提供了优雅的解决方案,Feign的一个关键机制就是使用了动态代理: 首先,如果你对某个接口定义了@FeignClient注解,Feign就会针对这个接口创建一个动态代理; 接着你要是调用那个接口,本质就是会调用 Feign创建的动态代理,这是核心中的核心; Feign的动态代理会根据你在接口上的@RequestMapping等注解,来动态构造出你要请求的服务的地址; 最后针对这个地址,发起请求、解析响应 服务消费者启动类:
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients(basePackages = { "com.kyle.client.feign.inter" })
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
1.服务消费者添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
2.服务提供者启动类:
@SpringBootApplication
@EnableDiscoveryClient
public class B1ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(B1ServiceApplication.class, args);
}
}
3.在服务提供者定义HelloServiceFeign,接口@FeignClient注解指定服务名来绑定服务,然后再使用Spring MVC 的注解来绑定具体该服务提供的REST接口。
@FeignClient(value = "hello-service-provider")
public interface HelloServiceFeign {
@RequestMapping(value = "/demo/getHost", method = RequestMethod.GET)
public String getHost(String name);
@RequestMapping(value = "/demo/postPerson", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
public Person postPerson(String name);
}
注意:这里服务名不区分大小写,所以使用hello-service-provider和HELLO-SERVICE-PROVIDER都是可以的。 另外,在Brixton.SR5版本中,原有的serviceId属性已经被废弃,若要写属性名,可以使用name或value。 4.在服务消费者创建一个RestClientController来实现对Feign客户端的调用。 使用@Autowired直接注入上面定义的HelloServiceFeign实例,并在postPerson函数中调用这个绑定了 hello-service服务接口的客户端来向该服务发起/hello接口的调用。
@RestController
public class RestClientController {
@Autowired
private HelloServiceFeign client;
/**
* @param name
* @return Person
* @Description: 测试服务提供者post接口
*/
@RequestMapping(value = "/client/postPerson", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
public Person postPerson(String name) {
return client.postPerson(name);
}
/**
* @param name
* @return String
* @Description: 测试服务提供者get接口
*/
@RequestMapping(value = "/client/getHost", method = RequestMethod.GET)
public String getHost(String name) {
return client.getHost(name);
}
}
需要在application.properties中指定服务注册中心,并定义服务提供者的应用名为hello-service-provider。 启动两个服务,设置服务消费者的访问端口为8080,服务提供者的访问端口为8090,访问http://localhost:8080/client/postPerson,成功调用服务即可。