以下心得皆是个人根据网上资料学习实现后的个人理解,如有不对不足之处,请指出,勿喷!
前言:
这里我们还是继续使用之前的服务和工程:一个server,两个client。
直接上手:
我们新建一个工程,命名随意,其中pom文件如下:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springcloud</groupId>
<artifactId>wujie_study_day03_eureka_feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>wujie_study_day03_eureka_feign</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我们在启动类上加入@EnableFeignClients开启Feign的功能。
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class WujieStudyDay03EurekaFeignApplication {
public static void main(String[] args) {
SpringApplication.run(WujieStudyDay03EurekaFeignApplication.class, args);
}
}
新建一个Feign的接口并使用注解@FeignClient(value="需要调用的服务")
@FeignClient(value = "wujie-study-day01-eureka-client")
public interface ServerFeignService {
@RequestMapping(value = "test")
public String testFeign(@RequestParam(value = "name") String name);
}
在web层新建一个controller来使用这个服务:
@RestController
public class FeignTestController {
@Autowired
private ServerFeignService serverFeignService;
@RequestMapping(value = "testFeign")
public String testFeign(@RequestParam String name){
return serverFeignService.testFeign(name);
}
}
修改配置文件如下:
server:
port: 8892
spring:
application:
name: eureka-feign
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka
最后我们依次启动相应的项目,然后我们访问:http://localhost:8892/testFeign?name="XXX";
注意事项
我本人犯错:在定义的Feign接口的方法里时,应注意在@RequestMapping(value="这里需要写我们要调用的服务接口")