这篇文章是在看了多篇文章之后写的 在此致谢几位作者!
整体的流程就是先创建一个空的父级maven工程用来管理jar包 被子module继承 ,然后创建一个子module模块eureka服务注册中心,创建两个子module作为eureka client注册到服务中心,然后实现两个微服务之间的通信。
眼看千遍 不如手过一遍 直接切入正题:
step1:
idea创建一个空的maven 父级工程 basepro 用来管理公共的jar包
创建之后删除src r文件夹,pom文件修改打包方式为pom 添加modules申明子模块 在子模块中也要通过parent标签继承父级的资源
<?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>com.etothree</groupId>
<artifactId>basepro</artifactId>
<version>1.0-SNAPSHOT</version>
<!--注意打包方式为 pom -->
<packaging>pom</packaging>
<!--添加子模块标识 !!!! -->
<modules>
<!--声明 eureka_server 为当前项目 子模块 ,以后有新的子模块添加也要在此添加-->
<module>serverpro</module>
<module>webpro</module>
<module>webpro2</module>
</modules>
<name>basepro</name>
<description>主工程--用来控制公共版本</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</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>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step2 创建eureka服务注册中心:
点击basepro 右键 new module groupid与父级工程保持一致 选择eureka server
pom文件:
<?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>com.etothree</groupId>
<artifactId>serverpro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>serverpro</name>
<description></description>
<!-- 确认继承关系 -->
<parent>
<!--父工程 gropuId-->
<groupId>com.etothree</groupId>
<!--父工程 artifactId-->
<artifactId>basepro</artifactId>
<!--父工程 版本-->
<version>1.0-SNAPSHOT</version>
</parent>
<!--spring cloud eureka server 必须依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
parent写上父级工程 然后在父级工程dependencyManagement引入包 不需要写版本
application.yml内容如下
# 本身是注册中心,但是也需要注册,这里可以向自己注册
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
# 不在页面上显示当前 服务
register-with-eureka: false
# 为true时,可以启动,但报异常:Cannot execute request on any known server
fetch-registry: false
# 当应用上线率过低时,eureka会启动保护机制,默认确定当前服务是开启的,开发 环境中尽量关闭(设置为false)
server:
enable-self-preservation: false
# 服务名 ,很重要 ,后期 config 配置中心会以 服务名为标识找对应的 服务
spring:
application:
name: eureka
# 设置当前项目端口号
server:
port: 8761
然后在程序入口处声明这是一个eureka server;
启动项目这时候访问地址:http://localhost:8761/
step3 创建第一个服务提供的子module webpro2
右键basepro new module 这里选择是eureka client 和spring web
pom文件:
<?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>
<groupId>com.etothree</groupId>
<artifactId>webpro2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>webpro2</name>
<description>webpro2</description>
<!-- 确认继承关系 -->
<parent>
<!--父工程 gropuId-->
<groupId>com.etothree</groupId>
<!--父工程 artifactId-->
<artifactId>basepro</artifactId>
<!--父工程 版本-->
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
-->
</dependencies>
<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>
application.yml内容:
# 注册到 eureka server 配置
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
server:
port: 8763
spring:
#数据源配置
datasource:
url: jdbc:mysql://localhost:3306/zz
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
application:
name: eurekaclient2
# redis:
# database: 0 # Redis服务器数据库
# host: 127.0.0.1 # Redis服务器地址
# port: 6379 # Redis服务器连接端口
# password: 123456 # Redis服务器连接密码(默认为空)
# timeout: 6000ms # 连接超时时间(毫秒)
# jedis:
# pool:
# max-active: 200 # 连接池最大连接数(使用负值表示没有限制)
# max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
# max-idle: 10 # 连接池中的最大空闲连接
# min-idle: 0 # 连接池中的最小空闲连接
mybatis:
#实体类所做包
type-aliases-package: com.etothree.webpro2.entity
#mapper.xml所在位置
mapper-locations: classpath:mappers/*.xml
随后开放一个接口:
step4 创建一个服务调用的子module webpro
同step3步骤一样 只是名字不同
pom文件内容如下:
<?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>
<groupId>com.etothree</groupId>
<artifactId>webpro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>webpro</name>
<description>webpro</description>
<!-- 确认继承关系 -->
<parent>
<!--父工程 gropuId-->
<groupId>com.etothree</groupId>
<!--父工程 artifactId-->
<artifactId>basepro</artifactId>
<!--父工程 版本-->
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
-->
</dependencies>
<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>
application.yml内容如下:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
server:
port: 8762
spring:
datasource:
url: jdbc:mysql://localhost:3306/zz
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
application:
name: eurekaclient
mybatis:
type-aliases-package: com.etothree.webpro.entity
mapper-locations: classpath:mappers/*.xml
app:
service-url: http://EUREKACLIENT2/ ##这里是要调用的服务的地址
然后创建一个service 用来调用注册对接注册中心的服务
创建一个controller进行调用:
启动服务提供模块 和服务调用模块 访问 http://localhost:8761/
然后单独访问服务提供模块的接口如下:
、
通过服务调用模块来访问效果如下:
在这里 基础的两个微服务通信就实现了。