创建一个Feign项目
yml配置文件
server:
port: 8888
spring:
application:
name: feign-client
eureka:
## 表示为Erureka客户端
client:
## 单台server为服务器不需要注册到服务器上
register-with-eureka: true
## 获取注册列表的信息 道理同上 基本两者参数是一样的
fetch-registry: true
service-url: # Eureka服务提供地址
defaultZone: http://node1:10097/eureka/
启动类
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients //扫描了增加了FeignClient注解的接口
public class SpringCloudFeignApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudFeignApplication.class, args);
}
}
编写service层
public interface MyService {
String infoByFeign();
}
编写Impl层
@Service
public class MyServiceImpl implements MyService {
@Autowired
private EurekaClientFeign eurekaClientFeign;
@Override
public String infoByFeign() {
return eurekaClientFeign.infoByFeign();
}
}
编写Client层
@FeignClient(value = "Eureka-client") //被调用端的服务应用名
public interface EurekaClientFeign {
@GetMapping(value = "/info") //被调用端的info方法 返回类型需与抽象方法返回一致
String infoByFeign();
}
编写Controller层
@RestController
public class MyController {
@Autowired
private MyService myService;
@GetMapping("/infoByFeign")
public String infoByFeign() {
return this.myService.infoByFeign();
}
}
此时依次启动server ,client ,feign接口
访问http://localhost:8888/infoByFeign 刷新内容不变此时已经实现负载均衡