The previous article, about how to go through the Resttemplate+ribbon to consumer services, this article mainly on how to go through the feign to consumer services.
I. Introduction of FEIGN
Feign is a declarative pseudo-HTTP client that makes it easier to write HTTP clients. With feign, you only need to create an interface and annotate it. It has pluggable annotation features that can be used with feign annotations and jax-rs annotations. The feign supports pluggable encoders and decoders. The feign integrates the Ribbon by default and combines with Eureka to achieve load balancing by default.
Nutshell:
- Feign uses an interface-based annotation
- Feign integrates the ribbon, with load balancing capability
- Integrated Hystrix, with the ability to fuse
Ii. preparatory work
Continue with the previous section of the project, start the Eureka-server, the port is 8761; Start Service-hi two times with ports of 8762, 8773, respectively.
Third, create a feign service
Create a new Spring-boot project, named Serice-feign, in its pom file introduced feign start dependent spring-cloud-starter-feign, Eureka's start relies on spring-cloud-starter-netflix-eureka-client, the web's start-dependent Spring-boot-starter-web, the code is as follows:
<Projectxmlns="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><Groupid>com.forezp</Groupid><Artifactid>service-feign</Artifactid><Version>0.0.1-snapshot</Version><Packaging>jar</Packaging><Name>service-feign</Name><Description>demo Project for Spring Boot</Description><Parent><Groupid>com.forezp</Groupid><Artifactid>sc-f-chapter3</Artifactid><Version>0.0.1-snapshot</Version></Parent><Dependencies><Dependency><Groupid>org.springframework.cloud</Groupid><Artifactid>spring-cloud-starter-netflix-eureka-client</Artifactid></Dependency><Dependency><Groupid>org.springframework.boot</Groupid><artifactid>spring-boot-starter-web </artifactid> </ dependency> <dependency> <groupid>org.springframework.cloud</ groupid> <artifactId> Spring-cloud-starter-openfeign</artifactId> </dependency> </ dependencies> </ PROJECT>
In the project configuration file Application.yml file, specify the program named Service-feign, the port number is 8765, the service registered address is http://localhost:8761/eureka/, the code is as follows:
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/server: port: 8765spring: application: name: service-feign
In the program's startup class Servicefeignapplication, plus the @enablefeignclients annotation opens the feign function:
@SpringBootApplication@EnableEurekaClient@EnableDiscoveryClient@EnableFeignClientspublic class ServiceFeignApplication { public static void main(String[] args) { SpringApplication.run( ServiceFeignApplication.class, args ); }}
Defines a feign interface that specifies which service to invoke through @ feignclient ("service name"). For example, in the code called the Service-hi Service "/hi" interface, the code is as follows:
@FeignClient(value = "service-hi")public interface SchedualServiceHi { @RequestMapping(value = "/hi",method = RequestMethod.GET) String sayHiFromClientOne(@RequestParam(value = "name") String name);}
At the controller layer of the Web layer, exposes a "/hi" API interface, consumes the service through the feign client Schedualservicehi defined above. The code is as follows:
@RestControllerpublic class HiController { //编译器报错,无视。 因为这个Bean是在程序启动的时候注入的,编译器感知不到,所以报错。 @Autowired SchedualServiceHi schedualServiceHi; @GetMapping(value = "/hi") public String sayHi(@RequestParam String name) { return schedualServiceHi.sayHiFromClientOne( name ); }}
Launch program, multiple access to Http://localhost:8765/hi?name=forezp, browser alternate display:
Hi forezp,i am from port:8762
Hi forezp,i am from port:8763
Feign source code Analysis: http://blog.csdn.net/forezp/article/details/73480304
This article source code download:
Https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter3
V. References
This article references the following:
http://blog.csdn.net/forezp/article/details/69808079
Http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html
Original address: Http://blog.csdn.net/forezp. 81040965
Springcloud Tutorials | Article Three: Service Consumers (feign) (Finchley version)