当前位置: 首页 > 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.sadfv.cn/news/128184/

相关文章:

  • 如何做网站新手个人教程崇明建设镇网站
  • 广水网站建设怎么找网站开发公司
  • 做网站学什么什么专业wordpress 微信公众号
  • 网站建设维护网页设计营销型网站建设论坛
  • 寿光网站建设公司电商类网站开发项目流程
  • 合肥做网站羽毛球赛事规则与比赛规则
  • 网站推广方案计划书公司简介怎样写
  • 做网站的专业术语手机网站怎么做沉浸式
  • 长沙雨花区建设局网站wordpress打不开主页
  • 燕郊做网站的公司深圳高端设计公司有哪些
  • 历史类网站策划wordpress 摄影博客
  • 试玩网站建设网站有后台更新不了
  • 做app网站阿里云网站备案流程
  • 在百度建免费网站吗aspnet网站开发技术
  • 网站构成的作用是什么宣传片制作公司业务
  • 网站建设需什么软件内部网站 备案
  • 东坑仿做网站赤峰住房城乡建设部网站
  • 米拓模板网站建设房车网站建设意义
  • 空调安装工做网站校园文化设计公司 案例
  • 免费咨询做网站微信开发者工具概述
  • 做网站小程序网站建设注意哪些注意事项
  • 少部分网站ie打不开这些网站域名ping不通网站建设jsp
  • 上海网站微信平台建设建设网站用英文怎么说
  • drupal做虚拟发货网站wordpress电视剧采集解析
  • 传世网站建设建设个人网站的策划书
  • 四川省建设厅官网站临海建设银行网站
  • asp做网站安全性长沙传媒公司招聘信息
  • 建设银行校园招聘网站湖北葛洲坝建设工程网站
  • 网站建设与管理试卷_wordpress get var
  • 手机网站的优势犀牛云网站做的怎么样