1.Eureka/Ribbon基本概念
Eureka是Spring Cloud全家桶中锨亏,服務(wù)注冊(cè)活合、發(fā)現(xiàn)的組件文判。
Eureka主要由三個(gè)模塊構(gòu)成
服務(wù)注冊(cè)中心:
(1)服務(wù)注冊(cè)胧谈,新服務(wù)都需要在注冊(cè)中心進(jìn)行注冊(cè)忆肾;
(2)服務(wù)發(fā)現(xiàn),服務(wù)需求方菱肖,即Eureka客戶端來注冊(cè)中心進(jìn)行服務(wù)發(fā)現(xiàn)客冈,獲取需要的服務(wù)地址;
(3)服務(wù)管理稳强,維護(hù)各個(gè)服務(wù)的健康情況郊酒,保證服務(wù)的可用性服務(wù)提生產(chǎn)者,提供各種服務(wù)
-
服務(wù)消費(fèi)者(client)键袱,使用生產(chǎn)者提供的服務(wù)
Ribbon:主要功能為保障服務(wù)訪問的負(fù)載均衡
2.構(gòu)建Eureka 服務(wù)注冊(cè)發(fā)現(xiàn)中心
2.1 創(chuàng)建一個(gè)spring boot工程
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>
<groupId>xyz.onepig</groupId>
<artifactId>discovery-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>discovery-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.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>Finchley.SR1</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>
</project>
2.2在入口文件中添加注解
@SpringBootApplication
@EnableEurekaServer
public class DiscoveryServerApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryServerApplication.class, args);
}
}
2.3配置文件如下:
/resources/application.roperties
spring.application.name=discovery-server
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
server.port=8761
2.4啟動(dòng)注冊(cè)中心
(啟動(dòng)server的spring boot)摹闽,訪問本地8761端口蹄咖,出現(xiàn)如下界面,則server創(chuàng)建成功付鹿。
3.構(gòu)建Eureka server 服務(wù)生產(chǎn)者
3.1創(chuàng)建一個(gè)spring boot工程
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>
<groupId>xyz.onepig</groupId>
<artifactId>time-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>time-service</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.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>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
</project>
3.2編寫服務(wù)生產(chǎn)者代碼
我們?cè)谶@里構(gòu)建一個(gè)時(shí)間查詢服務(wù)澜汤,用于返回當(dāng)前時(shí)間,代碼如下舵匾,關(guān)鍵注解為@EnableDiscoveryClient俊抵,功能是注冊(cè)服務(wù)。
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class TimeServiceApplication {
@Value("${server.port}")
private int port;
public static void main(String[] args) {
SpringApplication.run(TimeServiceApplication.class, args);
}
@GetMapping
public String getTime() {
return "The current time is " + new Date().toString() + "(answered by service running on " + port + ")";
}
}
3.3配置文件如下:
/resources/application.roperties坐梯,注意service-url為服務(wù)發(fā)現(xiàn)server的地址
spring.application.name=time-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
#eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
3.4定制化啟動(dòng)
配置server.port參數(shù)為4444和5555(Edit Configurations)徽诲,分別啟動(dòng)兩個(gè)服務(wù)實(shí)例
可以看到Eureka控制中心已經(jīng)出現(xiàn)了該服務(wù)的兩個(gè)實(shí)例(Instances currently registered with Eureka),即剛才啟動(dòng)的4444和5555
4.構(gòu)建Eureka客戶端,使用Ribbon負(fù)載均衡
4.1創(chuàng)建一個(gè)spring boot工程
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>
<groupId>xyz.onepig</groupId>
<artifactId>time-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>time-client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.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>Finchley.SR1</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.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>
</project>
4.2主程序代碼
其中關(guān)鍵注解為
@EnableDiscoveryClient:發(fā)現(xiàn)服務(wù)
@LoadBalanced:?jiǎn)⒂肦ibbon
@SpringBootApplication
@RestController
@EnableDiscoveryClient
public class TimeClientApplication {
@Autowired
private RestTemplate restTemplate;
public static void main(String[] args) {
SpringApplication.run(TimeClientApplication.class, args);
}
@GetMapping
public String getTime() {
return restTemplate.getForEntity("http://time-service", String.class).getBody();
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
4.3配置文件
/resources/application.properties
spring.application.name=time-client
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
eureka.client.register-with-eureka=false
server.port=8081
4.4啟動(dòng)client
訪問本機(jī)8081端口谎替,刷新幾次偷溺,可以看到訪問被負(fù)載到了兩個(gè)服務(wù)上:
本文參考:《極客時(shí)間-微服務(wù)建構(gòu)實(shí)戰(zhàn)160講》
要走一刻,請(qǐng)不必諸多眷戀钱贯。
離別大概是職場(chǎng)的日常吧挫掏。
臺(tái)風(fēng)已過境,明天又是新的一周秩命,晚安尉共。