spring cloud Feign使用

一、提供者controller配置

@RequestMapping ("/getUserNameByName")
public SystemUserEntity getUserNameByName(String userName) {
    return userService.getUserNameByName(userName);
}

二、消费者controller配置


@RestController
@RequestMapping ("/systemUserController")
public class SystemUserController {

    @Autowired
    private UserFeignHystrixClient userFeignHystrixClient;

    /**
     * 将Cookie添加进Session
     */
    private SystemUserEntity setSession(String userName) {
        SystemUserEntity user = this.userFeignHystrixClient.getUserNameByName(userName);
        Session session = SecurityUtils.getSubject().getSession();
        LoginUserInfoBean loginUserInfoBean = new LoginUserInfoBean();
        loginUserInfoBean.setUserName(user.getUserName());
        loginUserInfoBean.setRealName(user.getRealName());
        loginUserInfoBean.setEmail(user.getEmail());
        loginUserInfoBean.setLoginTime(new Date());
        session.setAttribute("userInfo", loginUserInfoBean);
        return user;
    }
}

三、UserFeignHystrixClient配置

package cn.pzh.system.customer.feign;

import cn.pzh.system.provider.dao.first.model.SystemUserEntity;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 使用@FeignClient注解的fallback属性,指定fallback类
 */
@FeignClient (name = "cloud-system-provider")
public interface UserFeignHystrixClient {

    @RequestMapping ("/getAll")
    public List<SystemUserEntity> getAll();

    @RequestMapping ("/registration")
    public Boolean registration(@RequestParam ("userEntity") SystemUserEntity userEntity)
            throws UnsupportedEncodingException, NoSuchAlgorithmException;

    @RequestMapping ("/loginCheck")
    public String loginCheck(@RequestParam ("userName") String userName, @RequestParam ("password") String password,
            @RequestParam ("rememberFlag") boolean rememberFlag)
            throws UnsupportedEncodingException, NoSuchAlgorithmException;

    @RequestMapping ("/getUserNameByName")
    public SystemUserEntity getUserNameByName(@RequestParam ("userName") String userName);
}

源码:https://github.com/pengzh13579/spring-common-cloud

猜你喜欢

转载自blog.csdn.net/xiaoshiyiqie/article/details/79708699