Spring Cloud微服务实战(四) - 服务消费者实例

上文我们创建了注册中心,以及服务的提供者microservice-provider-user,并成功地将服务提供者注册到了注册中心上。

要想消费microservice-provider-user的服务是很简单的,我们只需要使用RestTemplate即可,或者例如HttpClient之类的http工具也是可以的。但是在集群环境下,我们必然是每个服务部署多个实例,那么服务消费者消费服务提供者时的负载均衡又要如何做呢?

准备工作

  1. 启动注册中心:microservice-discovery-eureka
  2. 启动服务提供方:microservice-provider-user
  3. 修改microservice-provider-user的端口为8001,另外启动一个实例

此时,访问http://discovery:8761

Eureka上面注册2个服务提供者

可以在Eureka中看到microservice-provider-user有两个实例在运行。

下面我们创建一个新的微服务(microservice-consumer-movie-*),负载均衡地消费microservice-provider-user的服务。

Ribbon介绍

Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器。我们也很容易使用Ribbon实现自定义的负载均衡算法。简单地说,Ribbon是一个客户端负载均衡器。

Ribbon工作时分为两步:第一步先选择 Eureka Server, 它优先选择在同一个Zone且负载较少的Server;第二步再根据用户指定的策略,在从Server取到的服务注册列表中选择一个地址。其中Ribbon提供了多种策略,例如轮询、随机、根据响应时间加权等。

Ribbon代码示例

创建一个Maven项目,并在pom.xml中加入如下内容:

<?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>

  <artifactId>microservice-consumer-movie-ribbon</artifactId>
  <packaging>jar</packaging>

  <parent>
    <groupId>com.itmuch.cloud</groupId>
    <artifactId>spring-cloud-microservice-study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>

    <!-- 整合ribbon -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
  </dependencies>
</project>

启动类:MovieRibbonApplication.java。使用@LoadBalanced注解,为RestTemplate开启负载均衡的能力。

@SpringBootApplication
@EnableDiscoveryClient
public class MovieRibbonApplication {
  /**
   * 实例化RestTemplate,通过@LoadBalanced注解开启均衡负载能力.
   * @return restTemplate
   */
  @Bean
  @LoadBalanced
  public RestTemplate restTemplate() {
    return new RestTemplate();
  }

  public static void main(String[] args) {
    SpringApplication.run(MovieRibbonApplication.class, args);
  }
}

实体类:User.java

public class User {
  private Long id;
  private String username;
  private Integer age;
  ...
  // getters and setters
}

Ribbon的测试类:RibbonService.java

@Service
public class RibbonService {
  @Autowired
  private RestTemplate restTemplate;

  public User findById(Long id) {
    // http://服务提供者的serviceId/url
    return this.restTemplate.getForObject("http://microservice-provider-user/" + id, User.class);
  }
}

controller:RibbonController.java

@RestController
public class RibbonController {
  @Autowired
  private RibbonService ribbonService;

  @GetMapping("/ribbon/{id}")
  public User findById(@PathVariable Long id) {
    return this.ribbonService.findById(id);
  }
}

application.yml

server:
  port: 8010
spring:
  application:
    name: microservice-consumer-movie-ribbon
eureka:
  client:
    serviceUrl:
      defaultZone: http://discovery:8761/eureka/
  instance:
    preferIpAddress: true

启动后,访问多次http://localhost:8010/ribbon/1,返回结果:

{
    "id": 1,
    "username": "Tom",
    "age": 12
}

然后打开两个microservice-provider-user实例的控制台,发现两个实例都输出了类似如下的日志内容:

Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.username as username3_0_0_ from user user0_ where user0_.id=?
2016-09-13 21:38:56.719 TRACE 17404 --- [nio-8000-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [1]
2016-09-13 21:38:56.720 TRACE 17404 --- [nio-8000-exec-1] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([age2_0_0_] : [INTEGER]) - [12]
2016-09-13 21:38:56.720 TRACE 17404 --- [nio-8000-exec-1] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([username3_0_0_] : [VARCHAR]) - [Tom]
2016-09-13 21:39:10.588  INFO 17404 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration

至此,我们已经通过Ribbon在客户端侧实现了均衡负载。

代码地址(任选其一):

Ribbon代码地址:

http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microservice-consumer-movie-ribbon
https://github.com/eacdy/spring-cloud-study/tree/master/microservice-consumer-movie-ribbon

Feign介绍

Feign是一个声明式的web service客户端,它使得编写web service客户端更为容易。创建接口,为接口添加注解,即可使用Feign。Feign可以使用Feign注解或者JAX-RS注解,还支持热插拔的编码器和解码器。Spring Cloud为Feign添加了Spring MVC的注解支持,并整合了Ribbon和Eureka来为使用Feign时提供负载均衡。

译自:http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html#spring-cloud-feign

Feign示例

创建一个Maven项目,并在pom.xml添加如下内容:

<?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>

  <artifactId>microservice-consumer-movie-feign</artifactId>
  <packaging>jar</packaging>

  <parent>
    <groupId>com.itmuch.cloud</groupId>
    <artifactId>spring-cloud-microservice-study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-feign</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
  </dependencies>
</project>

启动类:MovieFeignApplication.java

/**
 * 使用@EnableFeignClients开启Feign
 * @author eacdy
 */
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class MovieFeignApplication {
  public static void main(String[] args) {
    SpringApplication.run(MovieFeignApplication.class, args);
  }
}

实体类:User.java

public class User {
  private Long id;
  private String username;
  private Integer age;
  ...
  // getters and setters
}

Feign测试类:UserFeignClient.java。

/**
 * 使用@FeignClient("microservice-provider-user")注解绑定microservice-provider-user服务,还可以使用url参数指定一个URL。
 * @author eacdy
 */
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
  @RequestMapping("/{id}")
  public User findByIdFeign(@RequestParam("id") Long id);
}

Feign的测试类:FeignController.java

@RestController
public class FeignController {
  @Autowired
  private UserFeignClient userFeignClient;

  @GetMapping("feign/{id}")
  public User findByIdFeign(@PathVariable Long id) {
    User user = this.userFeignClient.findByIdFeign(id);
    return user;
  }
}

application.yml

server:
  port: 8020
spring:
  application:
    name: microservice-consumer-movie-feign
eureka:
  client:
    serviceUrl:
      defaultZone: http://discovery:8761/eureka/
  instance:
    preferIpAddress: true
ribbon:
  eureka:
    enabled: true         # 默认为true。如果设置为false,Ribbon将不会从Eureka中获得服务列表,而是使用静态配置的服务列表。静态服务列表可使用:<client>.ribbon.listOfServers来指定。参考:http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html#spring-cloud-ribbon-without-eureka
    
### 参考:https://spring.io/guides/gs/client-side-load-balancing/

同样的,启动该应用,多次访问http://localhost:8020/feign/1,我们会发现和Ribbon示例一样实现了负载均衡。

代码地址(任选其一):

Feign代码地址:

http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microservice-consumer-movie-feign
https://github.com/eacdy/spring-cloud-study/tree/master/microservice-consumer-movie-feign

(全文完)

(转载本站文章请注明作者和出处 Spring Cloud微服务实战(四) - 服务消费者实例