Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 建立一个接口并对它进行注解,它具备可插拔的注解支持包括Feign注解与JAX-RS注解,Feign还支持可插拔的编码器与解码器,Spring Cloud 增长了对 Spring MVC的注解。在Spring Cloud中使用Feign, 咱们能够作到使用HTTP请求远程服务时能与调用本地方法同样的编码体验。Feign 采用的是基于接口的注解,Feign 整合了ribbon,具备负载均衡的能力。java
启动注册中心eureka-server,服务提供者say-hello。对这两个项目各自启动两个实例。web
1.新建一个springboot工程,取名为service-feignspring
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.definesys</groupId> <artifactId>my_cloud</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.definesys</groupId> <artifactId>service-feign</artifactId> <version>0.0.1-SNAPSHOT</version> <name>service-feign</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.修改配置文件apache
server.port=4444 eureka.client.service-url.defaultZone=http://server2:11112/eureka/,http://server1:11111/eureka/ spring.application.name=service-feign
3.修改启动类浏览器
@SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @EnableFeignClients public class ServiceFeignApplication { public static void main(String[] args) { SpringApplication.run(ServiceFeignApplication.class, args); } }
4.定义Feign接口
经过@ FeignClient(“服务名”),来指定调用哪一个服务。springboot
@FeignClient(value = "say-hello") public interface SayHelloFeignSer { @RequestMapping(value = "/sayHello",method = RequestMethod.GET) String feignSayHelloSer(@RequestParam(value = "helloName") String helloName); }
5.建立controller,对外提供接口app
@RestController public class FeignServiceController { //编译器报错,无视。 由于这个Bean是在程序启动的时候注入的,编译器感知不到,因此报错。 @Autowired private SayHelloFeignSer sayHelloFeignSer; @GetMapping("/feignSayHello") public String feignSayHelloCtr(@RequestParam("helloName")String helloName){ return sayHelloFeignSer.feignSayHelloSer(helloName); } }
6.启动service-feign
访问http://localhost:4444/feignSayHello?helloName=adaad,发现浏览器交替显示端口,说明feign已经集成ribbon。负载均衡
1.修改say-hello项目,在SayHelloController中添加两个方法maven
/** * get请求多请求参数 * @param userName * @param userPassword * @return */ @RequestMapping(value = "/manyParams",method = RequestMethod.GET) public String manyParamsCtr(@RequestParam("userName")String userName,@RequestParam("userPassword")String userPassword){ return "用户名:"+userName+",用户密码"+userPassword; } /** * post 对象参数 * @param user * @return */ @RequestMapping(value = "/objParams",method = RequestMethod.POST) public User objParamsCtr(@RequestBody User user){ System.out.println(JSON.toJSON(user)); return user; }
public class User { private String userName; private String userPassword; private String userSex; ...get(),set() }
2.修改service-feign项目Feign接口spring-boot
/** * 多参数get请求 * @param userName * @param userPassword * @return */ @RequestMapping(value = "/manyParams",method = RequestMethod.GET) String manyParamsSer(@RequestParam("userName")String userName,@RequestParam("userPassword")String userPassword); /** * 对象参数 post请求 * @param user * @return */ @RequestMapping(value = "/objParams",method = RequestMethod.POST) Object objParamsCtr(@RequestBody Object user);
3.修改service-feign项目FeignServiceController
@GetMapping("/feignManyParams") public String feignManyParamsCtr(@RequestParam("userName")String userName,@RequestParam("userPassword")String userPassword){ return sayHelloFeignSer.manyParamsSer(userName,userPassword); } @PostMapping("/feignObjParam") public Object feignObjParamCtr(@RequestBody Map<String,Object> map){ return sayHelloFeignSer.objParamsCtr(map); }
注:为了避免重复建立User实体类,这里用map去接收参数。
4.从新启动say-hello,service-feign两个项目
经过postman访问 /feignManyParams接口
访问 /feignObjParam接口