# 7-1 使用Feign实现远程HTTP调用

  • 什么是Feign?
  • 用Feign重构前面的代码

# 导航

回到spring cloud alibaba导航页

# 什么是Feign?

# 使用Feign重构代码

  1. 加依赖
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
1
2
3
4
  1. 写注解

在启动类上加上 @EnableFeignClients 注解

package com.itmuch.contentcenter;

import lombok.Builder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import tk.mybatis.spring.annotation.MapperScan;

//MapperScan注解作用:扫描mybatis哪些包里面的接口
@MapperScan(basePackages = "com.itmuch")
@SpringBootApplication
@EnableFeignClients
public class ContentCenterApplication {

    public static void main(String[] args) {
        SpringApplication.run(ContentCenterApplication.class, args);
    }
    //@Bean 在spring容器中,创建一个对象,名称/ID是:方法名
    //<bean id="restTemplate" class="xxx.RestTemplate">
    //@LoadBalanced : 让RestTemplate整合了Ribbon
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

创建一个包:feignclient,在该包下创建接口:UserCenterFeignClient, 代码如下:

package com.itmuch.contentcenter.feignclient;

import com.itmuch.contentcenter.domain.dto.user.UserDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @ClassName:UserCenterFeignClient
 * @Description:
 * @author: Torey
 */
//@FeignClient(name = "")
// name:要请求的微服务名称
@FeignClient(name = "user-center")
public interface UserCenterFeignClient {
    /**
     * 当findById调用的时候,Feign就会构造出 user-center/users{id} 这样的URL,
     * 并且去请求,将相应的结果 转换成 UserDTO
     * @param id
     * @return
     */
//    @GetMapping("/users/{id}")
//    UserDTO findById(@PathVariable Integer id); //使用这种方式会报错
    @GetMapping("/users/{id}")
    UserDTO findById(@RequestParam(value = "id",required = false) Integer id);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

ShareService 使用Feign调用:

package com.itmuch.contentcenter.service.content;

import com.itmuch.contentcenter.dao.share.ShareMapper;
import com.itmuch.contentcenter.domain.dto.content.ShareDTO;
import com.itmuch.contentcenter.domain.dto.user.UserDTO;
import com.itmuch.contentcenter.domain.entity.share.Share;
import com.itmuch.contentcenter.feignclient.UserCenterFeignClient;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.naming.ldap.PagedResultsControl;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

/**
 * @ClassName:ShareService
 * @Description:
 * @author: Torey
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShareService {
//    @Autowired
    private final ShareMapper shareMapper;
    private final UserCenterFeignClient userCenterFeignClient;
    public ShareDTO findById(Integer id){
        Share share = shareMapper.selectByPrimaryKey(id);
        Integer userId = share.getUserId();
        UserDTO userDTO= userCenterFeignClient.findById(userId);
// 消息的装配
        ShareDTO shareDTO=new ShareDTO();
        BeanUtils.copyProperties(share,shareDTO);
        shareDTO.setWxNickname(userDTO.getWxNickname());
        return shareDTO;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

# 导航,上一页,下一页

上一页
下一页

# 支持我-微信扫一扫-加入微信公众号

Aseven公众号

# 赞赏作者

赞赏作者