服务消费者

在上一篇文章,讲了服务的注册和发现http://linkais.com/archives/292,在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。本文先基于ribbon+rest实现服务消费。

一、ribbon简介
ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。Feign默认集成了ribbon。
基于上一篇文章,使用ribbon+restTemplate方式实现服务消费与负载均衡,需要启动三个工程,分别是EurakaServer、EurakaClient,复制EurakaClient工程,并命名为EurakaClient1,主要是为了等会验证负载均衡。

二、新建一个服务消费者工程
1、新建一个工程,取名为:serviceRibbon,在pom.xml引入了以下依赖:



4.0.0

com.example
demo
0.0.1-SNAPSHOT
jar

demo
Demo project for Spring Boot


	org.springframework.boot
	spring-boot-starter-parent
	2.1.0.RELEASE
	 



	UTF-8
	UTF-8
	1.8
	Finchley.RELEASE



	
		org.springframework.boot
		spring-boot-starter-web
	
	
		org.springframework.cloud
		spring-cloud-starter-netflix-eureka-client
	

	
		org.springframework.boot
		spring-boot-starter-test
		test
	

	
		org.springframework.cloud
		spring-cloud-starter-netflix-ribbon
	



	
		
			org.springframework.cloud
			spring-cloud-dependencies
			${spring-cloud.version}
			pom
			import
		
	



	
		
			org.springframework.boot
			spring-boot-maven-plugin
		
	


2、在工程的配置文件指定服务的注册中心地址为http://localhost:3401/eureka/,程序名称为 serviceRibbon,程序端口为3404。配置文件application.yml如下:

server:
  port: 3404

spring:
  application:
    name: serviceRibbon

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:3401/eureka/

在工程的启动类中,通过@EnableEurekaClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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;

@SpringBootApplication
@EnableEurekaClient
public class DemoApplication {

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

	@Bean
	@LoadBalanced
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}
}

3、编写服务类通过=注入ioc容器的restTemplate来消费EeurekaClient服务的接口,路径为“/test”,代码如下所示:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class TestService {

    @Autowired
    public RestTemplate restTemplate;

    public String testRibbonService() {
        return restTemplate.getForObject("http://eurekaClient/test",String.class);
    }
}

使用restTemplate.getForObject方法通过程序名替代了具体的url地址。

4、写一个controller,在controller中用调用TestService的方法,代码如下:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestControler {
    @Autowired
    public TestService testService;

    @GetMapping(value = "/testRibbon")
    public String testRibbonService() {
        return testService.testRibbonService();
    }
}

5、最后在浏览器上多次访问http://localhost:3404/testRibbon?name=XXX,将消费EnarakaClient和EnarakaClient1两个工程,打印出不同端口号,因此实现负载均衡。如图:

服务消费者_第1张图片

服务消费者_第2张图片

服务注册中心访问后如下图所示:

服务消费者_第3张图片

你可能感兴趣的:(spring,cloud)