【SpringCloud】FeignClient 接口简单调用(77)

8 篇文章 0 订阅
文章展示了如何在SpringBoot应用中配置POM依赖,包括SpringCloudEureka客户端、FeignClient以及相关服务发现和Web支持。接着,定义了一个FeignClient接口来调用名为test-register的服务,并在Controller中注入并使用这个接口。最后,启动类通过@EnableFeignClients注解启用Feign客户端,并配置了超时设置。
摘要由CSDN通过智能技术生成

1.pom.xml 文件

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
			polaris注册中心
<!--        <dependency>-->
<!--            <groupId>com.tencent.cloud</groupId>-->
<!--            <artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>-->
<!--        </dependency>-->
		这里是openfeign客户端
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

使用pom.xml文件

		<!-- 微服务需要的包,版本需要和规定版本一致 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.1.7.RELEASE</version>
        </dependency>
		
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.1.7.RELEASE</version>
        </dependency>
		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.1.7.RELEASE</version>
            <scope>test</scope>
        </dependency>
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.1.7.RELEASE</version>
        </dependency>

2.FeignClient

package com.kk.test.testprovider;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;


@FeignClient("test-register")/*application.yml 中的  spring.application.name*/
public interface RegisterFeignClient {
	/*此处的url就是需要调用另一个服务的url*/
    @GetMapping("/register/getCC")
    String getCC();
}

3.新的Controller 调用FeignClient接口

package com.kk.test.testprovider;

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;


@RestController
@RequestMapping("/register")
public class TestController {

    @Autowired
    private RegisterFeignClient registerFeignClient;

    @GetMapping("/getCC")
    public String getCC(){
        return registerFeignClient.getCC();
    }
}

4.启动类注册为FeignClient
备注:EnableFeignClients注解的属性字段basePacka根据需求添加;

package com.kk.test.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableFeignClients(basePackages = {
        "com.kk.test.itheima.service.fegin",
		"com.kk.test.platform.common.feign.services.camunda"})
public class TestProviderApplication  {
    public static void main(String[] args) {
        SpringApplication.run(TestProviderApplication .class, args);
    }
    
	@Bean
	public RestTemplate restTemplate() {
		//复杂构造函数的使用
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
		requestFactory.setConnectTimeout(300000);// 设置超时
		requestFactory.setReadTimeout(300000);
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.setRequestFactory(requestFactory);
		return restTemplate;
	}
	
	@Bean
	public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
		SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
		factory.setConnectTimeout(150000);
		factory.setReadTimeout(50000);
		return factory;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KevinDuc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值