springcloud学习之feign实现声名式Rest调用
在之前我们已经实现了eureka注册中心的服务,eureka注册中心的服务
本章节在这基础上实现,消费者通过feign调用提供者
版本介绍
springboot 2.1.6
springcloud Greenwich.SR1
文章目录
1 服务提供者的改造
改造提供者添加一个rest接口
1.1添加controller接口
package com.cloud.provider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SpringCloudController {
@Autowired
private DiscoveryClient discoveryClient;//服务注册对象
@Value("${server.port}")
private String port;
@GetMapping("client")
public String client() {
String services = "Services: " + discoveryClient.getServices() + " port :" + port;
System.out.println(services);
return services;
}
}
1.2 测试
2 构建消费者
重新创建一个服务作为消费者
pom
核心pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
整体pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cloud</groupId>
<artifactId>consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2 启动类配置
两个注解
@EnableEurekaClient //表示服务为eureka-client的服务
@EnableFeignClients //开启 feign组件
2.3 yaml
spring:
application:
name: eureka-consumer
server:
port: 9102
eureka:
client:
serviceUrl:
defaultZone: http://root:root@localhost:8761/eureka/
2.4 使用feign
创建一个接口
ConsumerService
package com.cloud.consumer;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(value = "eureka-client")
public interface ConsumerService {
@GetMapping("client")
String consumer();
}
@FeignClient 这个注解 表示要调用的提供者服务名称
controller
package com.cloud.consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
@Autowired
ConsumerService consumerService;
@GetMapping("consumer")
public String consumer() {
return consumerService.consumer();
}
}
到此消费者配置完成
2.5 测试
启动服务在eureka注册中心页面可以看到
http://localhost:9102/consumer
到此可以看到实现了调用
3 简单负载均衡
在启动一个服务作为提供者 只需要修改yaml文件配置新端口
spring:
application:
name: eureka-client
server:
port: 9003
eureka:
client:
serviceUrl:
defaultZone: http://root:root@localhost:8761/eureka/
3.1 测试
看eureka控制台
http://localhost:9102/consumer
显示结果会自动在两个服务提供者之间调用,切换
本文章只做了最简单的 消费者与提供者之间的调用
4 源码
github : https://github.com/imqw/spring-cloud