4.springcloud微服务架构搭建 之 《springboot集成openFeign》

 1.springcloud微服务架构搭建 之 《springboot自动装配Redis》

2.springcloud微服务架构搭建 之 《springboot集成nacos注册中心》

3.springcloud微服务架构搭建 之 《springboot自动装配ribbon》

本文结合上一篇文章 ,因为feign接口调用其实是通过ribbon来实现的负载,所以有问题的话结合上一篇文章一起看。

目录

1.配置feignconfig

2.自动装配feignConfig,spring.factories

3.启动类配置FeignClient

4.开始编写测demo

        4.1 新建一个feignclient

        4.2 实现接口服务类

        4.3 接口调用


1.配置feignconfig

package lilock.cn.common.ribbon.config;

import feign.Retryer;
import lilock.cn.common.ribbon.properties.RestTemplateProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;

import java.util.concurrent.TimeUnit;

@EnableConfigurationProperties
public class FeignConfig {

    @Bean
    @Primary
    @ConditionalOnMissingBean
    public Retryer feignRetryer(RestTemplateProperties restTemplateProperties){
        /** 默认重试机制
         * period: 重试间隔时间
         * duration: 时间单位
         * maxAttempts: 最大重试次数
         */
        return new Retryer.Default(100L, TimeUnit.SECONDS.toMillis(1L), restTemplateProperties.getMaxAutoRetries());
    }
}

2.自动装配feignConfig,spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration = \
lilock.cn.common.ribbon.properties.RestTemplateProperties,\
lilock.cn.common.ribbon.config.RibbonConfig,\
lilock.cn.common.ribbon.config.FeignConfig

3.启动类配置FeignClient

注意:@EnableFeignClients(basePackages = {"lilock.cn"}) 这个feignClient包扫描必须加,否则会提示找不到client对应的bean

package lilock.cn.cms;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication(scanBasePackages = {"lilock.cn"})
@EnableDiscoveryClient
@EnableConfigurationProperties
@EnableFeignClients(basePackages = {"lilock.cn"}) //启用feign调用
public class CmsApplication {

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

4.开始编写测demo

        4.1 新建一个feignclient

@FeignClient 注解的value表示 远程服务调用的applicationName,一定要跟服务名一一对应,否则远程调用的时候,ribbon会找不到调用接口的地址

@FeignClient 注解的configuration 配置的FeignConfig.class,因为FeignConfig里面只配置了重试策略

package lilock.cn.cms.api;

import lilock.cn.common.ribbon.config.FeignConfig;
import lilock.cn.common.ribbon.constant.ApplicationServiceConstants;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;


@FeignClient(value = ApplicationServiceConstants.LILOCK_CMS_WEB,configuration = {FeignConfig.class})
public interface ApiCmsServiceFeignClient {

    /**
     * 获取字符串
     * @return
     */
    @GetMapping("/api/hello")
    String getHello();
}

        4.2 实现接口服务类

package lilock.cn.cms.controller.feign;

import lilock.cn.cms.api.ApiCmsServiceFeignClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CmsServiceFeignController implements ApiCmsServiceFeignClient {
   
    @Value("${server.port}")
    private Integer port;
    @Value("${spring.application.name}")
    private String service;

    @Override
    public String getHello(){
        return "feign ["+ service +"]点前服务端口号是:["+ port +"]返回内容 HELLO" ;
    }
}

        4.3 接口调用

package lilock.cn.user.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lilock.cn.cms.api.ApiCmsServiceFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;

@RestController
@RequestMapping(path = "/user")
@Api(value = "系统用户",tags = {"系统用户"})
@Slf4j
public class UserController {

   
    @Autowired
    private ApiCmsServiceFeignClient apiCmsServiceFeignClient;

    @GetMapping("/testFeign")
    public String testFeign(){
        String value = apiCmsServiceFeignClient.getHello();
        log.info("[testFeign] 获取当前请求结果:{}",value);
        return value;
    }
}

返回结果4.springcloud微服务架构搭建 之 《springboot集成openFeign》_第1张图片

你可能感兴趣的:(spring,boot,spring,cloud,架构,java)