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

白城市网站建设印度购物网站排名

白城市网站建设,印度购物网站排名,网站搭建品牌,外贸人常用网站在上一篇文章#xff0c;讲了服务的注册和发现。在微服务架构中#xff0c;业务都会被拆分成一个独立的服务#xff0c;服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式#xff0c;一种是ribbonrestTemplate#xff0c;另一种是feign。在这一篇文章…在上一篇文章讲了服务的注册和发现。在微服务架构中业务都会被拆分成一个独立的服务服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式一种是ribbonrestTemplate另一种是feign。在这一篇文章首先讲解下基于ribbonrest。 一、ribbon简介 Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using FeignClient then this section also applies. -----摘自官网 ribbon是一个负载均衡客户端可以很好的控制http和tcp的一些行为。Feign默认集成了ribbon。 ribbon 已经默认实现了这些配置bean IClientConfig ribbonClientConfig: DefaultClientConfigImpl IRule ribbonRule: ZoneAvoidanceRule IPing ribbonPing: NoOpPing ServerList ribbonServerList: ConfigurationBasedServerList ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer 二、准备工作 这一篇文章基于上一篇文章的工程启动eureka-server 工程启动sale-service工程它的端口为8762将sale-service的代码复制一份工程取名为sale-service2的配置文件的端口改为8763,并启动 这时你会发现sale-service在eureka-server注册了2个实例这就相当于一个小的集群。访问localhost:8761如图所示 如何一个工程启动多个实例请自行搜资料解决。 三、建一个服务消费者 重新新建一个spring-boot工程取名为service-ribbon; 在它的pom.xml文件分别引入起步依赖spring-cloud-starter-eureka、spring-cloud-starter-ribbon、spring-boot-starter-web代码如下 ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.hmblogs/groupIdartifactIdservice-ribbon/artifactIdversion0.0.1-SNAPSHOT/versionpackagingjar/packagingnameservice-ribbon/namedescriptionDemo project for Spring Boot/descriptionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion1.5.2.RELEASE/versionrelativePath/ !-- lookup parent from repository --/parentpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingproject.reporting.outputEncodingUTF-8/project.reporting.outputEncodingjava.version1.8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-eureka/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-ribbon/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversionDalston.RC1/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/buildrepositoriesrepositoryidspring-milestones/idnameSpring Milestones/nameurlhttps://repo.spring.io/milestone/urlsnapshotsenabledfalse/enabled/snapshots/repository/repositories/project 在工程的配置文件指定服务的注册中心地址为http://localhost:8761/eureka/程序名称为 service-ribbon程序端口为8764。配置文件application.yml如下 eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/ server:port: 8764 spring:application:name: service-ribbon 在工程的启动类中,通过EnableDiscoveryClient向服务中心注册并且向程序的ioc注入一个bean: restTemplate;并通过LoadBalanced注解表明这个restRemplate开启负载均衡的功能。 package com.hmblogs;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate;SpringBootApplication EnableDiscoveryClient public class ServiceRibbonApplication {public static void main(String[] args) {SpringApplication.run(ServiceRibbonApplication.class, args);}BeanLoadBalancedRestTemplate restTemplate() {return new RestTemplate();}}写一个测试类HelloService通过之前注入ioc容器的restTemplate来消费sale-service服务的“/hi”接口在这里我们直接用的程序名替代了具体的url地址在ribbon中它会根据服务名来选择具体的服务实例根据服务实例在请求的时候会用具体的url替换掉服务名代码如下 package com.hmblogs;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate;Service public class HelloService {AutowiredRestTemplate restTemplate;public String hiService(String name) {return restTemplate.getForObject(http://sale-service/hi?namename,String.class);}}写一个controller在controller中调用HelloService 的方法代码如下 package com.hmblogs;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;RestController public class HelloControler {AutowiredHelloService helloService;RequestMapping(value /invokeHi)public String hi(RequestParam String name){return helloService.hiService(name);}}在浏览器上多次访问http://localhost:8764/invokeHi?nameforezp浏览器交替显示 hi forezp,i am from port:8762 hi forezp,i am from port:8763 这说明当我们通过调用restTemplate.getForObject(“http://sale-service/hi?name”name,String.class)方法时已经做了负载均衡访问了不同的端口的服务实例。 代码结构如下图 ​​​​​​​ 四、此时的架构 一个服务注册中心eureka server,端口为8761 sale-service工程跑了两个实例端口分别为8762,8763分别向服务注册中心注册 sercvice-ribbon端口为8764,向服务注册中心注册 当sercvice-ribbon通过restTemplate调用sale-service的hi接口时因为用ribbon进行了负载均衡会轮流的调用sale-service8762和8763 两个端口的hi接口
http://www.huolong8.cn/news/128184/

相关文章:

  • 公司自己做网站备案做的网站打不开
  • 南京最好的网站设计公司六安建设厅网站
  • 沈阳市网站制作公司做外贸上哪些网站
  • 南京的网站建设公司哪家好php 做网站
  • 温州网站建设设计公司孝感58同城网站建设
  • 中卫网站设计公司排名做网站的公司都很小吗
  • 专业免费网站建设一般多少钱互联网产品设计
  • 网站建设gzzctyi自己用电脑网站建设
  • 网站广告形式网站设计费用一览表
  • 有的域名怎样做网站阿里云腾讯云网站建设
  • 网站建站管理系统广州seo网站推广费用
  • 阳东网站seo开小程序要多少钱
  • 做免费漫画网站有风险吗百度指数有什么参考意义
  • 网页策划书 网站建设定位wordpress和wiki
  • 国外免费网站域名服务器查询网站制作公司 全贵州
  • 公众号链接网站都是怎么做的网站模板制作与安装教程视频
  • 个人怎么做影视网站广州企业网站找哪里
  • 做企业网站的哪家好房地产客户管理系统有哪些
  • 网站开发与兼容模式怎么把网站推广出去
  • 建设网站方面的证书做网站用什么语言开发
  • 营销型网站建设ppt模板下载wordpress 亲子主题
  • 网站建设服务器篇邯郸整站优化
  • 网站开发入门mvc网站入口asp
  • 可以做网站的网络北京建设网站设计
  • 成都网站建设公司 四川冠辰科技网站建设与管理 pdf
  • 教学网站模板下载什么是网站链接优化
  • 巩义网站河北省住房和城乡建设厅信用网站
  • 家政网站制作网站建设公司客户开发手册
  • 外贸网站平台排行榜专科最吃香的十大专业
  • 湘潭学校网站建设 磐石网络专注北京建设工程主管部门网站