原创

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

1.jpg

2.jpg

3.jpg

打开POM.XML ,增加红圈中的内容。

4.jpg

5.jpg

<?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

6.jpg

7.jpg

8.jpg

项目的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

10.jpg

1.3 运行Eureka Server

12.jpg

日志信息

13.jpg

打开浏览器,输入:http://localhost:7001/

11.jpg

至此Eureka注册中心已经运行成功

1.4 新建Eureka Client Module

14.jpg

15.jpg

修改clientpom.xml

<parent>
    <groupId>cn.djc8</groupId>
    <artifactId>blog2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

此时clientpom.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

17.jpg

1.5 运行Eureka Client

16.jpg

打开浏览器,输入:http://localhost

18.jpg

可以看到已经注册上了

浏览器中输入: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

温馨提示:
本文最后更新于 2020年08月18日,已超过 727 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我
正文到此结束
该篇文章的评论功能已被站长关闭
本文目录