Springcloud's service consumer feign (iii)

Source: Internet
Author: User

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 XXX. 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:

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:s chemalocation= "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> &LT;ARTIFACTID&GT;SC-F-CHAPTER3&LT;/ARTIFAC tid> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <depende Ncy> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-star Ter-netflix-eureka-client</artifactid> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifacti d>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupi D>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-openfeign</artifactid&        Gt </dependency> </dependencies> </project>

In the project configuration file Application.yml file, specify the program name Service-feign, the port number is 8765, the service registered address is http://localhost:8761/eureka/, The code is as follows: We welcome you to study the relevant technology to learn the source of friends directly seeking exchange and sharing technology: 2147775633

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, alternate browser display

hi forezp,i am from port:8762hi forezp,i am from port:8763

Springcloud's service consumer feign (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.