SpringCloud系列——Feign 服務調用

  前言

  前面咱們已經實現了服務的註冊與發現(請戳:SpringCloud系列——Eureka 服務註冊與發現),而且在註冊中心註冊了一個服務myspringboot,本文記錄多個服務之間使用Feign調用。css

  Feign是一個聲明性web服務客戶端。它使編寫web服務客戶機變得更容易,本質上就是一個http,內部進行了封裝而已。html

  GitHub地址:https://github.com/OpenFeign/feignjava

  官方文檔:https://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.1.0.RC2/single/spring-cloud-openfeign.htmlgit

 

  服務提供者

  提供者除了要在註冊中心註冊以外,不須要引入其餘東西,注意一下幾點便可:github

  一、經測試,默認狀況下,feign只能經過@RequestBody傳對象參數web

  二、接參只能出現一個複雜對象,例:public Result<List<UserVo>> list(@RequestBody UserVo entityVo) { ... }redis

  三、提供者若是又要向其餘消費者提供服務,又要向瀏覽器提供服務,建議保持原先的Controller,新建一個專門給消費者的Controllerspring

  

  測試Controller接口數據庫

@RestController
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private UserService userService;
    @RequestMapping("list")
    public Result<List<UserVo>> list(@RequestBody UserVo entityVo) {
        return userService.list(entityVo);
    }

    @RequestMapping("get/{id}")
    public Result<UserVo> get(@PathVariable("id") Integer id) {
        return userService.get(id);
    }
}

 

  服務消費者

  消費者maven引入jarjson

        <!-- feign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

 

  配置文件

  對日期的解析,消費者要跟提供者一致,否則會報json解析錯誤

#超時時間
feign.httpclient.connection-timeout=30000

#mvc接收參數時對日期進行格式化
spring.mvc.date-format=yyyy-MM-dd HH:mm:ss
#jackson對響應回去的日期參數進行格式化
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8

 

  服務調用

  一、springdatejpa 應用名稱,是服務提供者在eureka註冊的名字,Feign會從註冊中心獲取實例

  二、若是不想啓動eureka服務,直連本地開發:@FeignClient(name = "springdatejpa", path = "/user/",url = "http://localhost:10086"),或者無eureka,調用第三方服務,關閉eureka客戶端      (eureka.client.enabled=false),url直接指定第三方服務地址,path指定路徑,接口的方法指定接口

  三、若是使用@RequestMapping,最好指定調用方式

  四、消費者的返回值必須與提供者的返回值一致,參數對象也要一致

  五、2019-05-21補充:如需進行容錯處理(服務提供者發生異常),則須要配置fallback,若是須要獲取到報錯信息,則要配置fallbackFactory<T>,例:

fallback = MyspringbootFeignFallback.class,fallbackFactory = MyspringbootFeignFallbackFactory.class
/**
 * 容錯處理(服務提供者發生異常,將會進入這裏)
 */
@Component
public class MyspringbootFeignFallback implements MyspringbootFeign {
    @Override
    public Result<UserVo> get(Integer id) {
        return Result.of(null,false,"糟糕,系統出現了點小情況,請稍後再試");
    }

    @Override
    public Result<List<UserVo>> list(UserVo entityVo) {
        return Result.of(null,false,"糟糕,系統出現了點小情況,請稍後再試");
    }
}
/**
 * 只打印異常,容錯處理仍交給MyspringbootFeignFallback
 */
@Component
public class MyspringbootFeignFallbackFactory implements FallbackFactory<MyspringbootFeign> {
    private final MyspringbootFeignFallback myspringbootFeignFallback;

    public MyspringbootFeignFallbackFactory(MyspringbootFeignFallback myspringbootFeignFallback) {
        this.myspringbootFeignFallback = myspringbootFeignFallback;
    }

    @Override
    public MyspringbootFeign create(Throwable cause) {
        cause.printStackTrace();
        return myspringbootFeignFallback;
    }
}

 

  Feign接口

  更多@FeignClient註解參數配置,請參閱官方文檔

@FeignClient(name = "springdatejpa", path = "/user/")
public interface MyspringbootFeign {

    @RequestMapping(value = "get/{id}")
    Result<UserVo> get(@PathVariable("id") Integer id);

    @RequestMapping(value = "list", method = RequestMethod.GET)
    Result<List<UserVo>> list(@RequestBody UserVo entityVo);
}

   Controller層

    /**
     * feign調用
     */
    @GetMapping("feign/get/{id}")
    Result<UserVo> get(@PathVariable("id") Integer id){
        return myspringbootFeign.get(id);
    }


    /**
     * feign調用
     */
    @GetMapping("feign/list")
    Result<List<UserVo>> list(UserVo userVo){
        return myspringbootFeign.list(userVo);
    }

  啓動類

  啓動類加入註解:@EnableFeignClients

@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class MyspringbootApplication{

    public static void main(String[] args) {
        SpringApplication.run(MyspringbootApplication.class, args);
    }

}

 

  效果

  成功註冊兩個服務

   

  成功調用

 

  

  報錯記錄

  一、啓動時報了個SQL錯誤

  解決:配置文件鏈接數據時指定serverTimezone=GMT%2B8

 

  二、當我將以前搭好的一個springboot-springdata-jpa整合項目在eureka註冊時出現了一個報錯

  而後在網上查了下說是由於springboot版本問題(請戳:http://www.cnblogs.com/hbbbs/articles/8444013.html),以前這個項目用的是2.0.1.RELEASE,如今要在eureka註冊,pom引入了就出現了上面的報錯

        <!-- eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!-- actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RC1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

  解決:升級了springboot版本,2.1.0,項目正常啓動

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <!--<version>2.0.1.RELEASE</version>-->
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

 

  補充

  2019-10-17補充:Feign設置header請求頭

  方法1,mapping的headers屬性,單一設置

@FeignClient(name = "svc", path = "/modules/user/", url = "${feign.url}")
public interface UserFeign extends BaseFeign<UserVo> {
    @PostMapping(value = "xxx",headers = {"Cookie", "JSESSIONID=xxx"})
     ResultModel<List<UserVo>> xxx(UserVo entity);
}

 

  方法2,自定義FeignInterceptor,全局設置

/**
 * feign請求設置header參數
 * 這裏好比瀏覽器調用A服務,A服務Feign調用B服務,爲了傳遞一致的sessionId
 */
@Component
public class FeignInterceptor implements RequestInterceptor{

    public void apply(RequestTemplate requestTemplate){
        String sessionId = RequestContextHolder.currentRequestAttributes().getSessionId();
        requestTemplate.header("Cookie", "JSESSIONID="+sessionId);
    }
} 

  這樣就能夠設置cookie,傳遞token等自定義值

 

  常見場景1

  一般咱們一個服務web層、svc層、dao層,但有時候也會將拆分紅兩個服務:

  web服務提供靜態資源、頁面以及controller控制器控制跳轉,數據經過java調用svc服務獲取;

  svc服務,進行操做數據庫以及業務邏輯處理,同時提供接口給web服務調用;

 

  特殊狀況下咱們想svc服務的接口也作登陸校驗,全部接口(除了登陸請求接口)都有作登陸校驗判斷,未登陸的無權訪問,這時候就須要作sessionId傳遞,將web服務的sessionId經過Feign調用時傳遞到svc服務

  

  web服務

  注:登陸成功後用sessionId做爲key,登陸用戶的id做爲value,保存到redis緩存中

 

  登陸攔截器

/**
 * web登陸攔截器
 */
@Component
public class LoginFilter implements Filter {

    @Autowired
    private StringRedisTemplate template;

    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String requestURI = request.getRequestURI();

        //除了訪問首頁、登陸頁面、登陸請求,其餘的都要查看Redis緩存
        String sessionId = request.getSession().getId();
        String redis = template.opsForValue().get(sessionId);
        if (!
                //無需登陸便可訪問的接口
                (requestURI.contains("/index/") || requestURI.contains("/login/index") || requestURI.contains("/login/login")
                //靜態資源
                || requestURI.contains(".js") || requestURI.contains(".css") || requestURI.contains(".json")
                || requestURI.contains(".ico")|| requestURI.contains(".png")|| requestURI.contains(".jpg"))
                && StringUtils.isEmpty(redis)) {//重定向登陸頁面
            response.sendRedirect("/login/index?url=" + requestURI);
        } else {
            //正常處理請求
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    @Override
    public void destroy() {
    }
}

 

  自定義FeignInterceptor

/**
 * feign請求設置header參數
 * 這裏好比瀏覽器調用A服務,A服務Feign調用B服務,爲了傳遞一致的sessionId
 */
@Component
public class FeignInterceptor implements RequestInterceptor{

    public void apply(RequestTemplate requestTemplate){
        String sessionId = RequestContextHolder.currentRequestAttributes().getSessionId();
        requestTemplate.header("Cookie", "JSESSIONID="+sessionId);
    }
}

 

  svc服務

/**
 * svc登陸攔截器
 */
@Component
public class LoginFilter implements Filter {

    @Autowired
    private StringRedisTemplate template;

    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String requestURI = request.getRequestURI();

        //service服務,查看Redis緩存,登陸後才容許訪問(除了checkByAccountNameAndPassword)
        String sessionId = request.getRequestedSessionId();
        if (!(requestURI.contains("/modules/user/checkByAccountNameAndPassword")) && StringUtils.isEmpty(template.opsForValue().get(sessionId))) {
            //提示無權訪問
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            PrintWriter out = response.getWriter();
            out.print("對不起,你無權訪問!");
            out.flush();
            out.close();
        } else {
            //正常處理請求
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    @Override
    public void destroy() {
    }
}

 

  七天免登錄

  會話期的sessionId,關閉瀏覽器後就失效了,因此就會退出瀏覽器後就須要從新登錄,有些狀況咱們並不想這樣,咱們想實現七天免登錄,這時候就須要自定義token,而且存放在cookie

  登錄攔截器

/**
 * web登陸攔截器
 */
@Component
public class LoginFilter implements Filter {
    /** 靜態資源 爲防止緩存,加上時間戳標誌 */
    private static final String STATIC_TAIL = "_time_=";

    @Autowired
    private StringRedisTemplate template;

    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String requestURI = request.getRequestURI();

        //無需登陸便可訪問的接口,登錄頁面、登錄請求
        if(requestURI.contains("/login/index") || requestURI.contains("/login/login")){
            //正常處理請求
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        //靜態資源
        if(requestURI.contains(".js") || requestURI.contains(".css") || requestURI.contains(".json")
                || requestURI.contains(".woff2") || requestURI.contains(".ttf")|| requestURI.contains(".ico")
                || requestURI.contains(".png")|| requestURI.contains(".jpg")|| requestURI.contains(".gif")){

            //檢查是否有防緩存時間戳
            String queryStr = request.getQueryString();
            if(StringUtils.isEmpty(queryStr) || !queryStr.trim().contains(STATIC_TAIL)){
                response.sendRedirect(requestURI + "?" + STATIC_TAIL + System.currentTimeMillis());
                return;
            }

            //正常處理請求
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        //剩下的要檢查redis緩存
        String token = null;
        for (Cookie cookie : request.getCookies()) {
            if("TOKEN".equals(cookie.getName())){
                token = cookie.getValue();
            }
        }
        String redis = template.opsForValue().get(token);
        if(StringUtils.isEmpty(redis)){
            //重定向登陸頁面
            response.sendRedirect("/login/index?url=" + requestURI);
            return;
        }

        //若是都不符合,正常處理請求
        filterChain.doFilter(servletRequest, servletResponse);

    }

    @Override
    public void destroy() {
    }
}

 

  登錄成功,設置cookie

    public ResultModel<UserVo> login(UserVo userVo) {

此處省略查詢操做...

if (true) { HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getResponse(); //設置Redis,有效時長:7天 String uuid = UUID.randomUUID().toString(); template.opsForValue().set(uuid, userVo.getAccountNo()); template.expire(uuid, 7 * 60 * 60, TimeUnit.SECONDS); //設置cookie,有效時長:7天 Cookie cookie = new Cookie("TOKEN", uuid); cookie.setPath("/"); cookie.setMaxAge(7 * 24 * 60 * 60); response.addCookie(cookie); return ResultModel.of(userVo, true, "登陸成功"); } return ResultModel.of(null, false, "用戶名或密碼錯誤"); }

 

  推出登錄,銷燬cookie

    public ResultModel<UserVo> logout() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getResponse();
        String token = "";
        for (Cookie cookie : request.getCookies()) {
            if("TOKEN".equals(cookie.getName())){
                token = cookie.getValue();
                cookie.setValue(null);
                cookie.setPath("/");
                cookie.setMaxAge(0);// 當即銷燬cookie
                response.addCookie(cookie);
                break;
            }
        }
        template.delete(token);
        return ResultModel.of(null, true, "操做成功!");
    }

 

  代碼開源

  代碼已經開源、託管到個人GitHub、碼雲:

  GitHub:https://github.com/huanzi-qch/springCloud

  碼雲:https://gitee.com/huanzi-qch/springCloud

相關文章
相關標籤/搜索