SpringCloud2模块系列:Feign(服务间调用)

1 Feign

1.0 功能

服务间接口调用.

1.2 调用过程

  • 场景
    服务A调用服务B的接口.
  • 实现过程
    在服务A中使用Feign配置服务B的应用及路径属性.
  • 一句话
    在发起调用的服务中,配置被调用服务的信息.

1.3 流程图

图1 服务间调用示意图

2 Usage

2.1 服务A-工单服务

2.1.0 启动类

启动类中添加注解@EnableFeignClients,打开Feign,服务间进行Http调用.

package com.personal.microspersonalworkorder;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author xdq
 * @description Service Start.
 * @date 2020/6/18
 */
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class MicrospersonalWorkorderApplication {
	static Logger logger = LoggerFactory.getLogger(MicrospersonalWorkorderApplication.class);
	
	public static void main(String[] args) {
		SpringApplication.run(MicrospersonalWorkorderApplication.class, args);
		logger.info("WORKORDER启动");
	}
}

2.1.2 接口类

在发起调用的服务A中,配置被调用服务的应用和URI,发起调用的服务接口如下:
参数属性如下表:
@FeignClient(value,path)

序号 属性 描述
1 value 被调用的服务B应用名称,即配置文件中(*.yml)的application:name:micros-user
2 path 被调用的服务B的servlet路径,即配置文件中(*.yml)的servlet: context-path: /user,如没有配置,则省略参数path

@RequestMapping(value)
value为服务B的完整URI.
方法中的参数前添加@RequestBody或@RequestParam,与被调用的服务B中接口完全一致.

package com.personal.microspersonalworkorder.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.Map;

/**
 * @author xdq
 * @description Call user service
 * @date 2020/6/18
 */
@FeignClient(value="micros-user",path="user")
public interface MicrospersonalUser {
    @RequestMapping("/api/page/user/query")
    Map queryFromUser(@RequestBody Map params);
}

2.1.3 服务类

package com.personal.microspersonalworkorder.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Map;
import com.personal.microspersonalworkorder.feign.MicrospersonalUser;

/**
 * @author xdq
 * @description Service funciton.
 * @date 2020/6/18
 */
@Service
@Transactional
public class UserInfosService {
    @Autowired
    private MicrospersonalUser microspersonalUser;

    public Map getUserInfos(Map params){
        return microspersonalUser.queryFromUser(params);
    }
}

2.2 服务B-人员信息服务

2.2.1 配置文件

配置信息摘要,调用服务B的其他服务需要进行配置的:

序号 属性 描述
1 application:name:micros-user 服务应用名称
2 servlet:context-path:/user 服务B全局路径
spring:
  devtools: 
    restart: 
      enabled: true
  profiles: dev
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/data_repository?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
  application: 
    name: micros-user

server:
  port: 8091
  servlet:
    session:
      timeout: PT10S
    context-path: /user
eureka: 
  client: 
    service-url: 
      defaultZone: http://localhost:8090/eureka/eureka

logging: 
  level: 
    root: INFO
  
pagehelper: 
  helperDialect: mysql 
  reasonable: true 
  supportMethodsArguments: true
  params: count=countSql

2.2.1 实体类

package com.personal.microspersonaluser;

import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author xdq
 * @description Service Start.
 * @date 2020/6/18
 */
@SpringBootApplication
@MapperScan("com.personal.microspersonaluser.mapper")
@EnableSwagger2 
@EnableCaching 
@EnableEurekaClient
@EnableFeignClients
public class MicrospersonalUserApplication extends SpringBootServletInitializer{
    private static Logger logger = LoggerFactory.getLogger(MicrospersonalUserApplication.class);
    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
        return application.sources(MicrospersonalUserApplication.class);
    }
    public static void main(String[] args){
        SpringApplication.run(MicrospersonalUserApplication.class, args);
        logger.info("USER服务启动");
    }
}

2.2.2 接口类

package com.personal.microspersonaluser.mapper;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.personal.microspersonaluser.po.UserInfos;

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

/**
 * @author xdq
 * @description Interface.
 * @date 2020/6/6
 */
public interface UserInfosMapper {
    public Integer addUser(UserInfos params);
    public Integer deleteUser(Integer id );
    public Integer editUser(UserInfos params);
    public List<UserInfos> queryUser(Map params);
    public UserInfos queryUserWithId(Integer id);
    public UserInfos loginByEmail(String email);
}

2.2.3 服务类

package com.personal.microspersonaluser.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.personal.microspersonaluser.mapper.UserInfosMapper;
import com.personal.microspersonaluser.po.UserInfos;

/**
 * @author xdq
 * @description User service.
 * @date 2020/6/6
 */
@Service
@Transactional
public class UserInfosService {
    @Autowired
    private UserInfosMapper userInfosMapper;

    public Integer addUser(UserInfos params){
        return userInfosMapper.addUser(params);
    }
    public Integer deleteUser(Integer id){
        return userInfosMapper.deleteUser(id);
    }
    public Integer editUser(UserInfos params){
        return userInfosMapper.editUser(params);
    }
    public List queryUser(Map params){
        return userInfosMapper.queryUser(params);
    }

    public UserInfos queryUserWithId(Integer id){
        return userInfosMapper.queryUserWithId(id);
    }
    public UserInfos loginByEmail(String email){
        return userInfosMapper.loginByEmail(email);
    }
}

【参考文献】
[1]https://www.cnblogs.com/huangjuncong/p/9053576.html
[2]https://blog.csdn.net/qq_42815754/article/details/93341709
[3]https://blog.csdn.net/qq_29235677/article/details/91994857#feign%E6%96%B9%E5%BC%8F%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E9%97%B4%E7%9A%84%E8%B0%83%E7%94%A8
[4]https://www.cnblogs.com/a8457013/p/8290119.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章