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

网站建设常用视频格式免费b站动漫推广网站2023

网站建设常用视频格式,免费b站动漫推广网站2023,网站首页面,陕西省建设银行分行互联网互联网站eureka注册中心和restTemplate的使用说明 eureka的作用 消费者该如何获取服务提供者的具体信息 1.服务者启动时向eureka注册自己的信息 2.eureka保存这些信息 3.消费者根据服务名称向eureka拉去提供者的信息 如果有多个服务提供者,消费者该如何选择? 服…

eureka注册中心和restTemplate的使用说明

eureka的作用

  • 消费者该如何获取服务提供者的具体信息

    1.服务者启动时向eureka注册自己的信息

    2.eureka保存这些信息

    3.消费者根据服务名称向eureka拉去提供者的信息

  • 如果有多个服务提供者,消费者该如何选择?

    服务消费者利用负载均衡算法,从服务列表中挑选一个

  • 消费者如何感知服务提供者的健康状态?

    服务提供者每隔30s向eurekaserver发送心跳请求,报告健康状态。

    eureka会更新记录服务列表信息,心跳不正常会被剔除。

    消费者就可以拉取到最新的消息。

在这里插入图片描述

搭载eureka注册中心

1.引入依赖

    <dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies>

2.在启动类加@EnableEurekaServer注解

3.编写配置文件

server:port: 10086 #端口spring:application:name: eurekaserver #应用名eureka:client:service-url: #eureka注册中心地址defaultZone: http://localhost:10086/eureka

服务注册

1.加入eureka客服端的依赖

		<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>

2.添加客服端的配置

eureka:client:service-url:defaultZone: http://localhost:10086/eureka

服务发现

1.修改url访问路径,用服务名代替ip地址

package cn.itcast.order.web;import cn.itcast.order.pojo.Order;
import cn.itcast.order.pojo.User;
import cn.itcast.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;@RestController
@RequestMapping("order")
public class OrderController {@Autowiredprivate OrderService orderService;@Autowiredprivate RestTemplate restTemplate;@GetMapping("{orderId}")public Order queryOrderByUserId(@PathVariable("orderId") Long orderId) {// 根据id查询订单并返回Order order = orderService.queryOrderById(orderId);String url="http://userserver/user/"+order.getUserId();User user = restTemplate.getForObject(url, User.class);order.setUser(user);return order;}
}

2.加负载均衡的注解

package cn.itcast.order;import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;@MapperScan("cn.itcast.order.mapper")
@SpringBootApplication
public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}@Bean@LoadBalancedpublic RestTemplate restTemplate() {return new RestTemplate();}/* @Beanpublic IRule randomRule(){return new RandomRule();}*/}

负载均衡

在这里插入图片描述

1.通过定义IRule实现可以修改负载均衡的规则


import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;@Bean
public IRule randomRule(){return new RandomRule();
}

2.通过配置文件更改负载均衡规则

userservice: # 给某个微服务配置负载均衡规则,这里是userservice服务ribbon:NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule # 负载均衡规则

RestTemplate接口调用

简介

在项目中,当我们需要远程调用一个 HTTP 接口时,我们经常会用到 RestTemplate 这个类。这个类是 Spring 框架提供的一个工具类。

RestTemplate: The original Spring REST client with a synchronous, template method API.

从上面的介绍中我们可以知道:RestTemplate 是一个同步的 Rest API 客户端。下面我们就来介绍下 RestTemplate 的常用功能。

RestTemplate的使用

1.创建 RestTemplate

@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {RestTemplate restTemplate = new RestTemplate(factory);return restTemplate;
}/*** 创建 RestTemplate 时需要一个 ClientHttpRequestFactory,* 通过这个请求工厂,我们可以统一设置请求的超时时间,设置代理以及一些其他细节。* 通过上面代码配置后,我们直接在代码中注入 RestTemplate 就可以使用了。* @return*/
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();factory.setReadTimeout(5000);factory.setConnectTimeout(15000);// 设置代理//factory.setProxy(null);return factory;
}

2.方法介绍

getForObject

public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables)public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables)
  • 参数1:http请求路径
  • 参数2:返回值类型
  • 参数3:url参数 传参数的方式不一样而已,本质是一样的
@Resource
private RestTemplate restTemplate;@Testvoid testGetRequestForShopById() {String url="http://127.0.0.1:8081/shop/{id}";Result result = restTemplate.getForObject(url, Result.class, 1);System.out.println(result);}
# 响应结果
Result(success=true, errorMsg=null, data={id=1, name=103茶餐厅, typeId=1, images=https://qcloud.dpfile.com/pc/jiclIsCKmOI2arxKN1Uf0Hx3PucIJH8q0QSz-Z8llzcN56-_QiKuOvyio1OOxsRtFoXqu0G3iT2T27qat3WhLVEuLYk00OmSS1IdNpm8K8sG4JN9RIm2mTKcbLtc2o2vfCF2ubeXzk49OsGrXt_KYDCngOyCwZK-s3fqawWswzk.jpg,https://qcloud.dpfile.com/pc/IOf6VX3qaBgFXFVgp75w-KKJmWZjFc8GXDU8g9bQC6YGCpAmG00QbfT4vCCBj7njuzFvxlbkWx5uwqY2qcjixFEuLYk00OmSS1IdNpm8K8sG4JN9RIm2mTKcbLtc2o2vmIU_8ZGOT1OjpJmLxG6urQ.jpg, area=大关, address=金华路锦昌文华苑29, x=120.149192, y=30.316078, avgPrice=80, sold=4215, comments=3035, score=37, openHours=10:00-22:00, createTime=2021-12-22T18:10:39, updateTime=2023-04-06T22:18:52}, total=null)

getForEntity

public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables)public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables)public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType)
 	/*** 这个方法比getforobject多了一层包装。可以获取更多的响应信息*/@Testvoid testGetRequestForShopById_getForEntity() {String url="http://127.0.0.1:8081/shop/{id}";ResponseEntity<Result> response = restTemplate.getForEntity(url, Result.class,1);Result body = response.getBody();System.out.println(body);}

响应结果

Result(success=true, errorMsg=null, data={id=1, name=103茶餐厅, typeId=1, images=https://qcloud.dpfile.com/pc/jiclIsCKmOI2arxKN1Uf0Hx3PucIJH8q0QSz-Z8llzcN56-_QiKuOvyio1OOxsRtFoXqu0G3iT2T27qat3WhLVEuLYk00OmSS1IdNpm8K8sG4JN9RIm2mTKcbLtc2o2vfCF2ubeXzk49OsGrXt_KYDCngOyCwZK-s3fqawWswzk.jpg,https://qcloud.dpfile.com/pc/IOf6VX3qaBgFXFVgp75w-KKJmWZjFc8GXDU8g9bQC6YGCpAmG00QbfT4vCCBj7njuzFvxlbkWx5uwqY2qcjixFEuLYk00OmSS1IdNpm8K8sG4JN9RIm2mTKcbLtc2o2vmIU_8ZGOT1OjpJmLxG6urQ.jpg, area=大关, address=金华路锦昌文华苑29号, x=120.149192, y=30.316078, avgPrice=80, sold=4215, comments=3035, score=37, openHours=10:00-22:00, createTime=2021-12-22T18:10:39, updateTime=2023-04-06T22:18:52}, total=null)

head请求用的很少见。

Head 与服务器索与get请求一致的相应,响应体不会返回,获取包含在小消息头中的原信息(与get请求类似,返回的响应中没有具体内容,用于获取报头)

HEAD和GET本质是一样的,区别在于HEAD不含有呈现数据,而仅仅是HTTP头信息。有的人可能觉得这个方法没什么用,其实不是这样的。想象一个业务情景:欲判断某个资源是否存在,我们通常使用GET,但这里用HEAD则意义更加明确。

public HttpHeaders headForHeaders(String url, Object... uriVariables)
public HttpHeaders headForHeaders(String url, Map<String, ?> uriVariables)
public HttpHeaders headForHeaders(URI url)
@Testvoid headForHeaders() throws URISyntaxException {String url="http://127.0.0.1:8081/shop-type/list";URI uri = new URI(url);HttpHeaders headers = restTemplate.headForHeaders(uri);System.out.println(headers.toString());}

head请求的响应

[Content-Type:"application/json", Content-Length:"652", Date:"Wed, 19 Apr 2023 09:49:28 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]

在之后的内容之前我们先看看restTemplate的拦截器设置,主要设置请求头或者一些权限认证的需要


import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;import java.io.IOException;public class MyRestTemplateInterceptor implements ClientHttpRequestInterceptor {@Overridepublic ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {HttpHeaders headers = request.getHeaders();//设置请求头 做权限认证headers.set("authorization","50da33fa5c0a4ccea49a08bcdf3ee757");return execution.execute(request,body);}
}

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;import java.util.ArrayList;
import java.util.List;@Configuration
public class RestTemplateConfig {@Beanpublic RestTemplate restTemplate(ClientHttpRequestFactory factory){RestTemplate restTemplate = new RestTemplate(factory);MyRestTemplateInterceptor restTemplateInterceptor = new MyRestTemplateInterceptor();List<ClientHttpRequestInterceptor> list=new ArrayList<>(1);list.add(restTemplateInterceptor);restTemplate.setInterceptors(list);return restTemplate;}/*@Beanpublic RestTemplate restTemplate(ClientHttpRequestFactory factory){return new RestTemplate(factory);}*//*** 创建 RestTemplate 时需要一个 ClientHttpRequestFactory,* 通过这个请求工厂,我们可以统一设置请求的超时时间,设置代理以及一些其他细节。* 通过上面代码配置后,我们直接在代码中注入 RestTemplate 就可以使用了。* @return*/@Beanpublic ClientHttpRequestFactory simpleClientHttpRequestFactory(){SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();//设置超时的时间factory.setConnectTimeout(10000);//客户端从服务端读取数据的超时时间factory.setReadTimeout(10000);// 设置代理//factory.setProxy(null);return factory;}}

postForLocation方法

public URI postForLocation(String url, @Nullable Object request, Object... uriVariables)
public URI postForLocation(String url, @Nullable Object request, Map<String, ?> uriVariables)/***不做过多的介绍了* 主要是与上面的也大差不差。*/

最后我们看restTemplate中最丰富的一个方法。不用配置拦截器也能实现设置请求头。

exchange这个方法的重载也不少

public <T> ResponseEntity<T> exchange(String url, HttpMethod method,@Nullable HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables)public <T> ResponseEntity<T> exchange(String url, HttpMethod method,@Nullable HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables)
  • 参数一:url 请求路径

  • 参数二:method 请求方式

  • 参数三:requestEntity

    这个可以设置请求体和请求头,我们看一下这个类的构造器,我挑了一个参数最多的构造器。

    很明显第一个参数就是请求体,第二个参数就是设置请求头的一个map。

    也就是说我们不用设置拦截器就可以实现复杂的http请求

    public HttpEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers) {this.body = body;HttpHeaders tempHeaders = new HttpHeaders();if (headers != null) {tempHeaders.putAll(headers);}this.headers = HttpHeaders.readOnlyHttpHeaders(tempHeaders);}
  • 参数四:responseType 响应值类型

  • 参数五:url参数 也可以理解为请求行参数。

底下是一个实列。

/*** get请求设置请求头!* 设置url参数*/@Testvoid testGetRequestForShop() {long shopId = 1l;/*** authorization: ba8d06d954a04c32bc1e75e2625fe192*/String url="http://127.0.0.1:8081/shop/{id}";HttpHeaders httpHeaders=new HttpHeaders();httpHeaders.add("authorization","ba8d06d954a04c32bc1e75e2625fe192");HttpEntity httpEntity=new HttpEntity(httpHeaders);ResponseEntity<Result> result = restTemplate.exchange(url, HttpMethod.GET, httpEntity, Result.class, shopId);System.out.println(result.getStatusCode());System.out.println("============header==============");System.out.println(result.getHeaders());System.out.println("=============result==============");Object data = result.getBody().getData();System.out.println(data);}

Entity=new HttpEntity(httpHeaders);

    ResponseEntity<Result> result = restTemplate.exchange(url, HttpMethod.GET, httpEntity, Result.class, shopId);System.out.println(result.getStatusCode());System.out.println("============header==============");System.out.println(result.getHeaders());System.out.println("=============result==============");Object data = result.getBody().getData();System.out.println(data);
}

文章转载自:
http://dinncoblackmail.wbqt.cn
http://dinncoepitomist.wbqt.cn
http://dinncoricey.wbqt.cn
http://dinncoplover.wbqt.cn
http://dinncoepiphylline.wbqt.cn
http://dinncoentisol.wbqt.cn
http://dinncomisarticulation.wbqt.cn
http://dinncoprisere.wbqt.cn
http://dinncomotherfucking.wbqt.cn
http://dinncoredid.wbqt.cn
http://dinncolaxative.wbqt.cn
http://dinncoperfector.wbqt.cn
http://dinncofumulus.wbqt.cn
http://dinncovair.wbqt.cn
http://dinncojamesian.wbqt.cn
http://dinncoorientate.wbqt.cn
http://dinncotramontana.wbqt.cn
http://dinncoaroid.wbqt.cn
http://dinncodrawly.wbqt.cn
http://dinncosegu.wbqt.cn
http://dinncoclassmate.wbqt.cn
http://dinncomurkiness.wbqt.cn
http://dinncomisesteem.wbqt.cn
http://dinncoshear.wbqt.cn
http://dinncoislamite.wbqt.cn
http://dinncotamarau.wbqt.cn
http://dinncolithy.wbqt.cn
http://dinncosapotaceous.wbqt.cn
http://dinncosalubrity.wbqt.cn
http://dinncoloyally.wbqt.cn
http://dinncozygomere.wbqt.cn
http://dinncolustrous.wbqt.cn
http://dinncojocular.wbqt.cn
http://dinncoundiminishable.wbqt.cn
http://dinncobreviary.wbqt.cn
http://dinncomerciful.wbqt.cn
http://dinncostrength.wbqt.cn
http://dinncokuroshio.wbqt.cn
http://dinncoestranged.wbqt.cn
http://dinncocoercing.wbqt.cn
http://dinncosportively.wbqt.cn
http://dinnconicotinamide.wbqt.cn
http://dinncocaudad.wbqt.cn
http://dinnconeuroblastoma.wbqt.cn
http://dinncodecagynous.wbqt.cn
http://dinncomelancholic.wbqt.cn
http://dinncohaiduk.wbqt.cn
http://dinncolimby.wbqt.cn
http://dinncopetropolitics.wbqt.cn
http://dinncoagitator.wbqt.cn
http://dinncoinformercial.wbqt.cn
http://dinncoanalgesic.wbqt.cn
http://dinncoalter.wbqt.cn
http://dinncorescuable.wbqt.cn
http://dinncoaspersion.wbqt.cn
http://dinncoturfan.wbqt.cn
http://dinncohallucinosis.wbqt.cn
http://dinncoelven.wbqt.cn
http://dinncoaggregative.wbqt.cn
http://dinncocattleship.wbqt.cn
http://dinnconatterjack.wbqt.cn
http://dinncoforwarder.wbqt.cn
http://dinncospherulitize.wbqt.cn
http://dinncoefficaciously.wbqt.cn
http://dinncoptolemy.wbqt.cn
http://dinncophenocopy.wbqt.cn
http://dinncocausationism.wbqt.cn
http://dinncolocoism.wbqt.cn
http://dinncosalvar.wbqt.cn
http://dinncoswarthiness.wbqt.cn
http://dinncoencephala.wbqt.cn
http://dinncoequator.wbqt.cn
http://dinncotrunnion.wbqt.cn
http://dinncofacedown.wbqt.cn
http://dinncohandwheel.wbqt.cn
http://dinncomagnetodisk.wbqt.cn
http://dinncocics.wbqt.cn
http://dinncosequestrene.wbqt.cn
http://dinncothreshing.wbqt.cn
http://dinncodistinguished.wbqt.cn
http://dinncounevaluated.wbqt.cn
http://dinnconazarene.wbqt.cn
http://dinncoseroepidemiology.wbqt.cn
http://dinncojawbone.wbqt.cn
http://dinncoancipital.wbqt.cn
http://dinncohussitism.wbqt.cn
http://dinncosablefish.wbqt.cn
http://dinncocinchonise.wbqt.cn
http://dinncoen.wbqt.cn
http://dinncogazel.wbqt.cn
http://dinncomaintopmast.wbqt.cn
http://dinncoscribe.wbqt.cn
http://dinncohomephone.wbqt.cn
http://dinncostaminode.wbqt.cn
http://dinncoyom.wbqt.cn
http://dinncopubsy.wbqt.cn
http://dinncodissepiment.wbqt.cn
http://dinncozambo.wbqt.cn
http://dinncopulperia.wbqt.cn
http://dinncovulgarize.wbqt.cn
http://www.dinnco.com/news/121563.html

相关文章:

  • 专业外贸网站制作长沙网站制作主要公司
  • 设计建设网站哪家好制作网页设计公司
  • 网站分析流程seo网站推广助理招聘
  • 湖北大网站建设国际热点新闻
  • 如何建设网站制作平台推广专员
  • 得到做网站公司南宁关键词优化公司
  • eclipse可以做网站吗营销qq官网
  • 上海网站建设费用北京seo排名技术
  • 做一元购网站 要多少钱本周新闻热点
  • wordpress 写作seo友情链接
  • 山东省职业能力建设处网站杭州余杭区抖音seo质量高
  • 吐鲁番seo快速排名东莞百度网站排名优化
  • 什么网站必须做三级等保seo查询站长工具
  • 商城网站模版代码东莞全网营销推广
  • 个人网站备案 备注怎么把产品放到网上销售
  • 做国外网站要注意什么个人网页制作
  • 企业宣传网站建设重庆网站推广联系方式
  • 广州网站建设及推广长春网站建设技术托管
  • 做视频网站服务器要求吗网站seo优化技能
  • 电白网站建设公司教育机构培训
  • 在哪里推广网站网站入口
  • 网站后期维护内容本地推广平台
  • 无锡网站开发公司电话黄冈网站推广策略
  • 外贸建站用什么服务器人工智能教育培训机构排名
  • 优秀专题网站怎么做平台推广
  • 个人网站建设作用免费的网页入口
  • 网站简繁体转换.rar邵阳做网站的公司
  • 做破解软件网站赚广告费竞价恶意点击犯法吗
  • 深圳建站公司品牌网站建设seo网站优化报价
  • 棋牌游戏在哪做网站专业营销策划团队