1. 简单搭建微服务框架
1.1 idea创建maven多模块项目
- 直接看下面的文章
idea创建maven多模块项目.
1.2 项目结构
- 简单介绍一下:
- 父工程:
dog-cloud-parent
- 管理实体项目:
dog-po
- 微服务-服务提供者:
dog-provider-8001
- 微服务-服务消费者:
dog-consumer-80
- 父工程:
- 如下:
1.3 项目依赖与配置
1.3.1 父工程:dog-cloud-parent
-
pom如下:
<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.liu.susu</groupId> <artifactId>dog-cloud-parent</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>dog-cloud-parent</name> <url>http://maven.apache.org</url> <modules> <module>dog-po</module> <module>dog-provider-8001</module> <module>dog-consumer-80</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring-boot.version>1.5.9.RELEASE</spring-boot.version> <junit.version>3.8.1</junit.version> <log4j.version>1.2.17</log4j.version> <lombok.version>1.16.8</lombok.version> <java.version>1.8</java.version> <mysql-connector.version>8.0.15</mysql-connector.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!--注意驱动选择,如果mysql安装的是5.7的,可以用5.0.4,如果安装的是8.x的,要用8.0.15--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <!-- <version>5.0.4</version>--> <version>${mysql-connector.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.31</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
1.3.2 管理实体项目:dog-po
- 如下:
<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> <parent> <groupId>com.liu.susu</groupId> <artifactId>dog-cloud-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>dog-po</artifactId> <packaging>jar</packaging> <name>dog-po</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.26</version> </dependency> </dependencies> </project>
1.3.3 服务提供者:dog-provider-8001
1.3.3.1 简单项目结构
- 如下:
1.3.3.2 pom文件
- 如下:
<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> <parent> <groupId>com.liu.susu</groupId> <artifactId>dog-cloud-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>dog-provider-8001</artifactId> <packaging>jar</packaging> <name>dog-provider-8001</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.liu.susu</groupId> <artifactId>dog-po</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!--下面这几个,版本同${spring-boot.version}--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <!--热部署 版本同${spring-boot.version}--> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
1.3.3.3 yml文件
- 如下:
server: port: 8001 spring: application: name: dog-provider datasource: type: com.alibaba.druid.pool.DruidDataSource # driver-class-name: org.gjt.mm.mysql.Driver # mysql5.7的 driver-class-name: com.mysql.cj.jdbc.Driver # mysql8.x版本的 # url: jdbc:mysql://localhost:3306/dogCloud url: jdbc:mysql://localhost:3306/dogCloud?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false username: root password: susu@123 dbcp2: min-idle: 5 # 数据库连接池的最小维持连接数 initial-size: 5 # 初始化连接数 max-total: 5 # 最大连接数 max-wait-millis: 200 # 等待连接获取的最大超时时间 mybatis: config-location: classpath:mybatis/mybatis-config.xml type-aliases-package: com.liu.susu.pojo # 所有entity实体别名类实体所在包 mapper-locations: - classpath:mybatis/mapper/**/*.xml
1.3.4 服务消费者:dog-consumer-80
1.3.4.1 项目目录结构
- 如下:
1.3.4.2 pom文件
- 如下:
<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> <parent> <groupId>com.liu.susu</groupId> <artifactId>dog-cloud-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>dog-consumer-80</artifactId> <description>微服务消费者-狗狗</description> <packaging>jar</packaging> <name>dog-consumer-80</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.liu.susu</groupId> <artifactId>dog-po</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!--下面这几个,版本同${spring-boot.version}--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--热部署 版本同${spring-boot.version}--> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
1.3.4.3 yml文件
- 如下:
1.3.4.4 配置类
- 如下:
package com.liu.susu.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; /** * @Description * @Author susu */ @Configuration public class ConfigBean { @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
1.4 访问
1.4.1 直接访问服务提供者
- 如下:
1.4.2 访问服务消费者
- 如下:
2. 建立EurekaServer服务注册中心(本地)
2.1 创建子模块—EurekaServer服务注册中心
- 在上面基础上再创建一个子模块,如下:
2.2 pom文件
-
如下:
<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> <parent> <groupId>com.liu.susu</groupId> <artifactId>dog-cloud-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>dog-eureka-7001</artifactId> <packaging>jar</packaging> <name>dog-eureka-7001</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- eureka-server 服务端 版本号继承父工程 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> </project>
2.3 yml文件
-
如下:
server: port: 7001 eureka: instance: hostname: localhost # eureka服务端的实例名 client: register-with-eureka: false # false表示不向注册中心注册自己 fetch-registry: false # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 service-url: # 设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2.4 启动类
- 注意加注解
@EnableEurekaServer
,如下:
3. 将微服务注册进EurekaServer服务注册中心(本地)
3.1 将服务提供者注册到EurekaServer服务注册中心
3.1.1 修改pom文件
- 如下:
<!--引入eureka-client(eureka客户端),将微服务provider注册进eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
3.1.2 修改yml文件
- 如下:
eureka: client: # 客户端注册进eureka服务列表内 service-url: defaultZone: http://localhost:7001/eureka
3.1.3 启动类加注解
- 如下:
@EnableEurekaClient
3.1.4 测试
- 启动EurekaServer 和 服务提供者,查看注册情况,如下:
3.1.5 控制访问路径显示IP地址
- 如下
3.1.6 完善微服务info内容
- 完善前的如下:
要想完善,如下:
3.1.6.1 修改pom文件
3.1.6.1.1 微服务的pom文件
-
如下:
<!--完善微服务的info (actuator监控信息完善),版本同${spring-boot.version}--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
3.1.6.1.2 父工程的pom文件
- 添加build构建信息,如下:
<build> <finalName>dog-cloud-parent</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <!--过滤开启--> </resource> </resources> <plugins> <plugin> <!--插件负责解析和解读--> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> </plugin> </plugins> </build>
- 另外,关于maven的其他配置可以看下面的文章,如下
Maven中的小学问(版本问题、打包问题等).
3.1.6.2 修改yml文件
- 添加下面内容,如下:
info: app.name: demo-dog-cloud-parent company.name: com.liu.susu build.artifactId: ${project.artifactId} build.version: ${project.version}
3.1.6.3 观看效果
- 如下:
4. Eureka服务发现(本地)
4.1 提供发现服务的接口
4.1.1 服务提供者
- 如下:
package com.liu.susu.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @Description * @Author susu */ @RestController public class EurekaController { @Autowired private DiscoveryClient discoveryClient; @RequestMapping("/services/discoveryServices") public Object discoveryServices(){ List<String> serviceList = discoveryClient.getServices(); System.out.println(serviceList); Map<String, List<ServiceInstance>> serviceMap = new HashMap<>(); for (String clientStr : serviceList) { List<ServiceInstance> instances = discoveryClient.getInstances(clientStr.toUpperCase()); serviceMap.put(clientStr,instances); } return serviceMap; } }
4.1.2 服务消费者
-
如下:
文章来源:https://www.toymoban.com/news/detail-565281.htmlpackage com.liu.susu.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; /** * @Description * @Author susu */ @RestController public class EurekaConsumerController { private static final String REST_URL_PREFIX = "http://localhost:8001";//服务提供者的端口8001 @Autowired private RestTemplate restTemplate; @RequestMapping("/consumer/services/discoveryServices") public Object discoveryServices(){ String url = REST_URL_PREFIX + "/services/discoveryServices"; return restTemplate.getForObject(url, Object.class); } }
4.2 查看效果
- 服务提供者,如下:
- 服务消费者,如下:
5. Eureka集群
5.1 Eureka单机(远程服务器)
5.1.1 创建Eureka Server项目
- 为了整个微服务项目看着简洁,就把Eureka Server项目单独拿出来了,重新创建如下
5.1.2 项目简单配置
5.1.2.1 pom文件
- 如下:
<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.liu.susu</groupId> <artifactId>cloud-eureka</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>cloud-eureka</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.3.1.RELEASE</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.*</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.9.RELEASE</version> <configuration> <mainClass>com.liu.susu.App</mainClass> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> <finalName>${project.name}</finalName> </build> </project>
5.1.2.2 yml文件
- 如下:
server: port: 2886 eureka: instance: hostname: eureka-142 # eureka服务端的实例名 client: register-with-eureka: false # false表示不向注册中心注册自己 fetch-registry: false # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
5.1.2.3 启动类
- 如下:
5.1.3 服务提供者yml配置
- 地址换成启动服务的IP地址,如下:
5.1.4 启动看效果
- 如下:
5.2 Eureka集群(远程服务器)
5.2.1 EurekaServer准备yml配置文件
- 我这里直接准备了3个,当然一个也行,如果你部署的时候不嫌麻烦,可以修改一个,我这里为了部署方便,直接3套备好,如下:
- 每次只需要修改环境即可:
- 配置IP时,逗号后面不能有空格(注意!注意!注意!),通用配置如下:
# 1. 集群配置---175机器上 server: port: 2886 eureka: instance: hostname: eureka-142 # eureka服务端的实例名 client: register-with-eureka: false # false表示不向注册中心注册自己 fetch-registry: false # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 service-url: # 配置其他两台机器的IP defaultZone: http://IP2:2886/eureka/,http://IP3:2886/eureka/ # 注意:逗号后面不能有空格!!!!
5.2.2 微服务修改yml文件
- 这里的微服务还是上面的服务提供者,如下:
5.2.3 启动Eureka集群 和 微服务
- 这里的微服务就是上面的服务提供者,效果如下:
文章来源地址https://www.toymoban.com/news/detail-565281.html
6. 附项目
- 如下:
1-Eureka服务注册与发现以及Eureka集群搭建(实操型).
到了这里,关于1-Eureka服务注册与发现以及Eureka集群搭建(实操型)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!