微服务:Feign服务调用

feigin主要解决服务与服务直间的调用

1.3.1搭建一个主要管理链接的service-client父模块

引入依赖

<!-- 服务调用feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<scope>provided </scope>
</dependency>
</dependencies>

在搭建需要被调用的子模块

搭建service-xxx-client模块

在子模块中添加接口类

**
 * 数据字典API接口
 */
@FeignClient("service-cmn")//要引入服务的名称
public interface DictFeignClient {


//底下是要引入服务的方法


/**
     * 获取数据字典名称
     * @param parentDictCode
* @param value
* @return
*/
@GetMapping(value = "/admin/cmn/dict/getName/{parentDictCode}/{value}")
    String getName(@PathVariable("parentDictCode") String parentDictCode, @PathVariable("value") String value);

/**
     * 获取数据字典名称
     * @param value
* @return
*/
@GetMapping(value = "/admin/cmn/dict/getName/{value}")
    String getName(@PathVariable("value") String value);
}

在要调用的服务pom.xml加入依赖

<!-- 服务调用feign -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
    <groupId>com.atguigu.yygh</groupId>
    <artifactId>service-cmn-client</artifactId>
    <version>1.0</version>
</dependency>

在serviceImpl中引入

@Autowired
private DictFeignClient dictFeignClient;

引入某一个服务的接口

在要调用的启动类上加上,wei

@EnableFeignClients(basePackages = "com.atguigu")//指定包名

经验分享 程序员 微信小程序 职场和发展