版权声明:菜鸟_zhengke的博客 https://blog.csdn.net/qq_42014192/article/details/89377430
1.准备工作(设置idea开启多个springboot实例)https://blog.csdn.net/qq_42014192/article/details/89245306
启动eureka-server,端口为8761; 启动service-client 两次,端口分别为8762 、8773.
2.新建一个spring-boot工程,取名为:service-feign;
https://blog.csdn.net/qq_42014192/article/details/88742559
3.pom.xml依赖
<?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.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springcloud_serice-feign_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud_serice-feign_demo</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.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<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-openfeign</artifactId>
</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>
4.配置文件application.yml如下:
server:
port: 8765 #服务端口
spring:
application:
name: service-feign
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
5.在程序的启动类@EnableFeignClients注解开启Feign的功能
@SpringBootApplication
//通过注解@EnableEurekaClient 表明自己是一个eurekaclient
@EnableEurekaClient
//通过@EnableDiscoveryClient向服务中心注册
@EnableDiscoveryClient
//加上@EnableFeignClients注解开启Feign的功能
@EnableFeignClients
public class SpringcloudSericeFeignDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudSericeFeignDemoApplication.class, args);
}
}
6.定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了service-client服务的“/client”接口,代码如下:
@FeignClient(value = "service-client")
public interface HelloService {
@RequestMapping(value = "/client",method = RequestMethod.GET)
String hiService(@RequestParam(value = "name") String name);
}
7.在Web层的controller层,对外暴露一个"/client"的API接口,通过上面定义的Feign客户端SchedualServiceHi 来消费服务。代码如下:
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping(value = "/client")
public String client(@RequestParam String name){
return helloService.clientService(name);
}
}
8.在浏览器上多次访问http://localhost:8765/client?name=forezp,浏览器交替显示:
client forezp,i am from port:8762
client forezp,i am from port:8763