spring-cloud-feign 简单环境搭建

Feign服务端

1.添加Maven依赖

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

2.启动类配置

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeignServerApplication {

    private static final Logger log = LoggerFactory.getLogger(FeignServerApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(FeignServerApplication.class, args);
        log.info("========== feign server start success =====");
    }
}

3.开发控制层

@RestController
@RequestMapping("hello")
public class HelloFeignService {

    @PostMapping("add")
    public String add(@RequestBody String body) {
        return body;
    }
}

Feign客户端

1.添加maven依赖

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

2.添加feign相关配置

feign:
  client:
    config:
      default:
        connectTimeout: 5000
        readTimeout: 5000
        loggerLevel: basic
  hystrix:
    enabled: true

# feign client 开始日志
logging:
  level:
    com.lmn.DemoClient: debug

3.启动类启动FeignClient

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeignClientApplication {

    private static final Logger LOGGER = LoggerFactory.getLogger(FeignClientApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(FeignClientApplication.class, args);
        LOGGER.info("======= start success ====");
    }
}

4.定义FeignClient接口

@FeignClient(name = "feign-server")
public interface DemoClient {

    @PostMapping("/hello/add")
    String add(String body);
}

5.调用定义的FeignClient接口

@RestController
public class ControllerTest {
    private static final Logger LOG = LoggerFactory.getLogger(ControllerTest.class);
    @Autowired
    private DemoClient demoClient;

    @GetMapping("/hello/add")
    void add(){
        String resposne = demoClient.add("test test");
        LOG.info("结果:" + resposne);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值