OpenFeign功能:Feign是一种声明式、模板化的HTTP客户端。现在已升级为OpenFeign。基本使用方式如下。
1.修改02-customer项目
在pom文件中添加openfeign的引用:
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.cloud</groupId> 4 <artifactId>spring-cloud-starter-openfeign</artifactId> 5 </dependency> 6 <!-- 7 其他组件的引用 8 -->9 </dependencies>
修改02-customer的启动类,添加一个注解:
1 package com.yangasen; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.client.loadbalancer.LoadBalanced; 6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 7 import org.springframework.cloud.openfeign.EnableFeignClients; 8 import org.springframework.context.annotation.Bean; 9 import org.springframework.web.client.RestTemplate; 10 11 @SpringBootApplication 12 @EnableEurekaClient 13 @EnableFeignClients 14 public class CustomerApplication { 15 public static void main(String[] args) { 16 SpringApplication.run(CustomerApplication.class,args); 17 } 18 @Bean 19 @LoadBalanced 20 public RestTemplate restTemplate(){ 21 return new RestTemplate(); 22 } 23 }
在02和03项目中添加lombok注解,并定义一个Customer类,:
1 <dependency> 2 <groupId>org.projectlombok</groupId> 3 <artifactId>lombok</artifactId> 4 </dependency>
1 package com.yangasen.entity; 2 3 import lombok.AllArgsConstructor; 4 import lombok.Data; 5 import lombok.NoArgsConstructor; 6 7 @Data 8 @NoArgsConstructor 9 @AllArgsConstructor 10 public class Customer { 11 12 private Integer id; 13 14 private String name; 15 16 private Integer age; 17 }
在02-customer中添加一个接口,用于映射03-search项目注册在EUREKA中注册的SEARCH服务:
1 package com.yangasen.client; 2 3 import com.yangasen.entity.Customer; 4 import org.springframework.cloud.openfeign.FeignClient; 5 import org.springframework.web.bind.annotation.*; 6 7 @FeignClient("SEARCH") 8 public interface SearchClient { 9 @RequestMapping(method = RequestMethod.GET, value = "/search") 10 String search(); 11 12 //单个参数时,推荐使用@PathVariable 13 @RequestMapping(value = "/search/{id}",method = RequestMethod.GET) 14 Customer findById(@PathVariable(value = "id") Integer id); 15 16 //多个参数时,也可以采用@RequestParam,不要省略Value属性 17 @RequestMapping(value="/getcustomer",method = RequestMethod.GET) 18 Customer getCustomer(@RequestParam(value = "id") Integer id, @RequestParam(value = "name") String Name); 19 20 //传递的参数比较复杂时,会采用POST的方式请求,传递对象时统一采用JSON格式发送,需要添加@RequestBody注解 21 @RequestMapping(value = "/save",method = RequestMethod.GET)//自动转换为post请求 405 22 Customer save(@RequestBody Customer customer); 23 }
其中第10、14、18、22行对应的是03-search中controller的action:
1 package com.yangasen.controller; 2 3 import com.yangasen.entity.Customer; 4 import org.springframework.beans.factory.annotation.Value; 5 import org.springframework.web.bind.annotation.*; 6 7 @RestController 8 public class SearchController { 9 10 @Value("${server.port}") 11 private String port; 12 13 @GetMapping("/search") 14 public String search(){ 15 return "search:"+port; 16 } 17 18 19 @GetMapping("/search/{id}") 20 public Customer findById(@PathVariable Integer id){ 21 return new Customer(1,"zhangsan",18); 22 } 23 24 @GetMapping("/getcustomer") 25 public Customer getCustomer(@RequestParam Integer id,@RequestParam String name){ 26 return new Customer(id,name,18); 27 } 28 29 @PostMapping("/save")//自动转换为post请求 405 30 public Customer save(@RequestBody Customer customer){ 31 return customer; 32 } 33 }
最后修改02-customer的controller,修改调用方式,通过spring获取接口的实例,直接调用实例的search()方法:
1 package com.yangasen.controller; 2 3 import com.netflix.discovery.EurekaClient; 4 import com.yangasen.client.SearchClient; 5 import com.yangasen.entity.Customer; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.web.bind.annotation.GetMapping; 8 import org.springframework.web.bind.annotation.PathVariable; 9 import org.springframework.web.bind.annotation.RequestParam; 10 import org.springframework.web.bind.annotation.RestController; 11 import org.springframework.web.client.RestTemplate; 12 13 @RestController 14 public class CustomerController { 15 16 @Autowired 17 private RestTemplate restTemplate; 18 19 @Autowired 20 private EurekaClient eurekaClient; 21 22 @Autowired 23 private SearchClient searchClient; 24 25 @GetMapping("/customer") 26 public String customer(){ 27 String result = searchClient.search(); 28 return result; 29 } 30 31 @GetMapping("/search/{id}") 32 public Customer findById(@PathVariable Integer id){ 33 return searchClient.findById(id); 34 } 35 36 @GetMapping("/getcustomer") 37 public Customer findById(@RequestParam Integer id, @RequestParam String name){ 38 return searchClient.getCustomer(id,name); 39 } 40 41 @GetMapping("/save")//自动转换为post请求 42 public Customer save(Customer customer){ 43 return searchClient.save(customer); 44 } 45 }
2.测试效果:
重启全部五个模块,可以得到与之前一样的效果。
访问:http://localhost:8080/customer
可轮询(权重)得到8081和8082的服务的响应。
访问:http://localhost:8080/search/1
访问:http://locahost:8080/getcustomer?id=1&name=xxx
访问:http://localhost:8080/save?id=1&name=xxx&age=10