在微服务架构中,业务都会被拆分红一个独立的服务,服务与服务的通信是基于http restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另外一种是feignhtml
本片博客以上一篇博客 玩转SpringCloud 一.服务的注册与发现(Eureka) 的项目为基础 https://www.cnblogs.com/lsy131479/p/9613755.html web
本片博客将讲解ribbon+restTemplate模式,下一篇讲解feign模式spring
ribbon是一个负载均衡客户端,能够很好的控制htt和tcp的一些行为。浏览器
启动demo1 工程;启动demo2工程,它的端口为8762;将demo2的配置文件的端口改成8763,并启动,会发现:demo2在demo1 注册了2个实例,这就至关于一个小的集群。restful
启动以前先将demo2的启动设置单例关掉架构
项目启动后而且关掉单例启动后,改变demo2的端口号app
再次启动demo2,查看注册中心的服务http://localhost:8761负载均衡
会发现:demo2在demo1 注册了2个实例,这就至关于一个小的集群。框架
项目架构:tcp
从新新建一个spring-boot工程,取名为:demo3;
引入主项目,以及相关jar包:
<parent> <groupId>com.fsdm</groupId> <artifactId>SpringCloud_test1</artifactId> <version>1.0-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> </dependencies>
yml配置:
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8764 spring: application: #工程名称 name: service-ribbon
在工程的启动类中,经过@EnableDiscoveryClient向服务中心注册;而且向程序的ioc注入一个bean: restTemplate;并经过@LoadBalanced注解代表这个restRemplate开启负载均衡的功能。
@EnableEurekaClient @EnableDiscoveryClient @SpringBootApplication public class Demo3Application { public static void main(String[] args) { SpringApplication.run(Demo3Application.class, args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
注解解析:
@EnableDiscoveryClient
1. 基于spring-cloud-commons,而且在classpath中实现。
2. 就是若是选用的注册中心是eureka推荐@EnableEurekaClient,若是是其余的注册中心推荐使用@EnableDiscoveryClient,若是classpath中添加了eureka,则它们的做用是同样的。
@Bean
一、Java面向对象,对象有方法和属性,那么就须要对象实例来调用方法和属性(即实例化);
二、凡有方法或属性的类都须要实例化,这样才能具象化去使用这些方法和属性;
三、规律:凡是子类及带有方法或属性的类都要加上注册Bean到Spring IoC的注解;
四、把Bean理解为类的代理或代言人(实际上确实是经过反射、代理来实现的),这样它就能表明类拥有该拥有的东西了
五、咱们都在微博上@过某某,对方会优先看到这条信息,并给你反馈,那么在Spring中,你标识一个@符号,那么Spring就会来看看,而且从这里拿到一个Bean或者给出一个Bean
@LoadBalanced
1. 代表这个restRemplate开启负载均衡的功能。
2. 实现负载均衡
写一个测试类HelloService,经过以前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口,在这里咱们直接用的程序名替代了具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名.
public class HelloService { @Autowired RestTemplate restTemplate; public String hiService(String name) { return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class); } }
写一个controller,在controller中用调用HelloService 的方法
@RestController public class HelloControler { @Autowired HelloService helloService; @GetMapping(value = "/hi") public String hi(@RequestParam String name) { return helloService.hiService( name ); } }
启动demo3工程
运行工程状况:
(demo2双启动)
在浏览器上屡次访问http://localhost:8764/hi?name=forezp,浏览器交替执行显示:
这说明当咱们经过调用restTemplate.getForObject(“http://SERVICE-HI/hi?name=“+name,String.class)方法时,已经作了负载均衡,访问了不一样的端口的服务实例。
此时的架构:(网摘)
· 一个服务注册中心,eureka server,端口为8761
· service-hi工程跑了两个实例,端口分别为8762,8763,分别向服务注册中心注册
· sercvice-ribbon端口为8764,向服务注册中心注册
· 当sercvice-ribbon经过restTemplate调用service-hi的hi接口时,由于用ribbon进行了负载均衡,会轮流的调用service-hi:8762和8763 两个端口的hi接口;
未完,待续。。。