前言:上一章节我们介绍了Eureka服务端的安装与配置,本章节则介绍关于微服务如何入职Eureka Server
Eureka架构原理图
1、修改provider-payment8001子模块的pom.xml文件,引入Eureka Clinet的依赖,然后reolad一下,下载依赖
例:
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">
<parent>
<artifactId>springcloud01</artifactId>
<groupId>com.ken.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>provider-payment8001</artifactId>
<dependencies>
<!--以下依赖都没写版本号,没写版本号的情况下会引用父项目的版本-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--Mybatis和SpringBoot的整合-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--引入自己定义的api调用包,可以使用Payment模块的entity-->
<dependency>
<groupId>com.ken.springcloud</groupId>
<artifactId>api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--Eureka Clinet-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
2、修改provider-payment8001子模块的application.yml文件配置的内容,将Eureka相关配置引入
XML
#服务端口号
server:
port: 8001
#服务名称
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型
driver-class-name: com.mysql.cj.jdbc.Driver #mysql驱动包(mysql驱动包版本是5的要写成com.mysql.jdbc.Driver)
url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8 #mysql连接路径,记得把cloud这个数据库名改成自己的,或者新建名为cloud的数据库
username: root
password: 123456
eureka:
client:
#表示是否将自己注册进Eureka Server里,默认为true
register-with-eureka: true
#是否从Eureka Server抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
#mybatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml #扫描类路径下的mapper文件夹下所有的.xml配置文件
type-aliases-package: com.ken.springcloud.entities #该包下的所有Entity类都取默认别名
效果图:
3、修改provider-payment8001子模块的主启动类,将provider-payment8001子模块标注为Eureka Client
java
package com.ken.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
//标注为Eureka Client(服务注册中心)
@EnableEurekaClient
public class PaymentMain {
public static void main(String[] args) {
SpringApplication.run(PaymentMain.class, args);
}
}
效果图:
4、启动provider-payment8001子模块的主启动类,测试provider-payment8001子模块是否成功注册进Eureka
注意:要显启动Eureka Server,然后再启动Eureka Client
效果图:
若看到Instances currently registered with Eureka下面多出一行名为cloud-pamyent-service的服务的相关信息,则证明provider-payment8001子模块成功注册进了Eureka,如下图所示:
注:我们在application.yml配置文件里的写的服务名称就是注册在Eureka Server里的名称