ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringCloud基础组件完结--第七章

2022-08-08 13:33:08  阅读:185  来源: 互联网

标签:feign 调用 -- SpringCloud MovieServiceFeign user public cloud 完结


SpringCloud基础组件完结(第六章用的是RestTemplate远程调用,现在讲的是OpenFeign远程调用)

第七章 SpringCloud-Hello案例开发-Feign-声明式调用

7.1 Feign声明式调用

准备工作:

copy 之前的cloud-consumer-user模块,在夫工程下粘贴-》粘贴完成后因为他不是maven工程,所以需要让他生成一下(注意生成前先把artifactId 改成你的项目名)

 <artifactId>cloud-consumer-user</artifactId>改为
 <artifactId>cloud-consumer-user-feign</artifactId>

 

接着右键user-feign的pom.xml -》Add as Maven Project让他构建maven工程

 

构建完成后接着把复制过来的pom.xml 里的ribbon依赖删掉,因为openfeign自带ribbon

 <!-- 引入ribbon实现远程调用和负载均衡功能 -->(删掉)
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
 </dependency>

 

然后把config配置类删掉

 @SpringBootConfiguration (删掉)
 public class MyConfig {
     @Bean
     @LoadBalanced //负载均衡
     public RestTemplate restTemplate(){
         return new RestTemplate();
    }
     @Bean //随机服务器调用
     public IRule rule(){
         return new RandomRule();
    }
 }

 

UserService 实现类中 删掉

 @Autowired
 private RestTemplate restTemplate;
 ​
 Movie movie = restTemplate.getForObject("http://CLOUD-PROVIDER-MOVIE/movie", Movie.class);
 ​
 先把 改为空
 map.put("moviename",null);

 

准备工作完成

OpenFeign更优雅的远程调用

7.2 引入eureka-Discovery、web、Feign模块

 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-openfeign</artifactId>
 </dependency>

导入完成需要

7.2.1 开启@EnableDiscoveryClient服务发现

user-feign中程序入口加上

 @SpringBootApplication
 @EnableDiscoveryClient //允许发现注册中心其他的客户端
 public class UserApplication {
     public static void main(String[] args) {
         System.out.println(SpringApplication.run(UserApplication.class,args));
    }
 }

 

7.2.2 提供个端口

 spring:
  application:
    name: cloud-consumer-user-feign
 ​
 server:
  port: 7000
 ​
 eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true #注册中心保存我的ip

 

7.2.3 创建个接口MovieServiceFeign

声明式的远程调用,因为都会在统一的接口中来管理远程调用的方法,所以叫做声明式远程调用 因为就单独的声明了一个接口

  1. user-feign下service新建feign实现接口MovieServiceFeign

 package com.wsk.user.service.feign;
 ​
 import org.springframework.cloud.openfeign.FeignClient;
 ​
 @FeignClient(value = "CLOUD-PROVIDER-MOVIE")
 public interface MovieServiceFeign {
 }
  1. 接着user-feign程序入口加上@EnableFeignClients注解(第三行)

 @SpringBootApplication
 @EnableDiscoveryClient //允许发现注册中心其他的客户端
 @EnableFeignClients //开启feign客户端功能
 public class UserApplication {
     public static void main(String[] args) {
         System.out.println(SpringApplication.run(UserApplication.class,args));
    }
 }
  1. 紧接着开始写接口代码(找MovieController代码)

 @GetMapping("/movie")
 public Movie getNewMovie() {
     System.out.println("port" + port);
     return movieService.getNewMovie();
 }

复制一下粘贴到MovieServiceFeign接口,并修改

 @FeignClient(value = "CLOUD-PROVIDER-MOVIE")
 public interface MovieServiceFeign {
     @GetMapping("/movie")
     public Movie getNewMovie();
 }
  1. 完成user-feign中 service层的操作(注入MovieServiceFeign,new一下,返回数据)

 @Service
 public class UserService {
     @Autowired
     UserDao userDao;
 ​
     public User getUserById(Integer id) {
         User user = userDao.getUser(id);
         return user;
    }
 ​
     @Autowired
     private MovieServiceFeign movieServiceFeign;
 ​
     public Map buyMovie(Integer id) {
         //1.根据id检索用户
         User user = userDao.getUser(id);
         //2.远程调用movie服务获取最新的电影信息
         //url指定注册中心中的服务名称/请求路径
         Movie newMovie = movieServiceFeign.getNewMovie();
         //3.封装map集合,返回数据
         Map map = new HashMap<>();
         map.put("username",user.getUserName());
         map.put("moviename",newMovie.getMovieName());
         return map;
    }
 }

到这,访问 http://localhost:7000/buyMovie?id=1

跟第六章显示的一样{"username":"张三","moviename":"战狼"}

OpenFeign内置负载均衡,默认是轮询处理

 

openFeign调用传参数总结:(重要)

https://blog.csdn.net/x123453316/article/details/108879921

 

openFeign参数传递

OpenFeign 传递参数,一定要绑定参数名,即有参数要加上 @RequestParam 注解,如果通过 Header (请求头)来传递参数,一定要中文转码,测试 provider 服务中的接口

 

标签:feign,调用,--,SpringCloud,MovieServiceFeign,user,public,cloud,完结
来源: https://www.cnblogs.com/wangshikang/p/16561476.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有