Spring cloud 3 - Eurake+Feign服务使用
程序员文章站
2022-06-05 08:08:27
...
前面已经搭建了Eurake的服务,以及提供了一个dept的三层架构查询。
现在只需要完成该系统从Eurake调用deptService。
jar包(有可以不需要加)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
</dependencies>
application.properties文件
server.port=8081
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.registry-fetch-interval-seconds: 30
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:8761/eureka/
spring.application.name=eurake-user-controller
需要写一个接口来调用给我们提供服务的类
//Eurake里面的应用名称
@FeignClient("USER-SERVICE")
public interface EurakeService {
//必须并且只能填写一种请求方法
//value:写请求那个项目的controller层路径
@RequestMapping(method = RequestMethod.GET,value="/dept/find")
public List<Dept> findAll();
}
本系统的调用
@Controller
@RequestMapping("/dept")
public class DeptController {
//把接口注入进来即可
@Autowired
private EurakeService eurakeService;
@RequestMapping("find")
@ResponseBody
public List<Dept> find(){
List<Dept> list = eurakeService.findAll();
System.out.println(list.size());
return list;
}
}
启动
//Springboot启动
@SpringBootApplication
//Eureka客户端
@EnableEurekaClient
//Feign客户端
@EnableFeignClients
public class StartProject {
public static void main(String[] args) {
SpringApplication.run(StartProject.class, args);
}
}
下一篇: quartz定时任务
推荐阅读
-
Spring Cloud Gateway 服务网关快速实现解析
-
详解spring cloud使用Hystrix实现单个方法的fallback
-
详解spring cloud Feign使用中遇到的问题总结
-
使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务的方法(推荐)
-
Spring Cloud Gateway 之请求坑位[微服务IP不同请求会失败]
-
JSP spring boot / cloud 使用filter防止XSS
-
详解Spring Cloud Consul 实现服务注册和发现
-
详解Spring cloud使用Ribbon进行Restful请求
-
Spring-cloud 注册服务提供者搭建方法
-
Spring-cloud 服务发现与消费(以ribbon为例)