1. 程式人生 > >Eureka + Ribbon實現微服務的服務發現和負載均衡

Eureka + Ribbon實現微服務的服務發現和負載均衡

目錄

1:原理結構圖

2:搭建Eureka Server服務註冊中心

3:搭建Eureka Provider (服務提供者,即Eureka Client)

4:搭建 Eurka-Consumer


1:原理結構圖

2:搭建Eureka Server服務註冊中心

建立工程,引入Eureka,建立好的工程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.eureka</groupId>
	<artifactId>EurekaServer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>EurekaServer</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.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.M3</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</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>

在啟動類上加上 @EnableEurekaServer註解,啟動註冊服務中心,啟動類程式碼如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

配置application.yml檔案 

server:
  port: 8081 #服務註冊中心埠號

eureka:
  instance:
    hostname: 127.0.0.1 #服務註冊中心IP地址
  client:
    registerWithEureka: false #是否向服務註冊中心註冊自己
    fetchRegistry: false #是否檢索服務
    serviceUrl: #服務註冊中心的配置內容,指定服務註冊中心的位置
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

打成jar包,執行該工程後,在瀏覽器上輸入http://127.0.0.1:8081,看到如下介面,目前該頁面暫無註冊的服務提供者和消費者

3:搭建Eureka Provider (服務提供者,即Eureka Client)

建立工程,引入eureka 和spring boot web,其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.eureka</groupId>
	<artifactId>EurekaProvider</artifactId>
	<version>0.0.1</version>
	<packaging>jar</packaging>

	<name>EurekaProvider</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.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.M3</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-server</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>

建立一個RestController訪問方法:

        @Value("${server.port}")
	private String port;
	
	@GetMapping("/home")
	public String homePage() {
		return "Welcome to home page"+" : " + port;
	}

在啟動類上加上 @EnableEurekaClient註解,申明是Eureka客戶端

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient

public class EurekaProviderApplication {

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

配置application.yml檔案 

eureka:
  client:
    serviceUrl: #註冊中心的註冊地址
      defaultZone: http://127.0.0.1:8081/eureka/
server:
  port: 8082  #服務埠號
spring:
  application:
    name: service-provider #服務名稱--呼叫的時候根據名稱來呼叫該服務的方法

 

 

打成jar,執行兩個例項

java -jar EurekaProvider-0.0.1.jar   //配置檔案中設定成8082埠

java -jar EurekaProvider-0.0.1.jar --server.port=8083

成功執行後,Eureka服務註冊中心會註冊兩個provider例項,如下介面:

4:搭建 Eurka-Consumer

主要目的是用來消費服務,同時提供負載均衡的功能。引入Eurka,web後的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.eureka</groupId>
	<artifactId>EurekaConsumer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>EurekaConsumer</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.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.M3</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-server</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>

在啟動類中往spring boot容器中新增RestTemplate物件和IRule物件(用於負載均衡),後期會對常用的負載均衡策略方法做個總結。

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;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;

@SpringBootApplication
@EnableEurekaClient

public class EurekaConsumerApplication {
	
	@LoadBalanced
	@Bean
    public RestTemplate rest() {
        return new RestTemplate();
    }
	

	@Bean
	public IRule ribbonRule() {
		return new RandomRule(); //隨機負載均衡策略,如未向容器中注入,預設是輪詢方式
	}
	
	

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

注意:在往容器中注入RestTemplate物件時,需要加上@LoadBalanced註解,否則不能正常訪問Eurka中的服務。其原理可以參考https://blog.csdn.net/jin5203344/article/details/80496078

新增Controller,提供給外部訪問的介面以及訪問provider提供的介面

@RestController

public class goHomePage {
	
	@Autowired
	private RestTemplate restTemplate;
	
	
	@GetMapping("/home")
	public String getHome() {
		
		String data = restTemplate.getForObject("http://service-provider/home", String.class);
		
		return data;
	
	}

}

配置application. yml

eureka:
  client:
    serviceUrl: #註冊中心的註冊地址
      defaultZone: http://127.0.0.1:8081/eureka/
server:
  port: 8093  #服務埠號
spring:
  application:
    name: service-consumer #服務名稱--呼叫的時候根據名稱來呼叫該服務的方法

執行Consumer專案,在Eureka 註冊中心的管理頁面上可以看到其註冊資訊

目前為止,已經完成 Eureka的註冊中心,服務提供者,消費者。在瀏覽器中輸入http://127.0.0.1:8093/home 即可訪問到服務提供者提供的資料,如下截圖:

Eureka在開發除錯時會涉及到多個服務提供者,不適合點對點的調測,故後續會記錄點對點的直連方式,結構如下: