中小型网站建设新闻,镭拓网站建设,广德县住房和城乡建设网站,深圳seo网站优化公司2019独角兽企业重金招聘Python工程师标准 一、Ribbon简介 Ribbon是Netflix发布的开源项目#xff0c;主要功能是提供客户端的软件负载均衡算法#xff0c;将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时#xff0c;重试… 2019独角兽企业重金招聘Python工程师标准 一、Ribbon简介 Ribbon是Netflix发布的开源项目主要功能是提供客户端的软件负载均衡算法将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时重试等。简单的说就是在配置文件中列出Load Balancer后面所有的机器Ribbon会自动的帮助你基于某种规则如简单轮询随即连接等去连接这些机器。我们也很容易使用Ribbon实现自定义的负载均衡算法。 二、准备工作 这一篇文章基于上一篇文章的工程启动eureka-server 工程启动user-service工程它的端口为8084将user-service的项目复制出来一个将其名称修改为user-service1配置文件的端口改为8085. 启动这时你会发现user-service在eureka-server注册了2个实例这就相当于一个小的集群。访问localhost:1001如图所示 三、建一个服务消费者Ribbon 重新新建一个工程取名为da-ribbon-service; 在它的pom.xml文件分别引入起步依赖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.example/groupIdartifactIdda-ribbon-server/artifactIdversion0.0.1-SNAPSHOT/versionpackagingjar/packagingnameda-ribbon-server/namedescriptionDemo project for Spring Boot/descriptionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.0.0.RELEASE/versionrelativePath/ !-- lookup parent from repository --/parentpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingproject.reporting.outputEncodingUTF-8/project.reporting.outputEncodingjava.version1.8/java.versionspring-cloud.versionFinchley.M8/spring-cloud.version/propertiesdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-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/artifactIdversion${spring-cloud.version}/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:1001/eureka/程序名称为 ribbon-service程序端口为2001。配置文件application.yml如下 eureka:client:serviceUrl:defaultZone: http://localhost:1001/eureka/instance:lease-renewal-interval-in-seconds: 30
server:port: 2001
spring:application:name: ribbon-service 在工程的启动类中,通过EnableEurekaClient向服务中心注册并且向程序的ioc注入一个bean: restTemplate;并通过LoadBalanced注解表明这个restRemplate开启负载均衡的功能。 EnableEurekaClient
SpringBootApplication
public class DaRibbonServerApplication {public static void main(String[] args) {SpringApplication.run(DaRibbonServerApplication.class, args);}BeanLoadBalancedRestTemplate restTemplate(){return restTemplate();}
} 写一个测试类HelloService通过之前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口在这里我们直接用的程序名替代了具体的url地址在ribbon中它会根据服务名来选择具体的服务实例根据服务实例在请求的时候会用具体的url替换掉服务名代码如下 Service
public class HelloService {AutowiredRestTemplate restTemplate;public String getHello(String name){return restTemplate.getForObject(http://user-service/hi?namename,String.class);}
} 写一个controller在controller中用调用HelloService 的方法代码如下 RestController
public class HiController {AutowiredHelloService helloService;RequestMapping(/hi)public String hi(RequestParam String name){return helloService.getHello(name);}
} 在浏览器上多次访问http://localhost:2001/hi?namezhangsan浏览器交替显示 这说明当我们通过调用restTemplate.getForObject(http://user-service/hi?namename,String.class)方法时已经做了负载均衡访问了不同的端口的服务实例。 四、此时的架构 一个服务注册中心eureka-server,端口为1001user-service跑了两个工程实例端口分别为8084,8085分别向服务注册中心注册ribbon-sercvice端口为2001,向服务注册中心注册当ribbon-sercvice通过restTemplate调用user-service的hi接口时因为用ribbon进行了负载均衡会轮流的调用user-service8084和8085 两个端口的hi接口 五、源码下载 RibbonServer源码下载https://gitee.com/Clarences/Ribbon-Service UserServer1源码下载https://gitee.com/Clarences/User-server1 转载于:https://my.oschina.net/Clarences/blog/1635795