概述
在微服务架构中,服务发现是关键原则之一。手动配置每个客户端或某种形式的约定是很难做的,并且很脆弱。Spring Cloud提供了多种服务发现的实现方式,例如:Eureka、Consul、Zookeeper当然还有Alibaba的 Nacos
而本次教程系列采用Eureka;
本系列源码地址:https://github.com/lhmyy521125/toher-springcloud-sample
创建maven主工程
修改主项目的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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modules>
<module>eureka-server</module>
<module>service-hello</module>
</modules>
<groupId>cn.toher</groupId>
<artifactId>springcloud-sample</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<!-- Environment Settings -->
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring Settings -->
<spring-cloud.version>Hoxton.M3</spring-cloud.version>
</properties>
<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>
<dependencies>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<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>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
小提示:本次使用的IDEA创建的springboot版本为2.2.0 ,对于主项目的POM.XML
不知道版本和对应的spring-cloud.version 不知道如何配置写入,那么我们可以单独创建一个
SpringBoot项目引入SpringCloud Euerka Service 那么讲该项目的POM.XML复制到主项目的XML即可
创建服务注册中心
右键项目 新建一个module
工程创建完成后,因为是模块化开发,该项目会继承了父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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.toher</groupId>
<artifactId>springcloud-sample</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.toher</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
找到SpringBoot启动类 加入注解 @EnableEurekaServer 来启动一个服务注册中心,并修改application.properties为application.yml ,博主推荐采用yml的配置方式
eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件application.yml
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eurka-server
通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server,通过IDEA Run Dashboard 模式启动该项目 ,最后访问 http://localhost:8761/ 见到下图证明已经成功
第一个服务提供者service-hello
重复创建Euerka-server项目的步骤,再次创建一个名为 service-hello 子项目,这里就不再重复贴图了,将所需的配置贴出来即可
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.toher</groupId>
<artifactId>springcloud-sample</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.toher</groupId>
<artifactId>service-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-hello</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
启动类: 加入@EnableEurekaClient 证明该工程为服务提供者
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHelloApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceHelloApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hello")
public String home(@RequestParam(value = "name", defaultValue = "toher") String name) {
return "hi " + name + " ,this is EurekaClient , i am from port:" + port;
}
}
application.yml: 配置文件中配置该服务的注册中心的地址
server:
port: 9001
spring:
application:
name: service-hello
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
注意配置spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name,所以我们开发SpringCloud一定要习惯性的添加该配置
启动service-hello项目,查看euerka注册中心会发现 ,服务已经注册到了注册中心,且端口号是9001
直接请求我们的 http://localhost:9001/hello?name=mico 看到下图
结语
本章节主要介绍Euerka 服务注册中心的搭建,并以一个简单的服务提供者来进行服务注册;大家可以根据博主的代码样例,进行相关测试学习;
下一篇:创建服务消费者(Ribbon)