类似于 DUBBO 的 zookeeper, SpringCloud 本身提供一套服务注册中心 --eureka
与 zookeeper 的区别在于
1:zookeeper 本身就是一个应用, 安装即可用; eureka 其实是一个 jar, 需要新建一个 maven 项目, 以及手动配置端口和 pom 文件. 发布后即可使用
2:zookeeper 本身不提供 web 端展示, 需要重新安装配置 dubbo 客户端或者 dubbokeeper 实时监控服务; eureka 发布成功后, 即可有对应的 spring 服务监控页面.
搭建 SpringCloud 的服务注册中心 - eureka
1. 新建 maven 项目 eureka-server,eureka-server 是作为一个子项目, 目录结构如下
eureka-server 还是比较简单的, 主要需要修改这三个文件: EurekaServerApplication,application.yml,pom.xml
- 1.pom.xml
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <!--eureka server -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka-server</artifactId>
- </dependency>
- <!-- spring boot test-->
- <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>Dalston.RC1</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
spring-cloud-starter-eureka-server:erueka 注册中心核心依赖包
当然, 作为一个 springboot 项目, 需要依赖于:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
所有的子项目都是 springcoot 项目, 所以该依赖放在了父项目的 pom 中
2.application.yml
springboot 启动时, 会自动扫描该配置文件, 当然, application.properties 也是可以的
- server:
- port: 8761
- eureka:
- instance:
- hostname: localhost
- client:
- registerWithEureka: false
- fetchRegistry: false
- serviceUrl:
- defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
其中: server.port: 定义端口号
eureka.instance.hostname: 定义 eureka 为本地 ip, 发布测试或者线上环境视情况配置
eureka.client.registerWithEureka: 是否注册本身, 这里不需要. 所以为 false
eureka.client.fetchRegister: 是否从服务器获取注册信息, 这里也不需要
eureka.client.serviceUrl.defaultZone: 定义服务注册地址, 后续的提供或者消费都需要通过该地址进行注册
3.EurekaServerApplication.java
springboot 启动文件
- @EnableEurekaServer
- @SpringBootApplication
- public class EurekaServerApplication {
- public static void main(String[] args) {
- SpringApplication.run(EurekaServerApplication.class, args);
- }
- }
@EnableEurekaServer: 表明他是服务注册中心
@SpringBootApplication: 表明他是 springboot 项目
最后: 启动该项目, 就可以了
浏览器打开: http://localhost:8761/
从中可以看出, eureka 服务注册中心已经启动成功. 只是目前的 application 中没有任何内容, 当然, 我们只是搞定了这个注册中心, 还没有任何的服务呢
下面, 我们来尝试写一个服务, 同时注册到该注册中心
注册服务 eureka-client
首先, 还是来看下这个项目 eureka-client 的结构
还是修改这三个文件: EurekaClientApplication,application.yml,pom.xml(所有的 springboot 项目, 这三个文件也是最基本的, 缺一不可)
- 1.pom.xml
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka</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>Dalston.RC1</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- 2.application.yml
- eureka:
- client:
- serviceUrl:
- defaultZone: http://localhost:8761/eureka/
- server:
- port: 8762
- spring:
- application:
- name: eureka-client
eureka.client.serviceUrl.defaultZone: 与 eureka-server 中的配置文件配置一样就行了
server.port: 定义端口号
spring.application.name: 定义项目名, 方便在注册中心查看
- 3.EurekaClientApplication.java
- @SpringBootApplication
- @EnableEurekaClient
- @RestController
- public class EurekaClientApplication {
- public static void main(String[] args) {
- SpringApplication.run(EurekaClientApplication.class, args);
- }
- @Value("${server.port}")
- String port;
- @RequestMapping("/")
- public String home() {
- return "hello world from port" + port;
- }
- }
@EnableEurekaClient: 同意注册到注册中心
再启动 eureka-client 后, 会发现如图:
eureka-client 已经注册成功
直接输入: http://localhost:8762/
至此, 基础的服务注册中心搭建成功, 有问题希望大家多提意见, 谢谢!
来源: https://www.cnblogs.com/qiyuan880794/p/9315934.html