Feign is a pseudo Http client a declarative, it makes writing easier Http client. Using Feign, only you need to create an interface and annotation. It has pluggable annotation feature can be used Feign JAX-RS annotations and notes. Feign pluggable encoder and decoder. Feign default integrated Ribbon, and Eureka and combined, the default implementation of load balancing. Learn springcloud architecture can be added to beg: 3536247259
First, create a feign service
Create a new spring-boot project, named serice-feign, in its pom-dependent file introduced Feign start spring-cloud-starter-feign, Eureka start dependent spring-cloud-starter-netflix-eureka-client, Web of start dependent spring-boot-starter-web, code is as follows:
<?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>
<groupId>com.forezp</groupId>
<artifactId>service-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-ribbon</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.forezp</groupId>
<artifactId>sc-f-chapter2</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-netflix-ribbon</artifactId>
</dependency>
</dependencies>
</project>
In the address registration center of the project configuration file specifies services to http: // localhost: 8761 / eureka /, program name for the service-ribbon, the program for the 8764 port. Profile application.yml as follows:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: service-ribbon
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run( ServiceRibbonApplication.class, args );
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
Write a test class HelloService, to consume service-hi services by injecting restTemplate ioc containers before "/ hi" interfaces, where we direct replacement for specific url address with the name of the program, it will be based on the service name in the ribbon selection of a particular service instance, according to the service instance when the replacement request of a specific service name url out, as follows:
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hiService(String name) {
return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
}
}
Visit http many times in the browser: // localhost:? 8764 / hi name = forezp, browser alternates:
hi forezp,i am from port:8762
hi forezp,i am from port:8763
This shows that when we by calling restTemplate.getForObject ( "http: // SERVICE-HI / hi name =?" + Name, String.class) when the method has been done load balancing, access to different services, for instance ports.