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

上海轨道交通建设查询网站万网上买了域名怎么建设网站

上海轨道交通建设查询网站,万网上买了域名怎么建设网站,软件开发流程包括,莒县网页定制eureka注册中心和restTemplate的使用说明 eureka的作用 消费者该如何获取服务提供者的具体信息 1.服务者启动时向eureka注册自己的信息 2.eureka保存这些信息 3.消费者根据服务名称向eureka拉去提供者的信息 如果有多个服务提供者#xff0c;消费者该如何选择#xff1f; 服…eureka注册中心和restTemplate的使用说明 eureka的作用 消费者该如何获取服务提供者的具体信息 1.服务者启动时向eureka注册自己的信息 2.eureka保存这些信息 3.消费者根据服务名称向eureka拉去提供者的信息 如果有多个服务提供者消费者该如何选择 服务消费者利用负载均衡算法从服务列表中挑选一个 消费者如何感知服务提供者的健康状态 服务提供者每隔30s向eurekaserver发送心跳请求报告健康状态。 eureka会更新记录服务列表信息心跳不正常会被剔除。 消费者就可以拉取到最新的消息。 搭载eureka注册中心 1.引入依赖 dependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-server/artifactId/dependency/dependencies2.在启动类加EnableEurekaServer注解 3.编写配置文件 server:port: 10086 #端口spring:application:name: eurekaserver #应用名eureka:client:service-url: #eureka注册中心地址defaultZone: http://localhost:10086/eureka服务注册 1.加入eureka客服端的依赖 dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependency2.添加客服端的配置 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 urlhttp://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);}BeanLoadBalancedpublic 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, ClassT responseType, Object... uriVariables)public T T getForObject(String url, ClassT responseType, MapString, ? uriVariables)参数1http请求路径参数2返回值类型参数3url参数 传参数的方式不一样而已本质是一样的 Resource private RestTemplate restTemplate;Testvoid testGetRequestForShopById() {String urlhttp://127.0.0.1:8081/shop/{id};Result result restTemplate.getForObject(url, Result.class, 1);System.out.println(result);}# 响应结果 Result(successtrue, errorMsgnull, data{id1, name103茶餐厅, typeId1, imageshttps://qcloud.dpfile.com/pc/jiclIsCKmOI2arxKN1Uf0Hx3PucIJH8q0QSz-Z8llzcN56-_QiKuOvyio1OOxsRtFoXqu0G3iT2T27qat3WhLVEuLYk00OmSS1IdNpm8K8sG4JN9RIm2mTKcbLtc2o2vfCF2ubeXzk49OsGrXt_KYDCngOyCwZK-s3fqawWswzk.jpg,https://qcloud.dpfile.com/pc/IOf6VX3qaBgFXFVgp75w-KKJmWZjFc8GXDU8g9bQC6YGCpAmG00QbfT4vCCBj7njuzFvxlbkWx5uwqY2qcjixFEuLYk00OmSS1IdNpm8K8sG4JN9RIm2mTKcbLtc2o2vmIU_8ZGOT1OjpJmLxG6urQ.jpg, area大关, address金华路锦昌文华苑29号, x120.149192, y30.316078, avgPrice80, sold4215, comments3035, score37, openHours10:00-22:00, createTime2021-12-22T18:10:39, updateTime2023-04-06T22:18:52}, totalnull) getForEntity public T ResponseEntityT getForEntity(String url, ClassT responseType, Object... uriVariables)public T ResponseEntityT getForEntity(String url, ClassT responseType, MapString, ? uriVariables)public T ResponseEntityT getForEntity(URI url, ClassT responseType) /*** 这个方法比getforobject多了一层包装。可以获取更多的响应信息*/Testvoid testGetRequestForShopById_getForEntity() {String urlhttp://127.0.0.1:8081/shop/{id};ResponseEntityResult response restTemplate.getForEntity(url, Result.class,1);Result body response.getBody();System.out.println(body);}响应结果 Result(successtrue, errorMsgnull, data{id1, name103茶餐厅, typeId1, imageshttps://qcloud.dpfile.com/pc/jiclIsCKmOI2arxKN1Uf0Hx3PucIJH8q0QSz-Z8llzcN56-_QiKuOvyio1OOxsRtFoXqu0G3iT2T27qat3WhLVEuLYk00OmSS1IdNpm8K8sG4JN9RIm2mTKcbLtc2o2vfCF2ubeXzk49OsGrXt_KYDCngOyCwZK-s3fqawWswzk.jpg,https://qcloud.dpfile.com/pc/IOf6VX3qaBgFXFVgp75w-KKJmWZjFc8GXDU8g9bQC6YGCpAmG00QbfT4vCCBj7njuzFvxlbkWx5uwqY2qcjixFEuLYk00OmSS1IdNpm8K8sG4JN9RIm2mTKcbLtc2o2vmIU_8ZGOT1OjpJmLxG6urQ.jpg, area大关, address金华路锦昌文华苑29号, x120.149192, y30.316078, avgPrice80, sold4215, comments3035, score37, openHours10:00-22:00, createTime2021-12-22T18:10:39, updateTime2023-04-06T22:18:52}, totalnull) head请求用的很少见。 Head 与服务器索与get请求一致的相应响应体不会返回获取包含在小消息头中的原信息与get请求类似返回的响应中没有具体内容用于获取报头 HEAD和GET本质是一样的区别在于HEAD不含有呈现数据而仅仅是HTTP头信息。有的人可能觉得这个方法没什么用其实不是这样的。想象一个业务情景欲判断某个资源是否存在我们通常使用GET但这里用HEAD则意义更加明确。 public HttpHeaders headForHeaders(String url, Object... uriVariables) public HttpHeaders headForHeaders(String url, MapString, ? uriVariables) public HttpHeaders headForHeaders(URI url)Testvoid headForHeaders() throws URISyntaxException {String urlhttp://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:timeout60, 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();ListClientHttpRequestInterceptor listnew 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, MapString, ? uriVariables)/***不做过多的介绍了* 主要是与上面的也大差不差。*/最后我们看restTemplate中最丰富的一个方法。不用配置拦截器也能实现设置请求头。 exchange这个方法的重载也不少 public T ResponseEntityT exchange(String url, HttpMethod method,Nullable HttpEntity? requestEntity, ClassT responseType, Object... uriVariables)public T ResponseEntityT exchange(String url, HttpMethod method,Nullable HttpEntity? requestEntity, ClassT responseType, MapString, ? uriVariables)参数一url 请求路径 参数二method 请求方式 参数三requestEntity 这个可以设置请求体和请求头我们看一下这个类的构造器,我挑了一个参数最多的构造器。 很明显第一个参数就是请求体第二个参数就是设置请求头的一个map。 也就是说我们不用设置拦截器就可以实现复杂的http请求 public HttpEntity(Nullable T body, Nullable MultiValueMapString, 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 urlhttp://127.0.0.1:8081/shop/{id};HttpHeaders httpHeadersnew HttpHeaders();httpHeaders.add(authorization,ba8d06d954a04c32bc1e75e2625fe192);HttpEntity httpEntitynew HttpEntity(httpHeaders);ResponseEntityResult 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);}Entitynew HttpEntity(httpHeaders); ResponseEntityResult 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://www.w-s-a.com/news/583718/

相关文章:

  • 广州做公司网站网站开发培训机构
  • 网站建设与维护 课件网页版qq安全中心登录入口
  • 做三个月网站广告收入dw如何制作网页
  • ...课程网站建设简介工信部 网站备案查询
  • 网站代码建设 实例企业网站建设大概的费用
  • 制作网站软件排行榜过年做啥网站致富
  • 哪里有做网站企业seo关键词优化
  • 上海金山网站建设公司手机淘宝客网站怎么做的
  • 网站开发需要公司做网站费用计入什么科目
  • 网站优化有哪些类型免费制作app的傻瓜软件
  • 如何做网站咨询wordpress get
  • 企业网站建设网站做网站用别人的图片
  • 站长统计代码个人网站源代码
  • 求推荐专门做借条的网站公众号排版编辑器
  • 动态做网站网站开发语言查询 蔡学镛
  • 莆田网站建设创意自助建站英文
  • cms系统创建静态网站龙岗网站建设哪家好
  • 自己做的网站被封了邢台规划局网站建设
  • 网站建设项目合同wordpress主题没法用
  • 个旧市哪里有做网站wordpress内页php页面
  • 程序员接活的平台网站互联网平台建设方案
  • 网站安全建设模板深圳企业管理咨询公司
  • 做网站 还是淘宝店wordpress分类链接后加
  • wordpress腾讯云 COSseo内容优化心得
  • 特价旅游机票网站建设i营销
  • 如何成立网站深圳创业项目
  • 建设商业网站惠州网站建设推荐乐云seo
  • 如何申请免费域名做网站免费推广神器
  • 自媒体人专用网站安岳网站建设
  • 特乐网站建设做网站推广要多少钱