Feign使用

Feign使用

↓↓
↓↓
↓↓
正文

1.配置服务:
       1.1 服务注册中心.yml配置:
server:
  port: 10086	#服务端口
#服务名称
spring:
  application:
    name: zhopush-registry
#单个eureka的情况
eureka:
  client:
    fetch-registry: false #不用拉取服务
    register-with-eureka: false #不用在eureka中注册自己
    service-url:
      #defaultZone: http://127.0.0.1:10086/eureka
      defaultZone: http://127.0.0.1:${server.port}/eureka
    server:
      enable-self-preservation: false # 关闭自我保护 
       1.2 服务提供方.properties配置:

server.port=8081		#服务端口
spring.application.name=mainServer		#服务名称

eureka.client.service-url.defaultZone=http://127.0.0.1:10086/eureka		#Eureka地址
eureka.instance.lease-renewal-interval-in-seconds=5
eureka.instance.lease-expiration-duration-in-seconds=10
eureka.instance.prefer-ip-address=true
eureka.instance.ip-address= 192.168.1.50			#服务启动地址
eureka.instance.instance-id= ${spring.application.name}:${server.port}

       1.3 服务调用方.properties配置:

server.port=8088		#服务端口
spring.application.name=adminServer		#服务名称

eureka.client.service-url.defaultZone=http://127.0.0.1:10086/eureka		#Eureka地址
eureka.instance.lease-renewal-interval-in-seconds=5
eureka.instance.lease-expiration-duration-in-seconds=10
eureka.instance.prefer-ip-address=true
eureka.instance.ip-address= 192.168.1.50			#服务启动地址
eureka.instance.instance-id= ${spring.application.name}:${server.port}
       1.4 服务添加依赖:
		<dependency>
            <!-- eureka客户端 -->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
2.服务调用方配置:
       2.1 添加依赖:
		<dependency>
            <!-- eureka客户端 -->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
        	<!-- 引入Feign 方式1111111111111-->
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>
		<dependency>
            <!-- 引入Feign 方式2222222222222-->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>    
       2.2 启动类加上开启Feign注解:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //<<<<<<<<<<<<<开启feign
public class ConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }

}
       2.3 定义一个接口API:
//value=服务提供方的服务名称,configuration=Header参数
@FeignClient(value = "mainServer" ,configuration={FeignRequestInterceptor.class})
public interface MsgClient {

    @PostMapping("/httpMessage/sysNtf")
    JSONResult ssysNtf(@RequestParam("msg") String msg, @RequestParam("fromUserId") String fromUserId, @RequestParam("extra") String extra);
    @PostMapping("/httpMessage/groupWindowNtf")
    JSONResult gWindowNtf(@RequestParam("groupId") String groupId,@RequestParam("msg") String msg, @RequestParam("extra") String extra, @RequestParam("fromUserId") String fromUserId);
    @PostMapping("/httpMessage/privateWindowNtf")
    JSONResult pWindowNtf(@RequestParam("fromUserId") String fromUserId,@RequestParam("toUserIds") String toUserIds,@RequestParam("msg") String msg, @RequestParam("extra") String extra);

}

接口API参数传递规则---------------点击查看

       2.4 如果需要传递Header参数,编写FeignRequestInterceptor:
@Configuration
public class FeignRequestInterceptor implements RequestInterceptor {


    public FeignRequestInterceptor() {
    }

    public void apply(RequestTemplate requestTemplate) {
		//获取HttpServletRequest 
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        
        String appkey ="aaaaaaaaaaaaaaaa";
        String nonce = "345346";
        String timestamp = "14087145345000";     
  		String signature = "17938e487c41f52075bcb743534d96";         
      
        /*正式签名修改*/
        requestTemplate.header("Nonce", new String[]{nonce});
        requestTemplate.header("Timestamp", new String[]{timestamp});
        requestTemplate.header("Signature", new String[]{signature});
        requestTemplate.header("App-Key", new String[]{appkey});
    }
       2.5 接口调用
@Controller
public class BackstageMsgPushController {
    private final Log log = LogFactory.getLog(this.getClass());
    
    //注入接口实例
    @Autowired
    private MsgClient msgClient;

    @PostMapping(MODEL + "/test")
    @Function("admin.backstage.test")
    @ResponseBody
    public JSONResult test(@RequestParam(value = "app") String app,
                                           @RequestParam(value = "push") String push,
                                           @RequestParam(value = "UserIds", required = false) String UserIds,
                                           @RequestParam(value = "groupId", required = false) String groupId,
                                           @RequestParam(value = "pushMessage") String pushMessage,
                                           HttpServletRequest request) {

        JSONResult result = new JSONResult();
     
        // 要调用的接口方法
        result = msgClient.pWindowNtf(null, UserIds, pushMessage, null);
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值