The spring cloud feign method uses the service
feign to use the interface annotation
feign to integrate the ribbon (with load balancing function)
<?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.midea</groupId> <artifactId>base</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>base</name> <!-- Set the warehouse, search according to the set order. --> <!--<repositories> <repository> <id>public</id> <name>Team Nexus Repository</name> <url>http://10.33.183.113:8081/nexus/content/groups/public</url> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>public</id> <name>Team Nexus Repository</name> <url>http://10.33.183.113:8081/nexus/content/groups/public</url> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </pluginRepository> </pluginRepositories> --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> </build> </project>
server.port=8765 #logging.pattern.level=INFO #server path server.context-path=/feign # #eureka.instance.hostname=localHost # Indicates whether to register itself to the eureka server, because the current application is the eureka server, there is no need to register itself, so here is false. #eureka.client.registerWithEureka=false #fetchRegistry indicates whether to obtain registration information from the eureka server, same as above, no need here #eureka.client.fetchRegistry=false #Set the address where the eureka server is located. Both query services and registration services need to rely on this address. #eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ eureka.client.serviceUrl.defaultZone=http\://localhost\:8761/eurekaServer/eureka/ spring.application.name=feign
package com.Service; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @FeignClient(value = "eureka-Client") //Service name public interface CallService { @RequestMapping(value = "/eurekaClient/Test/test",method = RequestMethod.GET) String callService(); }
package com.controller; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.Service.CallService; import io.swagger.annotations.Api; @Api(value = "/FeignTest", description = "测试接口", tags = "TestController") @RequestMapping("/FeignTest") @RestController public class TestController { final static Logger logger = LogManager.getLogger(TestController.class); @Autowired CallService callService; @RequestMapping(value = "/test", method = RequestMethod.GET) public String test() { logger.info("test"); // Call the service to make a remote call String temp = callService.callService(); logger.info("temp == " + temp); return temp; } }
package com; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication //spring boot starts the application //@EnableDiscoveryClient //Discovery Service" has multiple implementations, such as: eureka, consul, zookeeper. @EnableEurekaClient //Only for eureka @EnableFeignClients //Feign public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }