微服務會把一個單個大項目拆分紅多個獨立的小服務,這些小服務之間的調用採用的是http restful,spring cloud提供了ribbon+restTemplate 。ribbon是一個負載均衡的客戶端。java
1.首先接着上篇博客的服務,啓動eureka-server工程,啓動eureka-client-say-hi工程,它的端口爲8792,而後更改端口由8792-》8793,並啓動,發現註冊中心8792爲Down了web
此時點擊EditConfiguration 將Single instance only前面對勾去掉;而後啓動兩個實例spring
此時發現eureka-server註冊了2個實例,這就至關於一個小的集羣。restful
2.新建一個服務消費者app
build.gradle文件負載均衡
buildscript { ext { springBootVersion = '2.0.4.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } ext { springCloudVersion = 'Finchley.SR1' } dependencies { compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client') compile('org.springframework.cloud:spring-cloud-starter-netflix-ribbon') testCompile('org.springframework.boot:spring-boot-starter-test') } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
application.yml文件eclipse
server: port: 8794 spring: application: name: service-ribbon eureka: client: service-url: defaultZone: http://localhost:8791/eureka/
主方法maven
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.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @EnableDiscoveryClient //向服務中心註冊 @EnableEurekaClient @SpringBootApplication public class ServiceRibbonApplication { public static void main(String[] args) { SpringApplication.run(ServiceRibbonApplication.class, args); } @Bean //注入一個Bean @LoadBalanced //代表該bean具備負載均衡的功能 RestTemplate restTemplate() { return new RestTemplate(); } }
HelloService.javaspring-boot
package com.example.serviceribbon; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class HelloService { @Autowired RestTemplate restTemplate; //經過以前注入ioc容器的restTemplate來消費eureka-client-say-hi服務的「/hi」接口,在這裏咱們直接用的程序名替代了具體的url地址,在ribbon中它會根據服務名來選擇具體的服務實例,根據服務實例在請求的時候會用具體的url替換掉服務名: public String hiService() { return restTemplate.getForObject("http://EUREKA-CLIENT-SAY-HI/hi", String.class); } }
HelloController.java微服務
package com.example.serviceribbon; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Autowired HelloService helloService; @RequestMapping("/hi") public String sayHi() { return helloService.hiService(); } }
此時啓動該項目service-ribbon,訪問http://localhost:/hi,出現如下兩種狀況
此時的註冊中心:
以上狀況進行解讀:
一個服務註冊中心,eureka-server 端口爲8791
eureka-client-say-hi項目跑了兩個實例,端口分別爲8792和8793分別向服務註冊中心註冊;
service-ribbon端口爲8794,向服務註冊中心註冊;
當service-ribbon經過restTemplate調用eureka-client-say-hi的接口時,由於ribbon進行了負載均衡,會輪流調用eureka-client-say-hi,8792,和8793兩個實例的hi接口。