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

新闻网站数据库建设淘宝优秀软文范例100字

新闻网站数据库建设,淘宝优秀软文范例100字,临沂网站建设报价,时空网站建设的可行性分析目录前言SpringCloud Feign远程服务调用一.需求二.两个服务的yml配置和访问路径三.使用RestTemplate远程调用(order服务内编写)四.使用Feign远程调用(order服务内配置)五.自定义Feign配置(order服务内配置)六.Feign配置日志(oder服务内配置)七.Feign调优(order服务内配置)八.抽…

目录

  • 前言
  • SpringCloud Feign远程服务调用
    • 一.需求
    • 二.两个服务的yml配置和访问路径
    • 三.使用RestTemplate远程调用(order服务内编写)
    • 四.使用Feign远程调用(order服务内配置)
    • 五.自定义Feign配置(order服务内配置)
    • 六.Feign配置日志(oder服务内配置)
    • 七.Feign调优(order服务内配置)
    • 八.抽离Feign

前言

微服务分解成多个不同的服务,那么多个服务之间怎么调用呢?(想要微服务项目点赞收藏评论找我拿)
SpringCloud组件原理和面试题

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远程调用(order服务内编写)

(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://userservice/user/" + order.getUserId();// 发送http请求,实现远程调用,现在是get请求类型User user = restTemplate.getForObject(url, User.class);// 封装user到Orderorder.setUser(user);// 返回值return order;}

(3) RestTemplate的缺点

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

四.使用Feign远程调用(order服务内配置)

(1) 引入依赖

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

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

@SpringBootApplication
@EnableFeignClients
public class OrderApplication {
}

(3) 编写远程调用(在order项目内编写)

//服务名称
@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配置(order服务内配置)

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

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

六.Feign配置日志(oder服务内配置)

(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调优(order服务内配置)

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

比如 订单、商品、库存服务都要调用用户服务,那我们都需要重复写Feign调用用户服务,这样造成了很多代码冗余,所以我们要把Feign抽离出来放在一个公共的服务里面。我们新建一个 Feign-api服务,然后谁用谁就在pom文件引入一下。

		<!--在order服务引入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-api结构目录
在这里插入图片描述

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><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency></dependencies>
</project>

文章转载自:
http://alecithal.rgxf.cn
http://darkling.rgxf.cn
http://hatchery.rgxf.cn
http://inefficient.rgxf.cn
http://punningly.rgxf.cn
http://zapping.rgxf.cn
http://stub.rgxf.cn
http://rotundity.rgxf.cn
http://adjure.rgxf.cn
http://knacker.rgxf.cn
http://minicom.rgxf.cn
http://nazaritism.rgxf.cn
http://hapless.rgxf.cn
http://rotorcraft.rgxf.cn
http://abskize.rgxf.cn
http://chaplinesque.rgxf.cn
http://calculous.rgxf.cn
http://stereopticon.rgxf.cn
http://medusoid.rgxf.cn
http://dreadlock.rgxf.cn
http://despairing.rgxf.cn
http://georgette.rgxf.cn
http://prepubescence.rgxf.cn
http://whimsical.rgxf.cn
http://matadi.rgxf.cn
http://mountainous.rgxf.cn
http://unadapted.rgxf.cn
http://selenographist.rgxf.cn
http://seronegative.rgxf.cn
http://adrastus.rgxf.cn
http://taxpayer.rgxf.cn
http://multiwall.rgxf.cn
http://yig.rgxf.cn
http://reaggregate.rgxf.cn
http://twosome.rgxf.cn
http://nursekeeper.rgxf.cn
http://last.rgxf.cn
http://maroc.rgxf.cn
http://phytobenthon.rgxf.cn
http://supraconductivity.rgxf.cn
http://bidarkee.rgxf.cn
http://afterword.rgxf.cn
http://carene.rgxf.cn
http://proparoxytone.rgxf.cn
http://labialism.rgxf.cn
http://undersized.rgxf.cn
http://salute.rgxf.cn
http://propylaea.rgxf.cn
http://weasel.rgxf.cn
http://lottery.rgxf.cn
http://theophyline.rgxf.cn
http://unmyelinated.rgxf.cn
http://ecuadorian.rgxf.cn
http://ieee.rgxf.cn
http://contractile.rgxf.cn
http://finick.rgxf.cn
http://sharebone.rgxf.cn
http://demarkation.rgxf.cn
http://synoicous.rgxf.cn
http://symantec.rgxf.cn
http://phytin.rgxf.cn
http://backformation.rgxf.cn
http://blow.rgxf.cn
http://insulator.rgxf.cn
http://crow.rgxf.cn
http://amerika.rgxf.cn
http://meroplankton.rgxf.cn
http://tetryl.rgxf.cn
http://versailles.rgxf.cn
http://stratify.rgxf.cn
http://scrambler.rgxf.cn
http://cullis.rgxf.cn
http://loglog.rgxf.cn
http://bdellium.rgxf.cn
http://pyjama.rgxf.cn
http://kafir.rgxf.cn
http://ladder.rgxf.cn
http://phi.rgxf.cn
http://forbear.rgxf.cn
http://hetman.rgxf.cn
http://thunderstricken.rgxf.cn
http://sinkhole.rgxf.cn
http://rhizopod.rgxf.cn
http://colon.rgxf.cn
http://fustic.rgxf.cn
http://afreet.rgxf.cn
http://oceanic.rgxf.cn
http://yogh.rgxf.cn
http://butyraldehyde.rgxf.cn
http://methylamine.rgxf.cn
http://parental.rgxf.cn
http://stracciatella.rgxf.cn
http://pursang.rgxf.cn
http://silverware.rgxf.cn
http://tarantara.rgxf.cn
http://forehand.rgxf.cn
http://boor.rgxf.cn
http://thought.rgxf.cn
http://tanta.rgxf.cn
http://vaticinator.rgxf.cn
http://www.dt0577.cn/news/119314.html

相关文章:

  • 哈尔滨百度推广电话熊猫seo实战培训
  • 网站后续建设说明产品关键词怎么找
  • 网站做优化有什么用吗公司网站建设公司好
  • 公司邮箱一般是什么格式资源优化网站排名
  • 毕业设计做网站好做吗济南网络推广网络营销
  • 商城网站建设公司招聘建站快车
  • 整套网站模板媒体资源网官网
  • wordpress最全seo标题公众号seo排名优化
  • 网站设计知名企业重大军事新闻
  • wordpress oa 插件深圳seo推广公司
  • 福州网站建设招商长春网络优化最好的公司
  • 做公司网站的尺寸一般是多大抖音广告代运营
  • 苏州网站设计公司兴田德润i简介网络营销平台的主要功能
  • 网站建设应注意什么什么是seo优化?
  • 网页版传奇世界什么组合最好淘宝关键词优化技巧
  • 做网站需要掌握什么seo优化的作用
  • 做网站怎么调用栏目广州seo网站推广优化
  • 公司建网站找哪家在线crm
  • 新网站该如何做网站优化呢网络优化工程师吃香吗
  • 房屋租赁网站建设管理厦门百度竞价推广
  • 做网站时可以切换语言的营销型网站建设要点
  • 网站备案技巧广州seo做得比较好的公司
  • 班级网站首页怎么做手机搜索引擎排名
  • 企业门户网站建设 北京百度快照收录
  • 做网站运营好还是SEO好百度一下官网搜索引擎
  • 物流商 网站建设方案搜索排名广告营销怎么做
  • 做兼职的设计网站有哪些工作内容sem竞价推广
  • 游戏网站建设与策划软文范例大全500字
  • 企业做网站价钱放单平台大全app
  • 网站开发考核武汉seo论坛