前言
本章学习Spring Cloud的第一个组件中的
方法
1.概念
Spring Cloud Netflix通过自动配置和Spring环境以及其他Spring编程模型习惯用法为Spring Boot应用程序提供Netflix OSS集成。 通过一些简单的注释,您可以快速启用和配置应用程序内的常见模式,并使用经过实战考验的Netflix组件构建大型分布式系统。 提供的模式包括服务发现(Eureka),断路器(Hystrix),智能路由(Zuul)和客户端负载平衡(Ribbon)。
那么,我们本次学习的是其第一个部分Eureka。
2.Eureka是什么
Eureka是一个服务注册中心,给客户端提供可供调用的服务列表,客户端在进行远程服务调用时,根据服务列表然后选择服务提供方的服务地址进行服务调用。服务注册中心在分布式系统中大量应用,是分布式系统中不可或缺的组件。
如同我们之前学习的Dubbo一样,它提供着和Zookeeper类似的功能。
3.Eureka注册中心的三种角色
- Eureka Server:相当于zookeeper,提供了服务的注册与发现等功能
- Application Service:相当于Dubbo的Provider角色,将自身的服务实例注册到Eureka Server中,暴露给消费者
- Application Client:相当于Dubbo的Consumer角色,通过Eureka Server获取服务列表,消费服务
4.搭建简单的Eureka项目
按照Eureka的角色划分,我们需要一次搭建Eureka Server、Application Service、Application Client
Eureka Server搭建
1)创建工程
2)修改pom文件,添加必要的依赖坐标
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
</parent>
<groupId>cn.edu.ccut</groupId>
<artifactId>springcloud-eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!-- 设定Java的版本 -->
<java.version>1.8</java.version>
<!-- 解决pom.xml首行报错 -->
<maven-jar-plugin.version>3.1.0</maven-jar-plugin.version>
<!-- spring cloud版本 -->
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<dependencies>
<!-- 配置springBoot的web启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka-server坐标 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- 配置devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 配置springBoot的test启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<!-- spring cloud依赖管理 -->
<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>
<configuration>
<mainClass>cn.edu.ccut.EurekaServerApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
3)修改application.properties文件,添加相应的配置
#eureka server port
server.port=8761
#eureka server hostname
eureka.instance.hostname=localhost
#Register yourself to the Eureka server
eureka.client.registerWithEureka=false
#Get service registration information from Eureka server
eureka.client.fetchRegistry=false
#eureka server url
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
4)编写Eureka Server启动器,主要加入注解@EnableEurekaServer
package cn.edu.ccut;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApp {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApp.class, args);
}
}
5)启动我们的项目,访问http://localhost:8761/,出现如下画面即项目启动成功。
Application Service搭建
接下来我们进行服务提供端的搭建,即Provider
1)创建工程和上面相同
2)pom文件修改如下
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
3)修改application.properties文件
#eureka server url
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#eureka provider port
server.port=8888
#application name
spring.application.name=eureka-provider
3)增加服务方法,供消费者调用
package cn.edu.ccut.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProviderController {
@RequestMapping("/getStr")
public String getString(){
return "略略略";
}
}
4)修改启动类,加入注解@EnableEurekaClient
package cn.edu.ccut;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class EurekaProviderApp {
public static void main(String[] args) {
SpringApplication.run(EurekaProviderApp.class, args);
}
}
5)启动项目,刷新之前的Eureka Server页面
我们可以发现,我们的服务已经注册到了Eureka中
Application Client搭建
接下来我们进行服务提供端的搭建,即Consumer
1)创建工程和上面相同
2)修改pom文件
为了方便我们的操作,我们引入ribbon坐标
<!-- ribbon坐标 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
3)修改application.properties
#eureka server url
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#eureka consumer port
server.port=8080
#application name
spring.application.name=eureka-consumer
4)创建配置类,实例化restTemplate
package cn.edu.ccut.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class Config {
@Bean
@LoadBalanced// 开启负载均衡
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
5)创建消费者的controller,用来调用服务方方法
package cn.edu.ccut.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/getMsg")
public String getMsg(){
String str = restTemplate.getForObject("http://eureka-provider/getStr",String.class);
return str;
}
}
5)启动器修改,加入注解@EnableEurekaClient
package cn.edu.ccut;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class EurekaConsumerApp {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerApp.class, args);
}
}
6)启动项目,观察Eureka页面
5.测试消费者
将上述三个项目启动,访问消费者方的路径:http://localhost:8080/getMsg,观察效果:
由此可见,我们成功的完成了服务的调用。