简单学习SpringCloud微服务-- Eureka
程序员文章站
2022-06-16 13:17:57
SpringBoot多模块微服务项目搭建创建Maven父工程创建Eureka模块pom.xml如下:application.yml如下:启动类中加入@EnableEurekaServer注解启动测试创建Maven父工程
SpringBoot多模块微服务项目搭建
创建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>SpringCloudTest</groupId>
<artifactId>EurekaTest</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 添加子模块 -->
<modules>
<module>eurekaclient</module>
<module>zuultest</module>
<module>ribbon</module>
<module>feign</module>
<module>eurakaservers</module>
</modules>
<!-- SpringBoot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 公用properties -->
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<!-- 公用依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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>
<version>2.3.2.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
创建Eureka模块
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>SpringCloudTest</groupId>
<artifactId>EurekaTest</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>SpringCloudTest</groupId>
<artifactId>eurakaservers</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eurakaservers</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.2.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
application.yml如下:
server:
port: 8080
#主机名
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #服务注册地址
register-with-eureka: false
fetch-registry: false
启动类中加入@EnableEurekaServer注解
启动测试
浏览器输入 localhost:8080
出现如图即为成功。
官方文档: https://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-eureka-server.
本文地址:https://blog.csdn.net/TYblackandwhite/article/details/109614533
推荐阅读
-
springcloud干货之服务注册与发现(Eureka)
-
SpringCloud之服务注册与发现Spring Cloud Eureka实例代码
-
springcloud学习之路: (四) springcloud集成Hystrix服务保护
-
微服务(入门学习五):identityServer4+ocelot+consul实现简单客户端模式
-
SpringCloud学习(SPRINGCLOUD微服务实战)一
-
Eureka(服务注册与发现)简单入门
-
SpringCloud-微服务的注册与发现Eureka
-
SpringCloud之Eureka:服务发布与调用例子
-
SpringCloud微服务实战:一、Eureka注册中心服务端
-
SpringCloud(二):服务的注册与发现(Eureka)