springCloud---fegin(ribbon的一定的改进)

fegin整合Ribbon的负载均衡特性,在调用上更加的简洁易用

在上一张随笔的基础上使用fegin  https://www.cnblogs.com/Web-spring/p/12255137.html 更改的是服务的消费者 配置文件为做任何改动

添加了一个依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
pom.xml

添加了一个接口,接口随意,一般为服务提供者的name加。。。

@FeignClient(name = "hello")
public interface EurekaFeignClient {

    /**
     * 配置需要调用的微服务的接口
     * @RequestParam 一定要加上,如果为@RequestBody 会传递不过去参数,参数名一定要保持一致
     * @param msg
     * @return
     */
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String findById(@RequestParam String msg);
}
interface

调用服务的controller里面的代码更改为

    @Autowired
    private EurekaFeignClient eurekaFeignClient;


    @GetMapping("/hello")
    public String hello() {
        System.out.println("hello");
        return eurekaFeignClient.findById("hello");
    }
controller

程序的入口添加注解  @EnableFeignClients RestTeplate可以将之删除

@SpringBootApplication
@EnableFeignClients
public class EurekaClientApplication {


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

}
View Code

启动就可以使用了。默认就已经开启了负载均衡不用做任何修改

 fegin的一些其他配置

#feign:
#  compression:
#    request:
#      enabled: true #对请求的内容进行压缩
#      min-request-size: 2048 #设置触发压缩的大小下限
#      mime-types: text/xml, application/xml, application/json #设置压缩的数据类型
#    response:
#      enabled: true #对响应的内容进行压缩
##日志的相关内容  日志配置 NONE:不输出日志 BASIC:适用于生产环境追踪问题  HEADERS:在BASIC的基础上,记录请求和响应头信息 FULL 记录所有
## hello 为服务名
#  client:
#    config:
#      hello:
#        loggerLevel: FULL
yml

 

posted @ 2020-02-04 17:39  张含韵好可爱  阅读(363)  评论(0编辑  收藏  举报