欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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);
    }
}

链接:https://pan.baidu.com/s/1IJLUhyUjPBHHb9_XzQsElA 密码:gsya

相关标签: 技术分享