本篇文章只介绍fegin 和 ribbon 的简单使用,不使用微服务。
maven依赖
复制代码
1
2
3
4
5
6
7
8
9<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>
启动类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16//@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);
)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21@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是调试微服务用的地址,现在的话是我请求的前缀地址。
复制代码
1
2
3
4
5
6
7@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的默认值
复制代码
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@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测试类
复制代码
1
2
3
4
5
6
7
8
9
10
11@RestController public class TestController { @Autowired private TestFegin testFegin; @RequestMapping("/getBanners") public Object getBanners(String type){ return testFegin.getBanners(type); } }
最后
以上就是拉长板栗最近收集整理的关于fegin 和 ribbon 的简单使用的全部内容,更多相关fegin内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复