第五节 Eureka客户端

亮子 2021-06-08 12:19:41 17539 0 0 0

1、user服务增加Eureka客户端功能

1)、增加Eureka客户端依赖

<?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.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mazong</groupId>
    <artifactId>server-blog-user</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>server-blog-user</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.RC2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

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

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

2)、启动类增加@EnableEurekaClient注解

package com.mazong.serverbloguser;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ServerBlogUserApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerBlogUserApplication.class, args);
    }

}

3)、修改配置文件

## application.properties
spring.application.name=server-blog-user
server.port=8000

## MyBatis 数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/db_user?autoReconnect=true&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

## mybatis
# 对应实体类的包名
mybatis.typeAliasesPackage=com.mazong.serverbloguser.mapper
# mapper.xml文件所在位置
# mybatis.mapperLocations=classpath:**/mapper/*.xml
logging.level.com.mazong.serverbloguser.mapper=debug

# eureka client
eureka.client.service-url.defaultZone=http://localhost:6060/eureka/
eureka.instance.prefer-ip-address=true

4)、编译运行

图片alt

注意:先运行eureka server,然后再运行server-blog-user服务

2、article服务增加客户端功能

1)、集成Eureka客户端功能

集成步骤和方法和上一节一模一样。

在pom.xml文件中增加依赖
在启动类中增加注解@EnableEurekaClient
在配置文件application.properties中增加连接Eureka服务的信息

2)、编译运行

图片alt

3、Eureka服务和客户端搭建时注意事项

  • SpringCloud的版本问题

SpringCloud官网的首页介绍了SpringBoot和SpringCloud之间版本的关系,官网地址如下:

https://spring.io/projects/spring-cloud

具体对应关系如下:

图片alt

  • 依赖包的引入问题(共有3段)
    <properties>
        <spring-cloud.version>Hoxton.RC2</spring-cloud.version>
    </properties>
    <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>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>