微服务之服务消费者(rest+ribbon)

"Hello SpringBoot, Hello SpringClould"

Posted by 墨城烟柳(Mcyl) on November 7, 2018

“Hello everyone! ”

Ribbon简介

Ribbon 主要有三大模块,模块说明如下:

  • ribbon-core:该模块为Ribbon项目的核心,主要包括负载均衡器接口定义、客户端接口定义、内置的负载均衡实现等API
  • ribbon-eureka:为 Eureka 客户端提供的负载均衡实现类
  • ribbon-httpclient:对 Apache 的 HttpClient 进行封装,该模块提供了含有负载均衡功能的 REST 客户端

第一步:创建服务集群

这一篇文章基于上一篇文章的工程, 启动EurekaServer工程;启动EurekaClient工程,它的端口为8762;复制工程EurekaClient,并将EurekaClient的配置文件的端口改为9873,并启动,这时你会 发现:EurekaClient在EurekaServer注册了2个实例,这就相当于一个小的集群。

第二步:创建一个服务消费者:

右键工程->创建创建module->选择spring initialir->web->web,cloud discovery->eureka discovery,cloud routing->ribbon,然后一直下一步就行了。

创建完成后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?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.test</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>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Greenwich.M1</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>


</project>

配置appication.yml文件:

1
2
3
4
5
6
7
8
9
eureka:
  client:
	service-url:
	  defaultZone: http://localhost:9871/eureka/
server:
  port: 9874
spring:
  application:
	name: service-ribbon

第三步 配置启动类

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 ServiceRibbonApplication {

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

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

第四步:写一个测试类HelloService

通过之前注入ioc容器的RestTemplate来消费eureka-client服务的”/hello”接口, 在这里我们直接用的程序名替代了具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例, 根据服务实例在请求的时候会用具体的url替换掉服务名,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;

	public String helloService(String name){
		return restTemplate.getForObject("http://eureka-client/hello?name="+name,String.class);
	}
}

第五步:写一个测试类controller

在controller中用调用HelloService 的方法,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import com.test.serviceribbon.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

	@Autowired
	HelloService helloService;

	@RequestMapping(value = "/hello")
	public String hello(@RequestParam String name){
		return helloService.helloService(name);
	}
}

启动工程

打开 http://localhost:9874/hello?name=handsomeman ,你会在浏览器上看到交替显示:

hello: handsomeman,i am from port:9872

hello: handsomeman,i am from port:9873

这说明当我们通过调用restTemplate.getForObject(“http://eureka-client/hello?name=”+name,String.class);方法时,已经做了负载均衡,访问了不同的端口的服务实例。

源码下载:

https://github.com/mochengyanliu/SpringCloudLearn/tree/master/section2