Feign:一个可以把远程服务提供方的 rest 接口变成本地方法调用
对上一个项目进行改造:
1.改造service0
Service0.java:
package service0;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@SpringBootApplication(scanBasePackages = "service0") @EnableEurekaClient @EnableWebMvc public class Service0 { public static void main(String[] args) { SpringApplication.run(Service0.class, args); } }
-----------------------------------------------------------------------------------------------------------
Service0Controller.java:
package service0.controller;
import java.util.Date;
import org.apache.commons.lang.time.FastDateFormat; import org.springframework.web.bind.annotation.*;
@RestController public class Service0Controller { @GetMapping("/service0/{value}") public String service0(@PathVariable("value") String value){ return value + ":" + FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.sss").format(new Date()); }
}
------------------------------------------------------------------------------
启动discovery,启动service0后,测试service0的调用:
2.改造service1
Service1.java:
package service1;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@SpringBootApplication(scanBasePackages = "service1") @EnableFeignClients @EnableDiscoveryClient @EnableWebMvc public class Service1 {
public static void main(String[] args) { // TODO Auto-generated method stub SpringApplication.run(Service1.class, args); }
}
--------------------------------------------------------------------
Server0Client.java:
package service1.client;
import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient("service0") //service0的application中的name属性一致 public interface Server0Client { @RequestMapping(method=RequestMethod.GET,path="/service0/{value}") public String service0(@PathVariable("value") String value);//与service0controller的方法一致
}
--------------------------------------------------------------------------
Service1Controller.java:
package service1.controller;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController;
import service1.client.Server0Client;
@RestController public class Service1Controller { @Autowired private Server0Client server0Client; @GetMapping("/service1/{value2}") public String service1(@PathVariable("value2") String value2){ return server0Client.service0(value2); }
} -----------------------------------------------------------------------
启动discovery,启动service0,service1后,测试service1的调用:
访问service1时,又通过本地调用调用service0的接口,最后返回service0的处理结果。