
SpingCloud之feign框架调用
发布日期:2021-05-09 04:58:04
浏览次数:18
分类:博客文章
本文共 10993 字,大约阅读时间需要 36 分钟。
1.生产者(没有什么特殊性)
pom.xml
4.0.0 com.example cloud-provider 0.0.1-SNAPSHOT jar cloud-provider Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.6.RELEASE UTF-8 UTF-8 1.8 Finchley.SR2 org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 org.springframework.cloud spring-cloud-starter-zookeeper-discovery org.springframework.boot spring-boot-devtools runtime mysql mysql-connector-java org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin true
application.yml
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///testdb?useSSL=true username: root password: 123 cloud: zookeeper: connect-string: 192.168.3.201:2181 application: name: providermybatis: configuration: map-underscore-to-camel-case: true mapper-locations: classpath:mapper/*Mapper.xmllogging: level: com.example.cloudprovider.mapper: debug
实体类
package com.example.cloudprovider.domain;import lombok.Getter;import lombok.Setter;import java.util.Date;@Setter@Getterpublic class UserInfo { private Integer userId; private String userName; private int userAge; private Date userBirth;}
Mapper
package com.example.cloudprovider.mapper;import com.example.cloudprovider.domain.UserInfo;public interface UserInfoMapper { UserInfo getUser(Integer userId);}
Mapper.xml
Service接口
package com.example.cloudprovider.service;import com.example.cloudprovider.domain.UserInfo;public interface UserService { UserInfo getUser(Integer userId);}
Service实现
package com.example.cloudprovider.service.impl;import com.example.cloudprovider.domain.UserInfo;import com.example.cloudprovider.mapper.UserInfoMapper;import com.example.cloudprovider.service.UserService;import org.springframework.stereotype.Service;import javax.annotation.Resource;@Servicepublic class UserServiceImpl implements UserService { @Resource private UserInfoMapper userMapper; @Override public UserInfo getUser(Integer userId) { return userMapper.getUser(userId); }}
Controller
package com.example.cloudprovider.controller;import com.example.cloudprovider.domain.UserInfo;import com.example.cloudprovider.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class UserInfoController { @Autowired private UserService userService; @GetMapping("user/{id}") public UserInfo getUser(@PathVariable("id") Integer userId) { return userService.getUser(userId); }}
服务发布类
package com.example.cloudprovider;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@MapperScan("com.example.cloudprovider.mapper") //扫描Mapper类 注入到Spring容器中public class CloudProviderApplication { public static void main(String[] args) { SpringApplication.run(CloudProviderApplication.class, args); }}
2.消费者(引入feign框架)
pom.xml
4.0.0 com.example cloud-consumer 0.0.1-SNAPSHOT jar cloud-consumer Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.6.RELEASE UTF-8 UTF-8 1.8 Finchley.SR2 org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.cloud spring-cloud-starter-openfeign org.springframework.cloud spring-cloud-starter-zookeeper-discovery org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin true
application.yml
spring: cloud: zookeeper: connect-string: 192.168.3.201:2181 discovery: register: false #不会注册到zk中provider: # 服务名称 ribbon: # 负载均衡实现依靠ribbon # 负载策略 NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #随机策略 其他策略百度
实体类
package com.example.cloudconsumer.vo;import lombok.Getter;import lombok.Setter;import java.util.Date;@Getter@Setterpublic class UserVO { private Integer userId; private String userName; private Date userBirth;}
RestTemplate配置类
package com.example.cloudconsumer.config;import org.springframework.boot.SpringBootConfiguration;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@SpringBootConfigurationpublic class RestTemplateConfiguration { @Bean @LoadBalanced // Ribbon 负载均衡 public RestTemplate restTemplate() { return new RestTemplate(); }}
Controller
package com.example.cloudconsumer.controller;import com.example.cloudconsumer.vo.UserVO;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestControllerpublic class UserWarpController { @Autowired private RestTemplate restTemplate; // 调用只有一个或者多个服务实例API的情况下 @GetMapping("warp/user/{userId}") public UserVO getUserData(@PathVariable("userId") Integer userId) { return restTemplate.getForObject("http://provider/user/"+userId, UserVO.class); }}
使用框架的Controller
package com.example.cloudconsumer.controller;import com.example.cloudconsumer.client.UserClient;import com.example.cloudconsumer.vo.UserVO;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestControllerpublic class UserFeignController { @Resource private UserClient userClient; // 调用只有一个或者多个服务实例API的情况下 @GetMapping("feign/user/{userId}") public UserVO getUserData(@PathVariable("userId") Integer userId) { return userClient.getUserData(userId); }}
feign调用客户端
package com.example.cloudconsumer.client;import com.example.cloudconsumer.vo.UserVO;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;/** * @FeignClient(name = "provider") * name为调用服务端的spring.application.name的值 */@FeignClient(name = "provider")public interface UserClient { @GetMapping("user/{id}") public UserVO getUserData(@PathVariable("id") Integer userId);}
服务启动类:
package com.example.cloudconsumer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication@EnableDiscoveryClient //可以发现ZK的服务@EnableFeignClients //可以发现feign的服务public class CloudConsumerApplication { public static void main(String[] args) { SpringApplication.run(CloudConsumerApplication.class, args); }}
总结起来,feign框架可以理解成路由,对url进行再次包装后供给客户端调用,可以在这个路由上进行一系列限制操作,增强安全性。
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月10日 23时33分41秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【SOLVED】Linux使用sudo到出现输入密码提示延迟时间长
2019-03-06
项目引入非配置的文件,打成war包后测试报错的可能原因
2019-03-06
Git学习笔记
2019-03-06
不需要爬虫也能轻松获取 unsplash 上的图片
2019-03-06
elementUi源码解析(1)--项目结构篇
2019-03-06
Nmap扫描工具介绍
2019-03-06
算法笔记:递归、动态规划
2019-03-06
常用Windows 快捷键
2019-03-06
linux命令-压缩与打包
2019-03-06
ORACLE 11g 生产中高水位线(HWM)处理
2019-03-06
weblogic 服务器部署SSL证书
2019-03-06
oracle 11g not in 与not exists 那个高效?
2019-03-06
Linux 安装Redis 5.0(以及参数调优)
2019-03-06
html5 Game开发系列文章之 零[开篇]
2019-03-06
Golang Web入门(4):如何设计API
2019-03-06