springboot和springCloud版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<docker.image.prefix>gkl</docker.image.prefix>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
启动类加上注解
@EnableEurekaClient
@EnableFeignClients
调用在eureka注册的服务spirng-boot-rabbitmq-sender中的"/user/list"地址
package com.gkl1120.eurekaclient1.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @author ctx_gao kailong
* @date 2020/4/8 16:11
*/
@FeignClient("spirng-boot-rabbitmq-sender")
public interface FeignProxy {
@GetMapping("/user/list")
public Object getList();
}
带上token,这里就先写死了。创建配置类
package com.gkl1120.eurekaclient1.config;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
/**
* @author ctx_gao kailong
* @date 2020/4/8 16:18
*/
@Configuration
public class FeignConfig implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
requestTemplate.header("Authorization","auth eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJna2wiLCJzdWIiOiJna2wiLCJpYXQiOjE1ODYzMzE4MjksInJvbGVzIjoiYWRtaW4ifQ.b6s1gmj8flw0KOmwc5Xhcxi6vGlYw0JQh3FFF2uqEXw");
}
}
配置文件
server.port=1120
#指定服务名
spring.application.name=eureka-client1
#Eureka客户端与Eureka服务端进行交互的地址
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:6868/eureka/
eureka.client.instance.prefer‐ip‐address=true
#设置连接或者读取3s就会抛异常,这里的spirng-boot-rabbitmq-sender为需要调用的服务名
feign.client.config.spirng-boot-rabbitmq-sender.connect-timeout=3000
feign.client.config.spirng-boot-rabbitmq-sender.read-timeout=3000
controller调用
package com.gkl1120.eurekaclient1.controller;
import com.gkl1120.eurekaclient1.feign.FeignProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author ctx_gao kailong
* @date 2020/4/8 16:14
*/
@RestController
public class FeignControllerTest {
@Autowired
private FeignProxy feignProxy;
@GetMapping("666")
public Object getList(){
return feignProxy.getList();
}
}