Spring Cloud 学习笔记1 Eureka环境搭建
Spring Cloud学习笔记1
跟着方志朋大佬学习的笔记,我的博客这里全部记录下,后续可能会基于Spring Cloud开源一个类似typecho的博客系统。
代码已经上传到github上,各位可参考 blog2
方志朋大佬的CSDN博客:1.方志朋CSDN博客地址 2.方志朋个人博客地址
首先大佬介绍了微服务的整体,其实也没啥好说的,具体可以上知乎:什么是微服务架构?
方志朋大佬用的Spring Boot是2.0.3
,因为版本比较旧了,不太合适,我使用的是2.3.1
。spring cloud 版本Hoxton.RELEASE
一. 环境搭建
1.1 新建ProJect
打开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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.djc8</groupId>
<artifactId>blog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>blog</name>
<description>Demo project for Spring Boot</description>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Hoxton.RELEASE</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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
</plugin>
</plugins>
</build>
</project>
1.2 新建Eureka Server Module
项目的Maven依赖遵从主从依赖,所以需要修改项目底下的POM.xml
增加modules
节点,并且将eurekaserver
增加进去
<modules>
<module>eurekaserver</module>
</modules>
eurekaserver 修改parent依赖
<parent>
<groupId>cn.djc8</groupId>
<artifactId>blog2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
eurekaserver 的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.djc8</groupId>
<artifactId>blog2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>cn.djc8</groupId>
<artifactId>eurekaserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eurekaserver</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
新建的module如果是 src\main\resources\application.properties
则需要改名为 src\main\resources\application.yml
修改application.yml
文件,如下
server:
port: 7001
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eurekaserver
修改src\main\java\cn\djc8\eurekaserver\EurekaserverApplication.java
增加 注解,如图
@EnableEurekaServer
@RestController
1.3 运行Eureka Server
日志信息
打开浏览器,输入:http://localhost:7001/
至此Eureka注册中心已经运行成功
1.4 新建Eureka Client Module
修改client
的pom.xml
<parent>
<groupId>cn.djc8</groupId>
<artifactId>blog2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
此时client
的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.djc8</groupId>
<artifactId>blog2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>cn.djc8</groupId>
<artifactId>eurekaclient</
artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eurekaclient</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR5</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
同时跟server
一样,需要增加项目的module
节点
<modules>
<module>eurekaserver</module>
<module>eurekaclient</module>
</modules>
此时项目的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.djc8</groupId>
<artifactId>blog2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>cn.djc8</groupId>
<artifactId>eurekaclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eurekaclient</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.1.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
修改src\main\resources\application.properties
名称改为 src\main\resources\application.yml
修改src\main\resources\application.yml
内容如下
server:
port: 7003
spring:
application:
name: eurekaclient
eureka:
client:
serviceUrl:
defaultZone: http://localhost:7001/eureka/
修改src\main\java\cn\djc8\eurekaclient\EurekaclientApplication.java
1.5 运行Eureka Client
打开浏览器,输入:http://localhost
可以看到已经注册上了
浏览器中输入:http://localhost
浏览器中显示:
djc8.com
二. 总结
1.使用idea搭建环境,比较简单快捷,但是idea需要使用收费版才能正常的进行srping的开发。
2.eureka注册中心端口可以自定义,文中使用7001。
3.本篇参考:史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本)
3.附加上其他参考文章:
3.1 SpringCloud 注册中心 EureKa Server
3.2 spring-boot服务启动报: Unable to start embedded container
本文来自:Spring Cloud 学习笔记1 Eureka环境搭建-小码农,转载请保留本条链接,感谢!
- 本文标签: spring cloud eureka
- 本文链接: https://djc8.cn/archives/spring-cloud-learning-note-1.html
- 版权声明: 本文由小码农原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权