凡铁游戏提供最新游戏下载和手游攻略!

深度解析:Spring Cloud - 单机版Eureka服务治理注册中心搭建教程

发布时间:2024-10-17浏览:79

一、服务治理

服务治理是分布式架构的基础设施服务,是微服务架构中核心模块,作用是微服务实例的自动化注册与发现。当微服务比较少的时候,通过静态配置就可以完成服务的调用。但当系统越来越复杂、相应的微服务应用也不断增加,静态配置就会难以维护。服务治理这时候主要实现以下功能:

服务注册

服务发现

Netflix的Eureka项目就是一个服务治理工具,开源地址:https://github.com/Netflix/eurekaEureka由两个组件组成:服务端和客户端。当一个中间层服务首次启动时,他会将自己注册到 Eureka 中,以便让客户端找到它,同时每 30 秒发送一次心跳。如果一个服务在几分钟内没有发送心跳,它将从所有 Eureka 节点上注销。

Spring Cloud Eureka,使用Netflix Eureka来实现服务注册与发现,支持通过Java实现分布式系统,或是与JVM兼容语言构建的系统。由于Eureka服务端的服务治理也提供了restful api,所以也支持非Java语言构建的微服务应用。

二、搭建服务注册中心

1. 新建一个maven项目

添加依赖:

<?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.xundh.test</groupId> <artifactId>eurekaserver1</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> </parent> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server --> <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-actuator</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</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>

通过注解启用服务注册中心

package com.xundh.test;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); }}

bootstrap.yml

spring: application: name: eureka cloud: config: uri: ${CONFIG_SERVER_URL:http://localhost:8888}

设置application.yml:

server: port: 8761 debug: trueeureka: client: registerWithEureka: false fetchRegistry: false server: waitTimeInMsWhenSyncEmpty: 0

配置说明:

Server.port:服务端口

eureka.client.register-with-eureka:该应用为注册中心,所以设置为false,不向注册中心注册自己

eureka.client.fetch-registry: 注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false

运行项目,访问网址 http://localhost:8761

2. 注册服务提供者

新建一个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> <groupId>com.xundh.test</groupId> <artifactId>eurekaservice</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> </parent> <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> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.7.RELEASE</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</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>

在启动类上加上@EnableDiscoveryClient注解:

package com.xundh;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication@EnableDiscoveryClientpublic class ServiceApplication { public static void main(String[] args) { SpringApplication.run(ServiceApplication.class, args); }}

建一个RestController

package com.xundh;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.client.discovery.DiscoveryClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController { private final Logger logger = LoggerFactory.getLogger(HelloController.class); @Autowired private DiscoveryClient client; @RequestMapping(value="/hello", method = RequestMethod.GET) public String index(){// ServiceInstance instance = client. logger.info("/hello,host"); return "Hello World"; }}

配置文件application.yml

spring: application: name: my-eureka-serviceeureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/

启动后,在注册中心可以看到注册了新的服务。

用户评论

巴黎盛开的樱花

这款《SpringCloud学习1》看起来非常好入门哦,对初学者来说是个不错的选择。

    有9位网友表示赞同!

拉扯

搭建单机Eureka服务治理注册中心的功能很实用,适合快速实践分布式系统概念。

    有11位网友表示赞同!

我一个人

游戏名称和内容有点不搭啊,但至少教了我如何设立服务注册中心。

    有17位网友表示赞同!

拽年很骚

虽然不是传统意义上的游戏,但这款程序教学游戏的互动性很棒。

    有11位网友表示赞同!

你很爱吃凉皮

这款游戏让我对SpringCloud有了更深的了解,感觉学习效率提升了。

    有17位网友表示赞同!

算了吧

对于想学Spring Cloud的开发者来说,这是一个不错的起点。

    有17位网友表示赞同!

米兰

通过这种方式学习,觉得代码编写和理解变得更加清晰了。

    有5位网友表示赞同!

不识爱人心

这个工具挺有帮助的,希望后续可以出更多这样的教学游戏。

    有15位网友表示赞同!

容纳我ii

之前在搭建注册中心时总遇到问题,《SpringCloud学习1》解决了我的烦恼。

    有20位网友表示赞同!

封心锁爱

对于喜欢动手实践的人来说,这个游戏太有趣了。

    有5位网友表示赞同!

玩味

《SpringCloud学习1》简单易懂,让我迅速掌握了单机Eureka的基础知识。

    有11位网友表示赞同!

热点资讯