SpringCloud 服務消費者-rest+ribbon
spring cloud有兩種服務呼叫方式,一種是ribbon+restTemplate,另一種是feign.
ribbon
ribbon是一個負載均衡客戶端,Feign預設集成了ribbon。ribbon已經預設實現瞭如下配置bean:
(1) IClientConfig ribbonClientConfig: DefaultClientConfigImpl
(2)IRule ribbonRule: ZoneAvoidanceRule
(3)IPing ribbonPing: NoOpPing
(4)ServerList ribbonServerList: ConfigurationBasedServerList
(5)ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter
(6)ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer
實戰
基於前上篇文章,啟動eureka-server工程,啟動server-hi工程,它的埠為8762,將server-hi的配置檔案的埠改為8763,再啟動一個server-hi。
1、如何在idea啟動多個spring boot工程例項
(1)在idea點選Application的右下角,彈出選項後,點選Edit Configuration.
(2)開啟配置後,將預設的Single instance only的鉤去掉。
2、建一個服務消費者
重新新建一個spring-boot工程,取名為:service-ribbon。
在它的pom.xml繼承父pom檔案,並引入以下依賴。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.forezp</groupId> <artifactId>service-ribbon</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service-ribbon</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.forezp</groupId> <artifactId>sc-f-chapter2</artifactId> <version>0.0.1-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> </project>
在工程的配置檔案指定服務的註冊中心地址為http://localhost:8761/eureka/,程式名稱為service-ribbon,程式埠為8764.
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: service-ribbon
在工程的啟動類中,通過@EnableDiscoveryClient向服務中心註冊,並且向程式的ioc注入一個bean:restTemplate,並通過@LoadBalanced註解表明這個restTemplate開啟負載均衡的功能。
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run( ServiceRibbonApplication.class, args );
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
寫一個測試類HelloService,通過之前注入ioc容器的restTemplate來消費service-hi服務的“/hi”介面,在這裡我們直接用程式名稱代替具體的url地址,在ribbon中它會根據服務名來選擇具體的服務例項。
@Service
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 );
}
}
在瀏覽器上多次訪問http://localhost:8764/hi?name=xxx