An overview,

Feign is a declarative pseudo Http client that makes writing Http clients much easier. With Feign, you just create an interface and annotate it. It has pluggable annotations that use Feign annotations and JAX-RS annotations. Feign supports pluggable encoders and decoders. Feign is integrated with the Ribbon by default, and Nacos is also fully compatible with Feign and implements load balancing by default

  • Feign uses interface-based annotations
  • Feign integrated the Ribbon

Second, the POM

Create a service consumer project named Hello-spring-cloud-Alibaba-nacos-consumer-feign with pom.xml configuration as follows:

<? The 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 > < the parent > < groupId > com. Funtl < / groupId > < artifactId > hello - spring - the cloud - alibaba - dependencies < / artifactId > < version > 1.0.0 - the SNAPSHOT < / version > < relativePath >). /hello-spring-cloud-alibaba-dependencies/pom.xml</relativePath> </parent> <artifactId>hello-spring-cloud-alibaba-nacos-consumer-feign</artifactId> <packaging>jar</packaging> <name>hello-spring-cloud-alibaba-nacos-consumer-feign</name> <url>http://www.funtl.com</url> <inceptionYear>2018-Now</inceptionYear> <dependencies> <! -- Spring Boot Begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <! -- Spring Boot End --> <! -- Spring Cloud Begin --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <! -- Spring Cloud End --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.funtl.hello.spring.cloud.alibaba.nacos.consumer.feign.NacosConsumerFeignApplication</mainClass> </configuration> </plugin> </plugins> </build> </project>Copy the code

Main increased org. Springframework. Cloud: spring – the cloud – starter – openfeign dependency

Third, the Application

EnableFeign with the @enableFeignClients annotation

package com.funtl.hello.spring.cloud.alibaba.nacos.consumer.feign; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class NacosConsumerFeignApplication { public static void main(String[] args) { SpringApplication.run(NacosConsumerFeignApplication.class, args); }}Copy the code

Create Feign interface

Specify which service to invoke with the @FeignClient(” service name “) annotation. The code is as follows:

package com.funtl.hello.spring.cloud.alibaba.nacos.consumer.feign.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "nacos-provider")
public interface EchoService {

    @GetMapping(value = "/echo/{message}")
    String echo(@PathVariable("message") String message);
}
Copy the code

Five, the Controller

package com.funtl.hello.spring.cloud.alibaba.nacos.consumer.feign.controller; import com.funtl.hello.spring.cloud.alibaba.nacos.consumer.feign.service.EchoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class NacosConsumerFeignController { @Autowired private EchoService echoService; @GetMapping(value = "/echo/hi") public String echo() { return echoService.echo("Hi Feign"); }}Copy the code

Six, application. Yml

Spring: Application: name: nacos-consumer-feign Cloud: nacos: Discovery: server-addr: 127.0.0.1:8848 server: port: 9092 management: endpoints: web: exposure: include: "*"Copy the code

Seven, start the project

Through the browser to http://localhost:8848/nacos, namely Nacos Server url

[img-mkqswrCB-1599814862579] (/assets/ lusifer_20190106143035.png)

You’ll find a new service called nacos-consumer-Feign

Then open the http://localhost:9092/echo/hi, you can see on the browser:

Hello Nacos Discovery Hi Feign
Copy the code

Test load balancing

  • Start multipleconsumer-providerExample, the renderings are as follows:

[img-2iuzeH3W-1599814862583] (/assets/ lusifer_20190106144323.png)

  • Modify the Controller code in the consumer-Provider project to make sure load balancing takes effect

    package com.funtl.hello.spring.cloud.alibaba.nacos.provider;

    import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController;

    @SpringBootApplication @EnableDiscoveryClient public class NacosProviderApplication { public static void main(String[] args) { SpringApplication.run(NacosProviderApplication.class, args); }

    @Value("${server.port}") private String port; @RestController public class EchoController { @GetMapping(value = "/echo/{message}") public String echo(@PathVariable String message) { return "Hello Nacos Discovery " + message + " i am from port " + port; }}Copy the code

    }

  • Several times on the browser to http://localhost:9092/echo/hi, the browser alternate display:

    Hello Nacos Discovery Hi Feign i am from port 8081 Hello Nacos Discovery Hi Feign i am from port 8082

For more details or to get the full Java Microservices architecture, spring Whole Family Barrel video course, please click here to get the Spring Microservices Learning video course.