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

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;
    }
 }

到这

image-20220808132252483

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

 

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

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

 

openFeign参数传递

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

image-20220808133714121

 

练习:(MovieController改变前)

image-20220808133838530

改变后:

image-20220808134140085

接着

image-20220808134648114

停掉三台movie服务,eureka自我保护机制会出来,重新运行user-feign

image-20220808135511757

 

加上

image-20220808135729581

image-20220808135740860

 

第三种接收json数据

image-20220808140426513

image-20220808140754255

image-20220808140811388

因为要传的是json,get请求不行,所以我们要用post请求

请求修改一下,改完刷到运行的movie服务器

image-20220808141614452

image-20220808141701348

 

第四种 restful风格

resuful默认请求put,delete

但是springboot默认接收不到put,delete请求,所以我们要改配置文件

image-20220808142408292

image-20220808143108565

image-20220808143200381

注意:上边截图代码movieId,我写成movidId了,记得改一下就好了

image-20220808145859037

 

第五种 请求头传递参数

image-20220808150647803

@RequestHeader

image-20220808151027948

image-20220808151350966

image-20220808151523383

 

接下来转转码

image-20220808152000310

接着MovieController转码

收到了给他解解码

image-20220808160353009

 

image-20220808160812544

image-20220808160949869

解码成功

image-20220808161049074

 

总结:

注解参数形式
@RequestHeader(“param”) 如果有中文,要进行 URL 编码 放在请求的 Head 头中
@RequestParam(“param”) GET /POST请求,传多个参数,注解用多次
@PathVariable(“param”) 请求路径上
@RequestBody POST 请求的 JSON
不知道 Form 表单

 

image-20220808162014649

扩展,传多行参数

image-20220808164544033

1熔断器看第八章

posted @ 2022-08-08 13:10  为了她  阅读(25)  评论(0编辑  收藏  举报