fegin 和 ribbon 的简单使用

本篇文章只介绍fegin 和 ribbon 的简单使用,不使用微服务。
maven依赖

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

启动类

//@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class CloundCusApplication {

    @Bean
//    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(CloundCusApplication.class, args);
    }

}

controller类,先使用ribbon,请求后台设置了拦截器,在请求的header中加入tuserphone,防止被拦截,(如果不需要在header中添加参数,可以直接使用restTemplate.getForObject()
public T getForObject(String url, Class responseType, Map<String, ?> uriVariables);

@RestController
public class CustomerController {

    @Autowired
    private RestTemplate restTemplate;
    
    @RequestMapping("/hello")
    public Object hello(){
//        return restTemplate.getForObject("http://clound-first/hello",String.class);
        String url = "http://192.168.31.10:9000/ZET/banner/getBanners";
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("type","LBhajN8+h+s=");
        HttpHeaders header = new HttpHeaders();
        // 需求需要传参为form-data格式  multipart/form-data
//        header.setContentType(MediaType.MULTIPART_FORM_DATA);
        header.set("tuserphone", "111212");
        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(map, header);
        return restTemplate.postForEntity(url,httpEntity,String.class);
    }
}

接下来使用fegin
fegin接口,如果有请求参数的话@RequestParam这个注解要加上,我之前没加就报404错误。@ZETFegin是我自定义的注解,主要是请求后台设置了拦截器,在请求的header中加入tuserphone,防止被拦截。@FeignClient注解的name不能重复定义,是微服务定义的服务名称,因为没有用微服务所以只要不重复就行,url是调试微服务用的地址,现在的话是我请求的前缀地址。

@FeignClient(name = "zet", url = "http://192.168.31.10:9000/ZET")
public interface TestFegin {

    @ZETFegin(value = "/banner/getBanners")
    String getBanners(@RequestParam(value = "type") String type);
}

@ZETFegin注解定义类,类似于@PostMapping注解,只改变了一下header的默认值

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping
public @interface ZETFegin {
    @AliasFor(
            annotation = RequestMapping.class
    )
    String name() default "";

    @AliasFor(
            annotation = RequestMapping.class
    )
    String[] value() default {};

    @AliasFor(
            annotation = RequestMapping.class
    )
    String[] path() default {};

    @AliasFor(
            annotation = RequestMapping.class
    )
    String[] params() default {};

    @AliasFor(
            annotation = RequestMapping.class
    )
    String[] headers() default {"tuserphone=111"};

    @AliasFor(
            annotation = RequestMapping.class
    )
    String[] consumes() default {};

    @AliasFor(
            annotation = RequestMapping.class
    )
    String[] produces() default {};
}

fegin的Controller测试类

@RestController
public class TestController {
    @Autowired
    private TestFegin testFegin;

    @RequestMapping("/getBanners")
    public Object getBanners(String type){
        return testFegin.getBanners(type);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值