本文共 8800 字,大约阅读时间需要 29 分钟。
距上次写东西已经一个月多了,今个体验了把spring cloud的ribbon(ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。)的负责均衡功能,感觉还行,因此记一笔
都是简单的测试,贴上原理图
首先向服务中心注册一个Eureka server服务和两个Eureka client服务(做两个client是为了轮询,验证ribbon的负责均衡功能),
再注册一个服务消费者,如下图
注册成功可以在服务中心看到
通过ribbon可以看到请求轮询到两个Eureka client,如图
具体创建过程
创建一个springboot工程,然后创建三个module,分别为一个server和两个client
server的pom:
配置文件:4.0.0 com.example eurekaserver 0.0.1-SNAPSHOT jar eurekaserver 服务注册中心 org.springframework.boot spring-boot-starter-parent 1.5.8.RELEASE UTF-8 UTF-8 1.8 Dalston.SR4 org.springframework.cloud spring-cloud-starter-eureka-server org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone false
server.port=8761eureka.instance.hostname=localhosteureka.client.register-with-eureka=falseeureka.client.fetch-registry=falseeureka.client.eureka-server-u-r-l-context=http://${eureka.instance.hostname}:${server.port}/eureka/
package com.example.eurekaserver;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer//向注册中心注册服务@SpringBootApplicationpublic class EurekaserverApplication { public static void main(String[] args) { SpringApplication.run(EurekaserverApplication.class, args); }}两个client除了端口的基本一样:
配置文件:4.0.0 com.example eurekaclient_1 0.0.1-SNAPSHOT jar eurekaclient_1 Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.8.RELEASE UTF-8 UTF-8 1.8 Dalston.SR4 org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone false
server.port=8762eureka.client.eureka-server-u-r-l-context=http://localhost:8761/eureka/spring.application.name=service-hi
server.port=8763eureka.client.eureka-server-u-r-l-context=http://localhost:8761/eureka/spring.application.name=service-hi
package com.example.eurekaclient_1;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@EnableEurekaClient@RestControllerpublic class Eurekaclient1Application { public static void main(String[] args) { SpringApplication.run(Eurekaclient1Application.class, args); } @Value("${server.port}") String port; @RequestMapping("/hi") public String home(@RequestParam String name){ return "hi"+name+",welcome here!"+port; }}服务消费者
pom:
配置:4.0.0 com.example service-ribbon 0.0.1-SNAPSHOT jar service-ribbon Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.8.RELEASE UTF-8 UTF-8 1.8 Dalston.SR4 org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-ribbon org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone false
server.port=8764eureka.client.eureka-server-u-r-l-context=http://localhost:8761/eureka/spring.application.name=server-ribbon
package com.example.serviceribbon;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); } @Bean @LoadBalanced//表明restTemplate开启负载均衡功能 RestTemplate restTemplate(){ return new RestTemplate(); }}开始测试:
package com.example.serviceribbon.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;@Servicepublic class HelloService { @Autowired RestTemplate restTemplate; public String hiService(String name){ return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class); }}
package com.example.serviceribbon.controller;import com.example.serviceribbon.service.HelloService;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;@RestControllerpublic class HelloController { @Autowired HelloService helloService; @RequestMapping(value="/hi") public String hi(@RequestParam String name){ return helloService.hiService(name); }}注意,这个使用到@RestController的类必须在@SpringBootpplication类的同包或子包下,否则,无法加载!
转载地址:http://aftti.baihongyu.com/