Feign是声明式Web Service客户端,它让微服务之间的调用变得更简单,类似controller调用service。SpringCloud集成了Ribbon和Eureka,可以使用Feigin提供负载均衡的http客户端
实现服务间通信的两种方式
1、RestTeampalte + Ribbon
2、OpenFeign
1、创建springcloud-consumer-fdept-openfeign消费者模块
2、添加依赖
<!--实体类+web,主要根前端打交道-->
<dependencies>
<dependency>
<groupId>org.study</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId> spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
3、编写application.yaml配置文件
server:
port: 80
#EurekaClient配置
eureka:
client:
register-with-eureka: false #不像eureka中注册自己
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
4、编写ConfigBean
package com.study.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ConfigBean { //相当于spring里面的applicationContext.xml
//配置负载均衡实现RestTemplate
@Bean
@LoadBalanced //Ribbon负载均衡
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
5、给springcloud-api模块添加openfeign依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId> spring-cloud-starter-openfeign</artifactId>
</dependency>
6、在springcloud-api模块添加接口DeptClientService
import com.study.springcloud.pojo.Dept;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
@Service
@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT") //服务名称
public interface DeptClientService {
@GetMapping("/dept/get/{id}")
Dept queryById(@PathVariable("id") Long id);
@GetMapping("/dept/list")
List<Dept> queryAll();
@PostMapping("/dept/add")
boolean addDept(Dept dept);
}
7、在springcloud-consumer-dept-openfeign添加DeptConsumerController类,调用实现API模块的接口
package com.study.springcloud.controller;
import com.study.springcloud.pojo.Dept;
import com.study.springcloud.service.DeptClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
public class DeptConsumerController {
//消费者不应该有service层,怎么才能拿到service层?
//RestTemplate,注册到spring中
@Autowired
private RestTemplate restTemplate;//提供多种便捷访问远程http服务器的方法,简单的Restful服务模板
@Autowired
private DeptClientService service = null;
@RequestMapping("consumer/dept/add")
private boolean add(Dept dept){
return this.service.addDept(dept);
}
@RequestMapping("/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Long id){
return this.service.queryById(id);
}
@RequestMapping("/consumer/dept/list")
public List<Dept> list(){
return this.service.queryAll();
}
}
8、在springcloud-consumer-dept-openfeign主启动类中添加@EnableFeignClients注解
项目结构
9、启动7001(eureka注册中心)、8001(服务提供者)、8002(服务提供者)、openfign(消费者)测试
SpringCloud(八)——openFeign服务器间的调用
原文:https://www.cnblogs.com/luoxiao1104/p/14998827.html