• pig4cloud中feign使用案例


    准备调用的接口

     写好调用的接口以及错误处理类

    package com.sensor.sellCabinet.feign;
    
    import com.pig4cloud.pig.admin.api.dto.UserDTO;
    import com.pig4cloud.pig.admin.api.feign.factory.RemoteLogServiceFallbackFactory;
    import com.pig4cloud.pig.common.core.constant.SecurityConstants;
    import com.pig4cloud.pig.common.core.constant.ServiceNameConstants;
    import com.pig4cloud.pig.common.core.util.R;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.stereotype.Service;
    import org.springframework.web.bind.annotation.*;
    
    @Service
    @FeignClient(value = ServiceNameConstants.UMPS_SERVICE, fallbackFactory = AdminUserServiceFallbackFactory.class)
    public interface AdminUserService {
    
        @PostMapping(value = "/user/mqAdd",produces = "application/json; charset=UTF-8")
        R addUser(@RequestBody UserDTO userDto, @RequestHeader(SecurityConstants.FROM) String from);
    
    
    }
    package com.sensor.sellCabinet.feign;
    
    import com.pig4cloud.pig.admin.api.dto.UserDTO;
    import com.pig4cloud.pig.common.core.util.R;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Component;
    
    @Slf4j
    @Component
    public class AdminUserServiceFallbackImpl implements AdminUserService{
    
    
        @Override
        public R addUser(UserDTO userDto, String from) {
            log.error("调用mq账号同步 失败====");
            return null;
        }
    }
    package com.sensor.sellCabinet.feign;
    
    import feign.hystrix.FallbackFactory;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Component;
    
    @Slf4j
    @Component
    public class AdminUserServiceFallbackFactory implements FallbackFactory<AdminUserService> {
        @Override
        public AdminUserService create(Throwable throwable) {
            log.error(throwable.getLocalizedMessage());
            AdminUserServiceFallbackImpl service= new AdminUserServiceFallbackImpl();
            throwable.printStackTrace();
            return service;
        }
    }

    注入调用的地方

     将接口请求url设置放行  ,不然访问401、403啥的反正请求不到,反正提示没身份验证

     内部访问接口设置了一个标识  ,标识以外的请求拦截

    /*
     *  Copyright (c) 2019-2020, 冷冷 (wangiegie@gmail.com).
     *  <p>
     *  Licensed under the GNU Lesser General Public License 3.0 (the "License");
     *  you may not use this file except in compliance with the License.
     *  You may obtain a copy of the License at
     *  <p>
     * https://www.gnu.org/licenses/lgpl.html
     *  <p>
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package com.pig4cloud.pig.common.security.feign;
    
    import cn.hutool.core.collection.CollUtil;
    import com.pig4cloud.pig.common.core.constant.SecurityConstants;
    import feign.RequestTemplate;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.cloud.security.oauth2.client.AccessTokenContextRelay;
    import org.springframework.cloud.security.oauth2.client.feign.OAuth2FeignRequestInterceptor;
    import org.springframework.security.oauth2.client.OAuth2ClientContext;
    import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
    
    import java.util.Collection;
    
    /**
     * @author lengleng
     * @date 2019/2/1
     * 扩展OAuth2FeignRequestInterceptor
     */
    @Slf4j
    public class PigFeignClientInterceptor extends OAuth2FeignRequestInterceptor {
    	private final OAuth2ClientContext oAuth2ClientContext;
    	private final AccessTokenContextRelay accessTokenContextRelay;
    
    	/**
    	 * Default constructor which uses the provided OAuth2ClientContext and Bearer tokens
    	 * within Authorization header
    	 *
    	 * @param oAuth2ClientContext     provided context
    	 * @param resource                type of resource to be accessed
    	 * @param accessTokenContextRelay
    	 */
    	public PigFeignClientInterceptor(OAuth2ClientContext oAuth2ClientContext
    		, OAuth2ProtectedResourceDetails resource, AccessTokenContextRelay accessTokenContextRelay) {
    		super(oAuth2ClientContext, resource);
    		this.oAuth2ClientContext = oAuth2ClientContext;
    		this.accessTokenContextRelay = accessTokenContextRelay;
    	}
    
    
    	/**
    	 * Create a template with the header of provided name and extracted extract
    	 * 1. 如果使用 非web 请求,header 区别
    	 * 2. 根据authentication 还原请求token
    	 *
    	 * @param template
    	 */
    	@Override
    	public void apply(RequestTemplate template) {
    		Collection<String> fromHeader = template.headers().get(SecurityConstants.FROM);
    		if (CollUtil.isNotEmpty(fromHeader) && fromHeader.contains(SecurityConstants.FROM_IN)) {
    			return;
    		}
    
    		accessTokenContextRelay.copyToken();
    		if (oAuth2ClientContext != null
    			&& oAuth2ClientContext.getAccessToken() != null) {
    			super.apply(template);
    		}
    	}
    }
    

      

  • 相关阅读:
    常见的问题系列【什么是子网掩码?】
    K8s系列【KubeSphere多租户账号创建流程】
    K8s系列【卸载K8s】
    K8s系列【Linux多节点部署KubeSphere】
    K8s系列【KubeSphere部署完mysql8.0再部署mysql5.0导致mysql5.0启动失败的问题】
    K8s系列【安装nfs文件系统(为k8s提供动态创建pv的能力)】
    K8s系列【Linux单节点部署KubeSphere】
    K8s系列【资源不足:0/3 nodes are available: 1 Insufficient cpu, 2 node(s) had taint {noderole.kubernetes.io/master: }, that the pod didn't tolerate】
    K8s系列【KubeSphere部署Mysql】
    idea格式化快捷键无效
  • 原文地址:https://www.cnblogs.com/qq376324789/p/15079629.html
Copyright © 2020-2023  润新知