概述
在前面的文章中愕秫,我們講了使用Eureka
作為服務(wù)注冊中心宣决,在服務(wù)啟動(dòng)后,各個(gè)微服務(wù)會(huì)將自己注冊到Eureka server
月弛。那么服務(wù)之間是如何調(diào)用?又是如何進(jìn)行負(fù)載均衡的呢科盛?本文講講服務(wù)之間調(diào)用及負(fù)載均衡Ribbon帽衙。
目前,在Spring cloud 中服務(wù)之間通過restful方式調(diào)用有兩種方式
- restTemplate+Ribbon
- feign
從實(shí)踐上看贞绵,采用feign的方式更優(yōu)雅(feign內(nèi)部也使用了ribbon做負(fù)載均衡)厉萝。
本文使用如下的項(xiàng)目實(shí)例來分別演示這兩種寫法。
-
hello
服務(wù),讀取數(shù)據(jù)庫谴垫,返回信息 -
world
服務(wù)章母,返回信息 -
helloworld
服務(wù),調(diào)用了hello
服務(wù)和world
服務(wù)(restTemplate+Ribbon方式) -
helloworldfeign
服務(wù)翩剪,調(diào)用了hello
服務(wù)和world
服務(wù)(feign方式)
本文項(xiàng)目代碼:springcloud-demo
如何理解客戶端Ribbon
zuul也有負(fù)載均衡的功能乳怎,它是針對外部請求做負(fù)載,那客戶端ribbon的負(fù)載均衡又是怎么一回事前弯?
客戶端ribbon的負(fù)載均衡蚪缀,解決的是服務(wù)發(fā)起方(在Eureka注冊的服務(wù))對被調(diào)用的服務(wù)的負(fù)載,比如我們查詢商品服務(wù)要調(diào)用顯示庫存和商品明細(xì)服務(wù)博杖,通過商品服務(wù)的接口將兩個(gè)服務(wù)組合椿胯,可以減少外部應(yīng)用的請求筷登,比如手機(jī)App發(fā)起一次請求即可剃根,可以節(jié)省網(wǎng)絡(luò)帶寬,也更省電前方。
ribbon是對服務(wù)之間調(diào)用做負(fù)載狈醉,是服務(wù)之間的負(fù)載均衡,zuul是可以對外部請求做負(fù)載均衡惠险。
hello服務(wù)與world服務(wù)
hello服務(wù)
Git項(xiàng)目代碼:hello
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--connect the db-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.5.4.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.5.6.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
啟動(dòng)類HelloApplication:
package com.example.hello;
import com.example.hello.model.Info;
import com.example.hello.repository.InfoRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
@EnableDiscoveryClient
@SpringBootApplication
public class HelloApplication {
private static final Logger log= LoggerFactory.getLogger(HelloApplication.class);
public static final String KEY="Database";
public static final String VALUE="MYSQL FROM BILLJIANG";
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
@Bean
public CommandLineRunner initDatabase(InfoRepository repository){
return (args) -> {
repository.save(new Info(KEY,VALUE));
};
}
}
入口類HelloController:
package com.example.hello.controller;
import com.example.hello.HelloApplication;
import com.example.hello.model.HelloMessage;
import com.example.hello.model.Info;
import com.example.hello.repository.InfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class HelloController {
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private InfoRepository infoRepository;
@GetMapping("/")
public String home(){
return "hello";
}
@GetMapping("/message")
public HelloMessage getMessage() {
HelloMessage helloMessage = new HelloMessage();
helloMessage.setName(getLocalInstanceInfo());
helloMessage.setMessage(getInfoFromDatabase());
return helloMessage;
}
private String getLocalInstanceInfo() {
ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance();
return serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort();
}
private String getInfoFromDatabase() {
List<Info> infoList = infoRepository.findByName(HelloApplication.KEY);
for (Info info : infoList) {
return info.toString();
}
return "(no database info)";
}
}
配置文件application.yml:
#隨機(jī)端口
server:
port: 0
#服務(wù)注冊中心
eureka:
client:
service-url:
default-zone: http://localhost:8761/eureka
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
#數(shù)據(jù)源
spring:
application:
name: hello
datasource:
url: jdbc:mysql://127.0.0.1:3306/${{MYSQL_DATABASE}:foodb}
username: ${{MYSQL_USERNAME}:root}
password: ${{MYSQL_PASSWORD}:billjiang}
testWhileIdle: true
validationQuery: SELECT 1
jpa:
show-sql: true
hibernate:
ddl-auto: update
naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
zipkin:
base-url: http://127.0.0.1:9411
其他類略苗傅,請參照源碼:
world
world服務(wù)與hello服務(wù)類似,不過沒有連接數(shù)據(jù)庫
項(xiàng)目代碼:world
helloworld調(diào)用hello服務(wù)和world服務(wù)
采用restTemplate+Ribbon調(diào)用服務(wù)班巩。
項(xiàng)目代碼:helloworld
啟動(dòng)類HelloworldApplication:
配置restTemplate的Bean
package com.example.helloworld;
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 HelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
入口類HelloworldController:
package com.example.helloworld.controller;
import com.example.helloworld.model.HelloMessage;
import com.example.helloworld.model.HelloworldMessage;
import com.example.helloworld.model.WorldMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author billjiang 475572229@qq.com
* @create 17-8-22
*/
@RestController
public class HelloworldController {
private static final Logger log = LoggerFactory.getLogger(HelloworldController.class);
private static final String HELLO_SERVICE_NAME = "hello";
private static final String WORLD_SERVICE_NAME = "world";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/")
public String home() {
return "hello world";
}
@GetMapping("/message")
public HelloworldMessage getMessage() {
HelloMessage hello = getMessageFromHelloService();
WorldMessage world = getMessageFromWorldService();
HelloworldMessage helloworld = new HelloworldMessage();
helloworld.setHello(hello);
helloworld.setWord(world);
log.debug("Result helloworld message:{}", helloworld);
return helloworld;
}
private HelloMessage getMessageFromHelloService() {
HelloMessage hello = restTemplate.getForObject("http://hello/message", HelloMessage.class);
log.debug("From hello service : {}.", hello);
return hello;
}
private WorldMessage getMessageFromWorldService() {
WorldMessage world = restTemplate.getForObject("http://world/message", WorldMessage.class);
log.debug("From world service : {}.", world);
return world;
}
}
測試:
helloworldfeign調(diào)用hello服務(wù)和world服務(wù)
feign方式調(diào)用服務(wù)渣慕,項(xiàng)目代碼參考:helloworldfeign
pom.xml引入feignx
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
配置啟動(dòng)類注解:
package com.example.helloworldfeign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class HelloworldFeignApplication {
public static void main(String[] args) {
SpringApplication.run(HelloworldFeignApplication.class, args);
}
}
被調(diào)用接口HelloService
package com.example.helloworldfeign.service;
import com.example.helloworldfeign.model.HelloMessage;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @author billjiang 475572229@qq.com
* @create 17-8-23
*/
@FeignClient(value="hello")
public interface HelloService {
@GetMapping("/message")
HelloMessage hello();
}
被調(diào)用接口WorldService
package com.example.helloworldfeign.service;
import com.example.helloworldfeign.model.WorldMessage;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @author billjiang 475572229@qq.com
* @create 17-8-23
*/
@FeignClient(value="world")
public interface WorldService {
@GetMapping("/message")
WorldMessage world();
}
入口類HelloworldController:
package com.example.helloworldfeign.controller;
import com.example.helloworldfeign.model.HelloMessage;
import com.example.helloworldfeign.model.HelloworldMessage;
import com.example.helloworldfeign.model.WorldMessage;
import com.example.helloworldfeign.service.HelloService;
import com.example.helloworldfeign.service.WorldService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author billjiang 475572229@qq.com
* @create 17-8-23
*/
@RestController
public class HelloworldController {
private static final Logger log = LoggerFactory.getLogger(HelloworldController.class);
private static final String HELLO_SERVICE_NAME = "hello";
private static final String WORLD_SERVICE_NAME = "world";
@Autowired
private HelloService helloService;
@Autowired
private WorldService worldService;
@GetMapping("/")
public String home() {
return "hello world";
}
@GetMapping("/message")
public HelloworldMessage getMessage() {
HelloMessage hello = helloService.hello();
WorldMessage world = worldService.world();
HelloworldMessage helloworld = new HelloworldMessage();
helloworld.setHello(hello);
helloworld.setWord(world);
log.debug("Result helloworld message:{}", helloworld);
return helloworld;
}
}
為了更好的地查看效果,hello服務(wù)和world服務(wù)可以啟動(dòng)多個(gè)實(shí)例抱慌,并把調(diào)用實(shí)例的ip和端口輸出來逊桦。