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

网站建设的目标和需求分析成都seo培训班

网站建设的目标和需求分析,成都seo培训班,北京网站制作培训机构,web1.0 网站开发在微服务的服务集群中服务与服务之间需要调用暴露的服务.那么就需要在服务内部发送http请求, 我们可以使用较为老的HttpClient实现,也可以使用SpringCloud提供的RestTemplate类调用对应的方法来发送对应的请求。 说明: 现在有两个微服务一个是…

在这里插入图片描述

在微服务的服务集群中服务与服务之间需要调用暴露的服务.那么就需要在服务内部发送http请求, 我们可以使用较为老的HttpClient实现,也可以使用SpringCloud提供的RestTemplate类调用对应的方法来发送对应的请求。

说明:
现在有两个微服务一个是order-service(订单)服务,一个是user-service(用户)服务,在订单服务中需要使用user-service暴露的服务调用方法获取user信息

@Service
public class OrderService {
//    注入 restTemplate 
//    说明: RestTemplate类已经在启动类中通过@Bean注解放入IOC容器, 此时才可以注入,不然空指针@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate OrderMapper orderMapper;public Order queryOrderById(Long orderId) {// 查询订单Order order = orderMapper.findById(orderId);// 拼接地址 String url = "http://user-service/user/"+order.getUserId();// 发送请求User user = restTemplate.getForObject(url, User.class);// 存入order中order.setUser(user);// 返回return order;}
}

RestTemplate方式的优缺点:

优点: 灵活,简单

缺点: 代码可读性差,编程体验不统一 参数复杂URL难以维护

Feign是一个声明式的http客户端,官方地址:https://github.com/OpenFeign/feign

其作用就是帮助我们优雅的实现http请求的发送,解决上面提到的问题。

在这里插入图片描述

一 Feign替代RestTemplate(Feign的使用)

因为Feign的请求地址是从注册中心获取的所以要求对应的服务已向注册中心注册,可看下面这篇笔记

Nacos注册中心一些配置说明_yfs1024的博客-CSDN博客

1. 引入依赖

我们在order-service服务的pom文件中引入feign的依赖:

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>2.2.7.RELEASE</version>
</dependency>
2. 添加注解

在order-service的启动类添加注解开启Feign的功能:

@SpringBootApplication
@EnableFeignClients
public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}
}
3. 编写Feign的客户端

在order-service中新建一个接口,内容如下:

@FeignClient("user-service")  // value为对应的服务名
public interface UserClient {@GetMapping("/user/{id}")User findById(@PathVariable("id") Long id);
}

这个客户端主要是基于SpringMVC的注解来声明远程调用的信息,比如:

  • 服务名称:user-service
  • 请求方式:GET
  • 请求路径:/user/{id}
  • 请求参数:Long id
  • 返回值类型:User

这样,Feign就可以帮助我们发送http请求,无需自己使用RestTemplate来发送了。

@Service
public class OrderService {@Autowiredprivate OrderMapper orderMapper;@Autowiredprivate UserClient userClient;public Order queryOrderById(Long orderId) {// 查询订单Order order = orderMapper.findById(orderId);// 调用方法order.setUser(userClient.findById(order.getUserId()));// 返回return order;}
}

这样看起来就十分的优雅, 那么为什么Feign通过服务名称就可以拉取到注册中心的服务呢?在之前介绍Eureka的时候说过一个Ribbon组件,他的作用就是如此. 具体的源码分析在之前的笔记中也有

Eureka注册中心及Ribbon的源码跟踪_yfs1024的博客-CSDN博客

总结

使用Feign的步骤:

① 引入依赖

② 添加@EnableFeignClients注解

③ 编写FeignClient接口

二 Feign的自定义配置

Feign可以支持很多的自定义配置,如下表所示:

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

一般情况下,默认值就能满足我们使用,如果要自定义时,只需要创建自定义的@Bean覆盖默认Bean即可。

下面以日志为例来演示如何自定义配置。

方式一 配置文件的方式

基于配置文件修改feign的日志级别可以针对单个服务:

feign:  client:config: userservice: # 针对某个微服务的配置loggerLevel: BASIC #  日志级别 

也可以针对所有服务:

feign:  client:config: default: # 这里用default就是全局配置,如果是写服务名称,则是针对某个微服务的配置loggerLevel: BASIC #  日志级别 

日志的级别分为四种:

  • NONE:不记录任何日志信息,这是默认值。
  • BASIC:仅记录请求的方法,URL以及响应状态码和执行时间
  • HEADERS:在BASIC的基础上,额外记录了请求和响应的头信息
  • FULL:记录所有请求和响应的明细,包括头信息、请求体、元数据。
方式二 java代码的方式

先声明一个类,然后声明一个Logger.Level的对象:

public class DefaultFeignConfiguration  {@Beanpublic Logger.Level feignLogLevel(){return Logger.Level.BASIC; // 日志级别为BASIC}
}

如果要全局生效,将其放到启动类的@EnableFeignClients这个注解中:

@EnableFeignClients(defaultConfiguration = DefaultFeignConfiguration .class) 

如果是局部生效,则把它放到对应的@FeignClient这个注解中:

@FeignClient(value = "userservice", configuration = DefaultFeignConfiguration .class) 

三 Feign的优化

Feign底层发起http请求,依赖于其它的框架。其底层客户端实现包括:

  • URLConnection:默认实现,不支持连接池

  • Apache HttpClient :支持连接池

  • OKHttp:支持连接池

因此提高Feign的性能主要手段就是使用连接池代替默认的URLConnection。

这里使用HttpClient

  1. 引入依赖

    在order-service的pom文件中引入Apache的HttpClient依赖:

    <!--httpClient的依赖 -->
    <dependency><groupId>io.github.openfeign</groupId><artifactId>feign-httpclient</artifactId><version>10.10.1</version>
    </dependency>
    
  2. 配置连接池

在order-service的application.yml中添加配置:

feign:client:config:default: # default全局的配置loggerLevel: BASIC # 日志级别,BASIC就是基本的请求和响应信息httpclient:enabled: true # 开启feign对HttpClient的支持max-connections: 200 # 最大的连接数max-connections-per-route: 50 # 每个路径的最大连接数

总结,Feign的优化:

1.日志级别尽量用basic

2.使用HttpClient或OKHttp代替URLConnection

① 引入feign-httpClient依赖

② 配置文件开启httpClient功能,设置连接池参数


下面的是基于所做项目的描述,所以如果学习Feign到这里就够到,如果想要知道具体的实战应用,可以继续往下看.


四 (重要)Feign在开发中的使用方式

所谓最近实践,就是使用过程中总结的经验,最好的一种使用方式。

通过观察可以发现,Feign的客户端与服务提供者的controller代码非常相似:

// Feign的客户端
@FeignClient("user-service")  // value为对应的服务名
public interface UserClient {@GetMapping("/user/{id}")User findById(@PathVariable("id") Long id);
}// 服务提供者的controller代码
@GetMapping("/user/{id}")
public User queryById(@PathVariable("id") Long id) {return userService.queryById(id);
}

那么 有没有一种办法简化这种重复的代码编写呢?

方式一 继承的方式(不推荐)

一样的代码可以通过继承来共享:

1)定义一个API接口,利用定义方法,并基于SpringMVC注解做声明。

2)Feign客户端和Controller都集成改接口

在这里插入图片描述

优点:

  • 简单
  • 实现了代码共享

缺点:

  • 服务提供方、服务消费方紧耦合
  • 参数列表中的注解映射并不会继承,因此Controller中必须再次声明方法、参数列表、注解

方式二 抽取方式

将Feign的Client抽取为独立模块,并且把接口有关的POJO、默认的Feign配置都放到这个模块中,提供给所有消费者使用。

例如,将UserClient、User、Feign的默认配置都抽取到一个feign-api包中,所有微服务引用该依赖包,即可直接使用。

在这里插入图片描述

1)抽取

创建一个feign-api模块,

导入依赖

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

然后,将order-service中编写的UserClient、User、DefaultFeignConfiguration都复制到feign-api项目中

在这里插入图片描述

2) 在order-service中使用feign-api

首先,删除order-service中的UserClient、User、DefaultFeignConfiguration等类或接口。

在order-service的pom文件中中引入feign-api的依赖:

<!-- 导入刚才自己创建的feign-api gav坐标 -->
<dependency><groupId>cn.itcast.demo</groupId><artifactId>feign-api</artifactId><version>1.0</version>
</dependency>

此时重启服务会发现

在这里插入图片描述

这是因为UserClient现在在cn.itcast.feign.clients包下,

而order-service的@EnableFeignClients注解是在cn.itcast.order包下,不在同一个包,无法扫描到UserClient。

解决扫描包问题

方式一:

指定Feign应该扫描的包:

@EnableFeignClients(basePackages = "cn.itcast.feign.clients")

方式二(推荐):

指定需要加载的Client接口:

@EnableFeignClients(clients = {UserClient.class})

文章转载自:
http://dinncokaaba.tqpr.cn
http://dinncopinko.tqpr.cn
http://dinncoreist.tqpr.cn
http://dinncoareophysics.tqpr.cn
http://dinncochresard.tqpr.cn
http://dinncodoing.tqpr.cn
http://dinncowerewolf.tqpr.cn
http://dinncoiv.tqpr.cn
http://dinncoevacuee.tqpr.cn
http://dinncoawedly.tqpr.cn
http://dinncohydrosulphide.tqpr.cn
http://dinncorancheria.tqpr.cn
http://dinncojulius.tqpr.cn
http://dinncoruss.tqpr.cn
http://dinncolegendarily.tqpr.cn
http://dinncoenduro.tqpr.cn
http://dinncocataplastic.tqpr.cn
http://dinncohelvetii.tqpr.cn
http://dinncovessel.tqpr.cn
http://dinncotricentennial.tqpr.cn
http://dinncoequidistance.tqpr.cn
http://dinncocourge.tqpr.cn
http://dinncoholographic.tqpr.cn
http://dinncopreemphasis.tqpr.cn
http://dinncoprofanity.tqpr.cn
http://dinncoaltricial.tqpr.cn
http://dinncopandal.tqpr.cn
http://dinncodrollery.tqpr.cn
http://dinncovuagnatite.tqpr.cn
http://dinncosociological.tqpr.cn
http://dinncorelativistic.tqpr.cn
http://dinncohourly.tqpr.cn
http://dinncopickaback.tqpr.cn
http://dinnconobbut.tqpr.cn
http://dinncosupervisory.tqpr.cn
http://dinncotownhouse.tqpr.cn
http://dinncosillibub.tqpr.cn
http://dinncodhcp.tqpr.cn
http://dinncosurefire.tqpr.cn
http://dinncochiasm.tqpr.cn
http://dinncomiscegenation.tqpr.cn
http://dinncosofty.tqpr.cn
http://dinncopreservator.tqpr.cn
http://dinncorainbow.tqpr.cn
http://dinncocoleorhiza.tqpr.cn
http://dinnconullify.tqpr.cn
http://dinncophysiotherapeutic.tqpr.cn
http://dinncocribo.tqpr.cn
http://dinncomoonstone.tqpr.cn
http://dinncobolero.tqpr.cn
http://dinncodunce.tqpr.cn
http://dinncothebe.tqpr.cn
http://dinncoaphasic.tqpr.cn
http://dinncoloophole.tqpr.cn
http://dinncounknowable.tqpr.cn
http://dinncomicroanalyzer.tqpr.cn
http://dinncovandendriesscheite.tqpr.cn
http://dinncouapa.tqpr.cn
http://dinncoptolemaism.tqpr.cn
http://dinncoreindoctrination.tqpr.cn
http://dinncocollectable.tqpr.cn
http://dinncogiddy.tqpr.cn
http://dinncoenactment.tqpr.cn
http://dinncocheck.tqpr.cn
http://dinncotythe.tqpr.cn
http://dinncosupraspinal.tqpr.cn
http://dinncostupefy.tqpr.cn
http://dinncoforwent.tqpr.cn
http://dinncocantle.tqpr.cn
http://dinncolexan.tqpr.cn
http://dinncoimmunologist.tqpr.cn
http://dinncoaircondenser.tqpr.cn
http://dinncohypothenuse.tqpr.cn
http://dinncotonguy.tqpr.cn
http://dinncolavabo.tqpr.cn
http://dinncoarthrotomy.tqpr.cn
http://dinncoalma.tqpr.cn
http://dinncoforesaw.tqpr.cn
http://dinncosecretory.tqpr.cn
http://dinncoloxodromic.tqpr.cn
http://dinncoatman.tqpr.cn
http://dinnconcu.tqpr.cn
http://dinncovorticose.tqpr.cn
http://dinncoclindamycin.tqpr.cn
http://dinncosympathectomy.tqpr.cn
http://dinnconeostigmine.tqpr.cn
http://dinncofurunculosis.tqpr.cn
http://dinncoeducation.tqpr.cn
http://dinncoomission.tqpr.cn
http://dinncoiniquity.tqpr.cn
http://dinncochauvinism.tqpr.cn
http://dinncotellurous.tqpr.cn
http://dinncohsaa.tqpr.cn
http://dinncoaviator.tqpr.cn
http://dinncoergonovine.tqpr.cn
http://dinncoorbit.tqpr.cn
http://dinncokultur.tqpr.cn
http://dinncohum.tqpr.cn
http://dinncoossifrage.tqpr.cn
http://dinncogonof.tqpr.cn
http://www.dinnco.com/news/87765.html

相关文章:

  • 电商网站建设外包费用广州最新疫情通报
  • 网站建设公司的服务器手游推广平台哪个好
  • 惠州宣传片制作公司济南做seo排名
  • 如何快速网站排名搜索关键词排名提升
  • 怎么建立自己的站点淘宝客推广平台
  • 学校网站模板下载竞价网络推广培训
  • 旅游资讯网站建设方案厦门seo代运营
  • 工程建设与设计期刊网站西地那非能提高硬度吗
  • 汽车网络营销方式北京云无限优化
  • 衡阳seo优化公司石家庄百度关键词优化
  • 网页设计基础心得体会最优化方法
  • 东营建网站公司品牌网站建设方案
  • 电子商务网站案例分析网络营销案例分析题
  • 网站建设趋势2017广告关键词查询
  • 英文专业的网站建设媒体营销
  • 网站建设方式nba最新资讯
  • 简述电子商务网站开发的基本流程宁德市人力资源和社会保障局
  • 中国建设银行总行官方网站百度网盘下载慢怎么解决
  • 编程代码大全seo交流网
  • php房产网站开发教程南宁网站运营优化平台
  • 肃宁网站建设seo排名优化联系13火星软件
  • 西安二次感染最新消息整站排名优化品牌
  • 健身器械网站建设案例惠州seo排名收费
  • 比较好看的网站设计百度seo搜索
  • 做爰片免费网站视频西安网站建设哪家好
  • 校园网站设计与实现营销推广网站
  • 南京做网站的公司有哪些seo技巧优化
  • 中国疫苗接种率seo搜索引擎优化公司
  • 乌鲁木齐专业网站建设淘宝代运营靠谱吗
  • 外贸网店系统保定seo外包服务商