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

学校网站平台建设市场调研报告3000字范文

学校网站平台建设,市场调研报告3000字范文,西安疫情防控最新政策,个人网页制作素材图片目录前言SpringCloud Feign远程服务调用一.远程调用逻辑图二.两个服务的yml配置和访问路径三.使用RestTemplate远程调用四.构建Feign五.自定义Feign配置六.Feign配置日志七.Feign调优八.抽离Feign前言 微服务分解成多个不同的服务,那么多个服务之间怎么调用呢&…

目录

  • 前言
  • SpringCloud Feign远程服务调用
    • 一.远程调用逻辑图
    • 二.两个服务的yml配置和访问路径
    • 三.使用RestTemplate远程调用
    • 四.构建Feign
    • 五.自定义Feign配置
    • 六.Feign配置日志
    • 七.Feign调优
    • 八.抽离Feign

前言

微服务分解成多个不同的服务,那么多个服务之间怎么调用呢?
Spring Cloud最新面试题
Spring Cloud Nacos详解之注册中心
Spring Cloud Nacos详解之配置中心
Spring Cloud Nacos详解之集群配置
Spring Cloud Eureka详解
Spring Cloud Ribbon详解
Spring Cloud Gateway详解
Spring Cloud Hystrix详解

SpringCloud Feign远程服务调用

一.远程调用逻辑图

现在有两个服务,订单服务(服务消费者)和用户服务(服务提供者),分别对应不同的数据库。
注意: 因为远程调用都是服务消费者调用服务提供者,所以配置和业务编写,都在服务消费者里面。
在这里插入图片描述

二.两个服务的yml配置和访问路径

用两个不同的数据库,模拟部署在两台服务器的数据库订单yml配置   访问路径:@GetMapping("order/{orderId}")
server:port: 8082
spring:datasource:url: jdbc:mysql://localhost:3306/cloud_order?useSSL=falseusername: rootpassword: rootdriver-class-name: com.mysql.jdbc.Driverapplication:name: orderservice //订单服务的名称用户yml配置  访问路径:@GetMapping("user/{id}")
server:port: 8081
spring:datasource:url: jdbc:mysql://localhost:3306/cloud_user?useSSL=falseusername: rootpassword: rootdriver-class-name: com.mysql.jdbc.Driverapplication:name: userservice //用户服务的名称

三.使用RestTemplate远程调用

1.注入RestTemplate

	/*** 因为启动类本身也是一个配置了,所以我们在启动类进行注入,你自己自定义配置类注入也行* 创建RestTemplate并注入Spring容器*/@Bean@LoadBalancedpublic RestTemplate restTemplate() {return new RestTemplate();}

2.编写远程调用

@Autowiredprivate RestTemplate restTemplate;public Order queryOrderById(Long orderId) {// 根据订单id查询订单Order order = orderMapper.findById(orderId);// 利用RestTemplate发起http请求,根据用户id查询用户// url路径  http://服务名称(上面配置了)/请求路径/参数String url = "http://localhost:8081/user/" + order.getUserId();// 发送http请求,实现远程调用,现在是get请求类型User user = restTemplate.getForObject(url, User.class);// 封装user到Orderorder.setUser(user);// 返回值return order;}

3.RestTemplate的缺点

  • 参数复杂URL难以维护。
  • 不符合正常接口调用的格式。

四.构建Feign

1.引入依赖

		<!--feign客户端依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>

2.在启动类使用注解开启Feign功能

@SpringBootApplication
@EnableFeignClients
public class OrderApplication {
}

3.编写远程调用

//服务名称
@FeignClient(value = "userservice")
public interface UserClient {@GetMapping("/user/{id}")User findById(@PathVariable("id") Long id);}

4.调用接口

@Autowiredprivate UserClient userClient;public Order queryOrderById(Long orderId) {// 根据订单id查询订单Order order = orderMapper.findById(orderId);// 用Feign远程调用User user = userClient.findById(order.getUserId());// 封装user到Orderorder.setUser(user);// 返回return order;}

5.Feign还集成了Ribbon,所以我们不用考虑负载均衡问题

在这里插入图片描述

五.自定义Feign配置

类型作用说明
feign.Logger.Level修改日志级别四种不同的级别:NONE(没有任何日志)、BASIC(发起请求的开始结束时间)、HEADERS(会记录请求头请求体)、FULL(请求和响应信息)
feign.codec.Decoder响应结果的解析器http远程调用的结果做解析,例如解析json字符串为java对象
feign.codec.Encoder请求参数编码将请求参数编码,便于通过http请求发送
feign. Contract支持的注解格式默认是SpringMVC的注解
feign. Retryer失败重试机制请求失败的重试机制,默认是没有,不过会使用Ribbon的重试

一般我们自定义配置的是日志

六.Feign配置日志

1.配置文件配置日志

//全局配置
feign:client:config:default://default全局配置,远程调用的服务的接口也会打印。loggerLevel:FULL //日志级别//局部配置
feign:client:config:orderservice://只打印服务名为orderservice的日志。loggerLevel:FULL //日志级别

2.代码方式配置日志

//第一步:注入对象
public class DefaultFeignConfiguration {@Beanpublic Logger.Level logLevel(){//日志级别return Logger.Level.BASIC;}
}
//第二步:注解配置//全局配置
@EnableFeignClients(defaultConfiguration = DefaultFeignConfiguration.class)//局部配置
@FeignClient(value = "userservice",confiquration = FeignClientConfiguration.class)

七.Feign调优

Feign底层客户端实现:

  • URLConnection:默认实现,不支持连接池。
  • Apache HttpClient: 支持连接池。
  • OKHttp:支持连接池。

1.使用连接池替代默认的URL Connection(使用HttpClient支持)

①pom文件引入依赖

	    <!--引入HttpClient依赖--><dependency><groupId>io.github.openfeign</groupId><artifactId>feign-httpclient</artifactId></dependency>

②yml文件进行配置

feign:httpclient:enabled: true # 支持HttpClient的开关max-connections: 100 # 最大连接数max-connections-per-route: 25 # 单个路径的最大连接数

2.日志级别最好是basic或none

八.抽离Feign

如果A、B、C服务都要调用D服务,那我们要在A、B、C里面都使用Feign调用D吗,如果写就造成了很多代码冗余,所以我们要把Feign抽离出来放在一个公共的服务里面。我们新建一个Feign-api服务,然后谁用谁就在pom文件引入一下。

注意: 需要把调用的接口(加上@FeignClient(vaule=服务名)注解)和实体类写在公共的服务里面。

Feign-api结构目录
在这里插入图片描述

		<!--谁用谁引入,引入feign的统一api--><dependency><groupId>cn.xinxin.demo</groupId><artifactId>feign-api</artifactId><version>1.0</version></dependency>

但是这样做会导致SpringBootApplication在扫描包时找不到定义FeignClient对象,那么怎么解决呢?

解决

方式一:指定FeignClient所在包
@EnableFeignClients(basePackages = "cn.xinxin.feign.clients")方式二:指定FeignClient字节码
EnableFeignClients(clients = {UserClient.class})

Feign的pom文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>cloud-demo</artifactId><groupId>cn.xinxin.demo</groupId><version>1.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>feign-api</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><!--feign客户端依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency></dependencies>
</project>

示例: 订单服务远程调用用户服务
在这里插入图片描述


文章转载自:
http://cahot.xhqr.cn
http://basification.xhqr.cn
http://octangular.xhqr.cn
http://ossa.xhqr.cn
http://showup.xhqr.cn
http://beingless.xhqr.cn
http://psylla.xhqr.cn
http://ghettoize.xhqr.cn
http://jovial.xhqr.cn
http://pathology.xhqr.cn
http://hernia.xhqr.cn
http://altruist.xhqr.cn
http://aestival.xhqr.cn
http://paedologist.xhqr.cn
http://perispomenon.xhqr.cn
http://singe.xhqr.cn
http://blutwurst.xhqr.cn
http://disfluency.xhqr.cn
http://spik.xhqr.cn
http://syzygy.xhqr.cn
http://araby.xhqr.cn
http://dryest.xhqr.cn
http://lagging.xhqr.cn
http://tannery.xhqr.cn
http://hydrotactic.xhqr.cn
http://gangmaster.xhqr.cn
http://glorious.xhqr.cn
http://prophetic.xhqr.cn
http://chudder.xhqr.cn
http://alarum.xhqr.cn
http://gaffsail.xhqr.cn
http://oversteering.xhqr.cn
http://transpacific.xhqr.cn
http://animism.xhqr.cn
http://devotement.xhqr.cn
http://almandine.xhqr.cn
http://xylographer.xhqr.cn
http://glabella.xhqr.cn
http://cobaltammine.xhqr.cn
http://freeminded.xhqr.cn
http://comprizal.xhqr.cn
http://islandless.xhqr.cn
http://lepidoptera.xhqr.cn
http://antiestrogen.xhqr.cn
http://supergraphics.xhqr.cn
http://hutment.xhqr.cn
http://mumpish.xhqr.cn
http://gaseous.xhqr.cn
http://lengthen.xhqr.cn
http://highbinder.xhqr.cn
http://andorra.xhqr.cn
http://catchweed.xhqr.cn
http://suiting.xhqr.cn
http://senarmontite.xhqr.cn
http://chemoceptor.xhqr.cn
http://localise.xhqr.cn
http://thrum.xhqr.cn
http://greenway.xhqr.cn
http://duoplasmatron.xhqr.cn
http://imbalance.xhqr.cn
http://overrigid.xhqr.cn
http://endocarp.xhqr.cn
http://get.xhqr.cn
http://epitome.xhqr.cn
http://muderer.xhqr.cn
http://roc.xhqr.cn
http://exfoliation.xhqr.cn
http://transfixion.xhqr.cn
http://bunnia.xhqr.cn
http://troppo.xhqr.cn
http://deprive.xhqr.cn
http://illuminate.xhqr.cn
http://antilogarithm.xhqr.cn
http://proletariate.xhqr.cn
http://undersow.xhqr.cn
http://apophysis.xhqr.cn
http://thitherto.xhqr.cn
http://coolant.xhqr.cn
http://inconveniently.xhqr.cn
http://echolalia.xhqr.cn
http://contour.xhqr.cn
http://sumpitan.xhqr.cn
http://pneumatology.xhqr.cn
http://carvel.xhqr.cn
http://colter.xhqr.cn
http://wenlockian.xhqr.cn
http://unset.xhqr.cn
http://pavin.xhqr.cn
http://airflow.xhqr.cn
http://ovulary.xhqr.cn
http://recuperative.xhqr.cn
http://responsible.xhqr.cn
http://absurdly.xhqr.cn
http://vail.xhqr.cn
http://flirtation.xhqr.cn
http://acronically.xhqr.cn
http://microprobe.xhqr.cn
http://sporulate.xhqr.cn
http://revenooer.xhqr.cn
http://resay.xhqr.cn
http://www.15wanjia.com/news/95774.html

相关文章:

  • 网站建设费用无形资产如何摊销百度浏览器网址是多少
  • 全屋定制设计网站推荐图片搜索识图入口
  • 深圳旅游公司网站网推技巧
  • 辽宁建设工程信息网官网盲盒系统网络营销乐云seo
  • 内江网站制作南京谷歌优化
  • 什么网站做视频大连seo关键词排名
  • 荥阳网站建设公司深圳市网络营销推广服务公司
  • 简易购物系统网站seo系统
  • 湖北省建设厅网站资质青岛seo培训
  • 厦门网站建设方案咨询百度自己的宣传广告
  • 网站备案期间怎么做免费的网站推广平台
  • 怎样自己做企业的网站网站关键词优化排名技巧
  • 家纺行业英文网站模板百度推广登录入口下载
  • 如何做网站怎么赚钱搜索引擎营销是什么意思
  • 唐山企业网站建设培训机构管理系统哪个好
  • 阿里云云服务器ecs能直接做网站什么是网络营销平台
  • 东莞商城网站开发常用的seo工具推荐
  • 玉环在哪里做网站夸克搜索引擎入口
  • seo招聘要求龙斗seo博客
  • 网站关键词在哪里做最新的军事新闻
  • 制作外贸网站国外免费ip地址
  • 网上做设计网站自媒体账号申请
  • 做网站下载好素材之后怎么建造主页网络广告有哪些形式
  • 域名哪个网站续费短视频运营公司
  • 跨境电商网站系统开发站长之家网站排名
  • 如何做企业网站及费用问题百度平台我的订单
  • 网站一般如何做搜索功能外包公司到底值不值得去
  • 做网站和app那个花销大一键生成个人网站
  • 无障碍网站建设方案怎样淘宝seo排名优化
  • 大连公司做网站国内广告联盟平台