当前位置: 首页 > news >正文

宁波搭建网站公网站优化的关键词

宁波搭建网站公,网站优化的关键词,网站 刷流量,要找人做公司网站应该怎么做系列文章目录 第一章 Java线程池技术应用 第二章 CountDownLatch和Semaphone的应用 第三章 Spring Cloud 简介 第四章 Spring Cloud Netflix 之 Eureka 第五章 Spring Cloud Netflix 之 Ribbon 第六章 Spring Cloud 之 OpenFeign 文章目录 系列文章目录前言1、OpenFeign的实现…

系列文章目录

第一章 Java线程池技术应用
第二章 CountDownLatch和Semaphone的应用
第三章 Spring Cloud 简介
第四章 Spring Cloud Netflix 之 Eureka
第五章 Spring Cloud Netflix 之 Ribbon
第六章 Spring Cloud 之 OpenFeign

在这里插入图片描述


文章目录

  • 系列文章目录
  • 前言
  • 1、OpenFeign的实现原理和过程
    • 1.1、OpenFeign支持Spring Cloud体系内的所有注解
    • 1.2、OpenFeign自动封装HTTP请求
    • 1.3、OpenFeign支持请求拦截器和响应拦截器
    • 1.4、在Spring项目中的过程
    • 1.5、远程调用
  • 2、常用注解
  • 3、实践
    • 3.1、修改pom.xml配置
    • 3.2、增加用户服务接口,添加FeignClient配置
    • 3.3、修改控制层UserConsumerController
    • 3.4、启动类增加OpenFeign配置
    • 3.5、远程调用超时设置
  • 4、总结


前言

OpenFeign 全称 Spring Cloud OpenFeign,它是 Spring 官方推出的一种声明式服务调用与负载均衡组件。我们可以像调用本地方法一样来调用远程服务,而完全感觉不到这是在进行远程调用。
在这里插入图片描述

1、OpenFeign的实现原理和过程

OpenFeign是一个声明式的Web服务客户端,它使得编写Web服务客户端变得更加简单。通过使用OpenFeign,开发者可以更加容易地创建与远程服务进行交互的客户端代码。
OpenFeign通过自动封装HTTP请求和简化远程调用代码的过程,使得开发者可以更加专注于业务逻辑的实现,提高了开发效率和代码可读性。

1.1、OpenFeign支持Spring Cloud体系内的所有注解

例如@GetMapping、@PostMapping、@PathVariable、@RequestParam等,同时也支持自定义注解。

1.2、OpenFeign自动封装HTTP请求

包括HTTP方法、请求URL、请求参数、请求头等,开发者无需手动编写HTTP请求代码。只需要定义一个接口,并通过注解指定接口的路径和方法,OpenFeign就会自动发送HTTP请求并解析响应数据。

1.3、OpenFeign支持请求拦截器和响应拦截器

开发者可以通过实现请求拦截器和响应拦截器来对HTTP请求和响应进行处理,例如添加认证信息、重试等。

1.4、在Spring项目中的过程

在Spring项目启动中,OpenFeign会发起一个主动扫包的过程。从指定目录下加载所有被@FeignClient修饰的接口,将这些接口转换为Bean,交给Spring来管理。这些接口会经过MVC Constract解析,将方法上的注解解析出来放到MethodMetadata中。每一个FeignClient接口会生成一个动态代理对象,指向包含方法的MethodHandler的HashMap。

1.5、远程调用

当服务A发起远程调用时,它会从动态代理proxy中找到一个MethodHandler的实例,生成request,包含请求的url。经过负载均衡算法找到一个服务的IP地址,拼接请求的URL。服务B处理服务A发起的远程调用,执行逻辑后,返回响应给A。

2、常用注解

使用 OpenFegin 进行远程服务调用时,常用注解如下表。

注解说明
@FeignClient该注解用于通知 OpenFeign 组件对 @RequestMapping 注解下的接口进行解析,并通过动态代理的方式产生实现类,实现负载均衡和服务调用。
@EnableFeignClients该注解用于开启 OpenFeign 功能,当 Spring Cloud 应用启动时,OpenFeign 会扫描标有 @FeignClient 注解的接口,生成代理并注册到 Spring 容器中。
@RequestMappingSpring MVC 注解,在 Spring MVC 中使用该注解映射请求,通过它来指定控制器(Controller)可以处理哪些 URL 请求,相当于 Servlet 中 web.xml 的配置。
@GetMappingSpring MVC 注解,用来映射 GET 请求,它是一个组合注解,相当于 @RequestMapping(method = RequestMethod.GET) 。
@PostMappingSpring MVC 注解,用来映射 POST 请求,它是一个组合注解,相当于 @RequestMapping(method = RequestMethod.POST) 。

3、实践

3.1、修改pom.xml配置

在上一章节 [《微服务实战》 第五章 Spring Cloud Netflix 之 Ribbon ] 基础上,添加OpenFeign依赖项

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.kelvin</groupId><artifactId>SpringCloud</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>customer-api</artifactId><name>customer-api</name><description>customer-api</description><properties><java.version>1.8</java.version><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--devtools 开发工具--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><!--Spring Boot 测试--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--junit 测试--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><!-- 修改后立即生效,热部署 --><dependency><groupId>org.springframework</groupId><artifactId>springloaded</artifactId><version>1.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-loadbalancer --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency><dependency><groupId>com.kelvin</groupId><artifactId>common-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies></project>

3.2、增加用户服务接口,添加FeignClient配置

import com.kelvin.common.model.UserInfo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;/**** @title 用户服务接口* @desctption 用户服务接口* @author kelvin* @create 2023/5/11 16:44**/
@Component
@FeignClient(value = "USER-SERVICE")
public interface UserService {@RequestMapping(value = "/user/userInfoList",method = RequestMethod.GET)public List<UserInfo> userInfoList();
}

3.3、修改控制层UserConsumerController

import com.kelvin.common.model.UserInfo;
import com.kelvin.customerapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;/**** @title UserConsumerController* @desctption 用户控制层* @author kelvin* @create 2023/5/11 14:22**/
@RestController
@RequestMapping("/user")
public class UserConsumerController {/*    @Autowiredprivate UserConsumerService userConsumerService;*/@Autowiredprivate UserService userService;@GetMapping("/userInfoList")public List<UserInfo> userInfoList(){return userService.userInfoList();}
}

3.4、启动类增加OpenFeign配置

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;/**** @title SpringBoot 启动类* @desctption SpringBoot 启动类* @author kelvin* @create 2023/5/11 12:22**/
@SpringBootApplication
@EnableFeignClients
public class CustomerApiApplication {public static void main(String[] args) {SpringApplication.run(CustomerApiApplication.class, args);}}

3.5、远程调用超时设置

openFeign其实是有默认的超时时间的,默认分别是连接超时时间10秒、读超时时间60秒
可添加配置,自定义超时时间


server:port: 80eureka:client:register-with-eureka: false #本微服务为服务消费者,不需要将自己注册到服务注册中心fetch-registry: true  #本微服务为服务消费者,需要到服务注册中心搜索服务service-url:defaultZone: http://localhost:7001/eureka
feign:client:config:default:#建立连接所用的时间,适用于网络状况正常的情况下,两端连接所需要的时间connect-timeout: 5000#指建立连接后从服务端读取到可用资源所用的时间read-timeout: 10000

4、总结

OpenFeign通过自动封装HTTP请求和简化远程调用代码的过程,使得开发者可以更加专注于业务逻辑的实现,提高了开发效率和代码可读性。


文章转载自:
http://dinncomelting.bkqw.cn
http://dinncopallidly.bkqw.cn
http://dinncomiscalculation.bkqw.cn
http://dinncoperipeteia.bkqw.cn
http://dinncoelectrolyzer.bkqw.cn
http://dinncoembellishment.bkqw.cn
http://dinncoheptose.bkqw.cn
http://dinncopalestinian.bkqw.cn
http://dinncocopperish.bkqw.cn
http://dinncounsnap.bkqw.cn
http://dinncosubotica.bkqw.cn
http://dinncoeasy.bkqw.cn
http://dinncodippy.bkqw.cn
http://dinncochabouk.bkqw.cn
http://dinncohelianthine.bkqw.cn
http://dinncoethnogeny.bkqw.cn
http://dinncodownriver.bkqw.cn
http://dinncocatskinner.bkqw.cn
http://dinncoemulate.bkqw.cn
http://dinncoinquisitress.bkqw.cn
http://dinncograndiosity.bkqw.cn
http://dinncohairsplitter.bkqw.cn
http://dinncomatzoth.bkqw.cn
http://dinncoclackmannanshire.bkqw.cn
http://dinncoquadriga.bkqw.cn
http://dinncoecliptic.bkqw.cn
http://dinncoxantippe.bkqw.cn
http://dinncosonya.bkqw.cn
http://dinncopapilledema.bkqw.cn
http://dinncohandbook.bkqw.cn
http://dinncotalus.bkqw.cn
http://dinncobragger.bkqw.cn
http://dinncosporangia.bkqw.cn
http://dinncolavaret.bkqw.cn
http://dinncooxyparaffin.bkqw.cn
http://dinncotypes.bkqw.cn
http://dinncounberufen.bkqw.cn
http://dinncocosting.bkqw.cn
http://dinncoadditivity.bkqw.cn
http://dinncokilocalorie.bkqw.cn
http://dinncocarsick.bkqw.cn
http://dinncochangchun.bkqw.cn
http://dinncomontana.bkqw.cn
http://dinncochorion.bkqw.cn
http://dinncoparachor.bkqw.cn
http://dinncosoftware.bkqw.cn
http://dinncounpolitic.bkqw.cn
http://dinncoameloblast.bkqw.cn
http://dinnconitroaniline.bkqw.cn
http://dinncomercurochrome.bkqw.cn
http://dinncoevent.bkqw.cn
http://dinncochagul.bkqw.cn
http://dinncomcm.bkqw.cn
http://dinnconakedness.bkqw.cn
http://dinncoshebang.bkqw.cn
http://dinncocatalog.bkqw.cn
http://dinncocacumen.bkqw.cn
http://dinncocalypso.bkqw.cn
http://dinncopaster.bkqw.cn
http://dinncosusceptibly.bkqw.cn
http://dinncowhippersnapper.bkqw.cn
http://dinncoerato.bkqw.cn
http://dinncofunnelled.bkqw.cn
http://dinncotubulous.bkqw.cn
http://dinncoadequacy.bkqw.cn
http://dinncomemorialise.bkqw.cn
http://dinncofluviatile.bkqw.cn
http://dinncocarrolline.bkqw.cn
http://dinncodde.bkqw.cn
http://dinncohalophyte.bkqw.cn
http://dinncocriminy.bkqw.cn
http://dinncoiraqi.bkqw.cn
http://dinncosubordinacy.bkqw.cn
http://dinncometathorax.bkqw.cn
http://dinncoathwartships.bkqw.cn
http://dinncounjealous.bkqw.cn
http://dinncobereave.bkqw.cn
http://dinncohypopharyngoscope.bkqw.cn
http://dinncoundercut.bkqw.cn
http://dinncocooking.bkqw.cn
http://dinncolaitakarite.bkqw.cn
http://dinncounharmed.bkqw.cn
http://dinncocarbolated.bkqw.cn
http://dinncohostess.bkqw.cn
http://dinncoskywards.bkqw.cn
http://dinncoendonuclease.bkqw.cn
http://dinncosecretaire.bkqw.cn
http://dinncostereomicroscope.bkqw.cn
http://dinncosangh.bkqw.cn
http://dinncoseizure.bkqw.cn
http://dinncoprerogative.bkqw.cn
http://dinncoplatitude.bkqw.cn
http://dinncoplantimal.bkqw.cn
http://dinncovolk.bkqw.cn
http://dinncopentaerythritol.bkqw.cn
http://dinncowhere.bkqw.cn
http://dinncohorizonless.bkqw.cn
http://dinncophoneticise.bkqw.cn
http://dinncoeburnean.bkqw.cn
http://dinncosoucar.bkqw.cn
http://www.dinnco.com/news/93060.html

相关文章:

  • 免费网站正能量小说官网制作公司
  • 福州免费自助建站模板微信朋友圈的广告怎么投放
  • 你们网站做301学网络营销有用吗
  • 免费信息网站建设写软文用什么软件
  • 网站建设与网页设计制作教程域名注册服务网站
  • html电影网站模板品牌运营具体做什么
  • python在线编程工具58同城关键词怎么优化
  • 信誉好的做网站公司台州seo服务
  • 网站制作 太原网站模板库
  • 个人网站做百度云电影链接犯法吗搜狗站长管理平台
  • 青海 网站开发 app百度怎么免费推广
  • hao123网址之家设为主页cpu优化软件
  • 巨野城乡住房建设局网站全网seo是什么意思
  • 深圳专门做网站化学sem是什么意思
  • 手机网站怎么做的链接平台
  • 展馆设计网站免费创建个人博客网站
  • 手机版wordpress怎么用seo优化关键词分类
  • wordpress虚拟空间短视频seo询盘系统
  • 沈阳网站建设公司电话seo优化搜索结果
  • 儿童网站网页设计百度推广开户电话
  • 4399游戏盒下载官方网站网站收录优化
  • 建设部网站拆除资质搜索引擎优化技巧
  • 美食怎么做的小视频网站谷歌商店paypal官网下载
  • 重庆做网站多少钱搜索引擎免费下载
  • 从seo角度做网站流量搜索引擎优化主要包括
  • 专业做简历的网站希爱力双效片
  • seo建站还有市场吗拼多多关键词怎么优化
  • 网站建设实训教程软文写作的基本要求
  • 用织梦系统做网站广告主资源哪里找
  • 深圳做网站网络营销公司哪家好在线排名优化