微服务架构概述:
springBoot和springCloud版本的关系:
Release Train | Boot Version |
---|---|
Hoxton | 2.2.x, 2.3.x (Starting with SR5) |
Greenwich | 2.1.x |
Finchley | 2.0.x |
Edgware | 1.5.x |
Dalston | 1.5.x |
注意springBoot和springCloud必须保持官网要求
springBoot官网:docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/htmlsingle/
项目构建:
约定>配置>编码
一、开始编码
1、编写父工程
建一个maven工程
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Q3HrFQY7-1597511041823)(C:UsersLENOVOAppDataRoamingTypora ypora-user-imagesimage-20200811230117732.png)]
2、设置编码
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LTfg23sE-1597511041824)(C:UsersLENOVOAppDataRoamingTypora ypora-user-imagesimage-20200811230659088.png)]
3、版本
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qeENbZXG-1597511041825)(C:UsersLENOVOAppDataRoamingTypora ypora-user-imagesimage-20200812091514945.png)]
4、开启注解
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TUROcdUx-1597511041827)(C:UsersLENOVOAppDataRoamingTypora ypora-user-imagesimage-20200812091538761.png)]
5、导入父依赖
<!--主要打包方式为Pom-->
<packaging>pom</packaging>
<!--统一管理jar包版本-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
<mysql.version>8.0.18</mysql.version>
<druid.version>1.1.16</druid.version>
<druid.spring.boot.starter.version>1.1.10</druid.spring.boot.starter.version>
<spring.boot.version>2.2.2.RELEASE</spring.boot.version>
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
<spring.cloud.alibaba.version>2.1.0.RELEASE</spring.cloud.alibaba.version>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
<mybatis-spring-boot-starter.version>2.1.1</mybatis-spring-boot-starter.version>
<hutool-all.version>5.1.0</hutool-all.version>
</properties>
<!--子模块继承后,提供作用:锁定版本+子module不用groupId和version-->
<dependencyManagement>
<dependencies>
<!--springboot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--Spring cloud Hoxton.SR1-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--Spring cloud alibaba 2.1.0.RELEASE-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.spring.boot.starter.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
Maven中dependencyManagement和denpendencies和dependency
dependencyManagement表示来提供了一种管理依赖版本号的方式
使用pom.xml中的dependencyManagement元素能让所有子项目中引用依赖不显示出版本号,maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后使用dependencyManagement元素指定版本号。
建立微服务的流程
二、子模块
1.1、建module
建立cloud-provider-payment8001的module项目
1.2、导入pom依赖
<description>支付模块</description>
<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>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<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>
<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>
</dependencies>
1.3、写yml
#端口
server:
port: 8001
#服务名称
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
#mapperLocationsPa
#type-aliases-package
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.xiaohe.springcloud.entities
1.4、主启动
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class);
}
}
1.5、业务类
entities模块
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
private Long id;
private String serial;
}
编写统一返回的实体类
public class CommonResult <T>
{
private Integer code;
private String message;
private T data;
public CommonResult(Integer code, String message) {
this.code = code;
this.message = message;
}
}
dao接口注意添加mapper注解
@Mapper
public interface PaymentDao
{
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
Mapper文件注意写resultMap控制字段名一致
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xiaohe.springcloud.dao.PaymentDao">
<!--添加-->
<insert id="create" parameterType="com.xiaohe.springcloud.entities.Payment" useGeneratedKeys="true" keyProperty="id">
insert into payment(serial) values (#{serial});
</insert>
<resultMap id="BaseResultMap" type="com.xiaohe.springcloud.entities.Payment">
<!--column数据库字段property实体类字段 -->
<id column="id" property="id" jdbcType="BIGINT"></id>
<id column="serial" property="serial" jdbcType="VARCHAR"></id>
</resultMap>
<select id="getPaymentById" resultType="com.xiaohe.springcloud.entities.Payment" parameterType="long" resultMap="BaseResultMap">
select * from payment where id=#{id}
</select>
</mapper>
service接口
public interface PaymentService {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
serviceImpl实现类注意写service注解
@Service
public class PaymentServiceImpl implements PaymentService {
@Resource
private PaymentDao paymentDao;
@Override
public int create(Payment payment) {
return paymentDao.create(payment);
}
@Override
public Payment getPaymentById(Long id) {
return paymentDao.getPaymentById(id);
}
}
Controller
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@PostMapping(value = "/payment/create")
public CommonResult create(Payment payment){
int result = paymentService.create(payment);
log.info("*******插入结果:"+result);
if(result>0){
return new CommonResult(200,"插入数据库成功",result);
}else{
return new CommonResult(444,"插入数据库失败",null);
}
}
@GetMapping(value = "/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id){
Payment payment = paymentService.getPaymentById(id);
log.info("*******插入结果:"+payment);
if(payment !=null){
return new CommonResult(200,"查询成功",payment);
}else{
return new CommonResult(444,"查询失败",null);
}
}
}
查询结果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-V1ADo5P4-1597511041828)(C:UsersLENOVOAppDataRoamingTypora ypora-user-imagesimage-20200812132335254.png)]
采用postman测试post请求
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vQvMAmcv-1597511041828)(C:UsersLENOVOAppDataRoamingTypora ypora-user-imagesimage-20200812134355993.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AgHXqFj0-1597511041829)(C:UsersLENOVOAppDataRoamingTypora ypora-user-imagesimage-20200812134413313.png)]
测试成功
补充:热部署配置
1.导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
2、添加插件到父类总工程
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5K8EnvVZ-1597511041830)(C:UsersLENOVOAppDataRoamingTypora ypora-user-imagesimage-20200812135653176.png)]
3、开启自动编译的选项
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2F3zzKJv-1597511041830)(C:UsersLENOVOAppDataRoamingTypora ypora-user-imagesimage-20200812135946313.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WEkHLc2h-1597511041831)(C:UsersLENOVOAppDataRoamingTypora ypora-user-imagesimage-20200812140130710.png)]
Updata the value of
快捷键:ctrl+shift+Alt+/ 选择Registry
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-azMi4cwZ-1597511041831)(C:UsersLENOVOAppDataRoamingTypora ypora-user-imagesimage-20200812140317522.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-edIWULs0-1597511041832)(C:UsersLENOVOAppDataRoamingTypora ypora-user-imagesimage-20200812140424705.png)]
配置之后重启即可
2.1建model
cloud-consumer-order80模块
2.2、导入pom依赖
<description>订单消费者</description>
<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>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<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>
</dependencies>
2.2写yml
server:
port: 800
2.3.主启动类
@SpringBootApplication
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}
分析:consumer模块需要调用payment模块,端口不一样
需要借助RestTemplate
官网:https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html
2.4、编写配置类
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
2.5、业务类
entities实体类和payment的一样copy即可
Controller
@RestController
@Slf4j
public class OrderController {
public static final String PAYMENT_URL="http://localhost:8001";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/create")
public CommonResult<Payment> create(Payment payment){
/**
* url表示访问的8001端口的/payment/create方法
* request:表示参数
* Class<T> responseType:表示返回的类
* */
return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
}
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
return restTemplate.getForObject(PAYMENT_URL+"//payment/get/"+id,CommonResult.class);
}
}
测试:查询成功
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-J3n174bp-1597511041832)(F:笔记微服务架构概述:.assetsimage-20200812164550265.png)]
测试:插入
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4FBvJyaT-1597511041833)(F:笔记微服务架构概述:.assetsimage-20200812164806427.png)]
注意显示成功但是数据库
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7eX6vwcb-1597511041833)(F:笔记微服务架构概述:.assetsimage-20200812164852989.png)]
注意:在prodiver要加上@RequestBody
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PU656znG-1597511041834)(F:笔记微服务架构概述:.assetsimage-20200812170447682.png)]
测试插入成功注意@RequestBody
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eVkmVxYg-1597511041834)(F:笔记微服务架构概述:.assetsimage-20200812171507963.png)]
工程重构
系统中的entities重复,需要重构
新建
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-O1i4wOJE-1597511041835)(F:笔记微服务架构概述:.assetsimage-20200812181246260.png)]
POM
<description>公共模块</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
//工具类
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
Hutool 就是这么一款超级强力的工具类。日期与字符串转换
功能: 日期与字符串转换
文件操作
转码与反转码
随机数生成
压缩与解压
编码与解码
CVS文件操作
缓存处理
加密解密
定时任务
邮件收发
二维码创建
FTP 上传与下载
图形验证码生成
entities:将公共的entities复制到这个目录里面
maven命令clean install
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3JghrIud-1597511041835)(F:笔记微服务架构概述:.assetsimage-20200812182558358.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dS8Y8f1F-1597511041836)(F:笔记微服务架构概述:.assetsimage-20200812182659506.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LavrjxGs-1597511041836)(F:笔记微服务架构概述:.assetsimage-20200812182737040.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iE3xsQRQ-1597511041837)(F:笔记微服务架构概述:.assetsimage-20200812182820370.png)]
订单80和支付8001分别改造
1、删除customer和payment各自的entities文件夹
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rFsKwuqZ-1597511041837)(F:笔记微服务架构概述:.assetsimage-20200812183017647.png)]
删除之后导入公共模块的pom坐标
<dependency>
<groupId>com.xiaohe</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
测试:成功实现[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bBaISXdD-1597511041837)(F:笔记微服务架构概述:.assetsimage-20200812183730625.png)]
工程样图
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nGQMQiiP-1597511041838)(F:笔记微服务架构概述:.assetsimage-20200812190224482.png)]
三、服务注册与发现
EUREKA
1、Eureka
1.1、概况:
Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hPvBmeyP-1597511041838)(F:笔记微服务架构概述:.assetsimage-20200813084550416.png)]
Eureka包含两个组件:Eureka Server和Eureka Client。
Eureka Server:提供服务注册服务
Eureka Client:通过注册中心进行访问
1.2、安装
1.2.1、IDEA生成eurekaServer端服务注册中心类似物业中心
建module
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZTsXeWrl-1597511041839)(F:笔记微服务架构概述:.assetsimage-20200813085454744.png)]
改pom
<description>eureka服务注册中心的服务端</description>
<dependencies>
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-common</artifactId>
<version>${project.version}</version>
</dependency>
<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>
<!--一般为通用配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<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>
</dependencies>
写yml
server:
port: 7001
#Eureka Server
eureka:
instance:
hostname: localhost #euraka服务端的实例名称
client:
#false表示不向注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心,职责是维护服务实例,不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server 交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
主启动
@SpringBootApplication
/**声明注册中心*/
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
启动:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rNp7RJrM-1597511041839)(F:笔记微服务架构概述:.assetsimage-20200813091555903.png)]
1.3、入驻注册中心
EurekaClient端cloud-provider-payment8001,注册进EurekaServer成为服务的提供者provider,类似尚硅谷学校对外提供授课服务。
payment8001入驻注册中心
步骤:
修改cloud-provider-payment8001的配置
2、改cloud-provider-payment8001pom
<!--eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
3、写yml
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
# defaultZone: http://localhost:7001/eureka
# 单机版
defaultZone: http://localhost:7001/eureka
4、主启动
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
测试
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Etlml2Wh-1597511041840)(F:笔记微服务架构概述:.assetsimage-20200813093832082.png)]
5、自我保护机制
红色报错
将customer以同样的方式入驻Eukera注册中心
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AheKb4Cc-1597511041840)(F:笔记微服务架构概述:.assetsimage-20200813095459648.png)]
测试结果:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Lex0PPyS-1597511041841)(F:笔记微服务架构概述:.assetsimage-20200813095522616.png)]
Eureka集群说明:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7985VsYt-1597511041841)(F:笔记微服务架构概述:.assetsimage-20200813100824766.png)]
1.4、搭建Eureka集群
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-22yhUeA0-1597511041842)(F:笔记微服务架构概述:.assetsimage-20200813101719804.png)]
互相包含的关系
搭建集群步骤:
1、参考cloud-eureka-server7001 新建cloud-eureka-server7002
2、改pom 和cloud-eureka-server7001一样
3、修改映射配置
找到C:WindowsSystem32driversetc路径下hosts文件
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EBNmRVpa-1597511041842)(F:笔记微服务架构概述:.assetsimage-20200813102737440.png)]
修改host文件需要首先关闭只读模式,给予用户权限方能修改
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qOgGVm1U-1597511041843)(F:笔记微服务架构概述:.assetsimage-20200813112055771.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FLi5tm2G-1597511041843)(F:笔记微服务架构概述:.assetsimage-20200813112128423.png)]
4、写Yml 7001yml
server:
port: 7001
#Eureka Server
eureka:
instance:
hostname: eureka7001.com #euraka服务端的实例名称
client:
#false表示不向注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心,职责是维护服务实例,不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server 交互的地址查询服务和注册服务都需要依赖这个地址
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://eureka7002.com/eureka/
改变的内容
eureka:
instance:
hostname: eureka7001.com #euraka服务端的实例名称
defaultZone: http://eureka7002.com/eureka/
写7002yml
server:
port: 7002
#Eureka Server
eureka:
instance:
hostname: eureka7002.com #euraka服务端的实例名称
client:
#false表示不向注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心,职责是维护服务实例,不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server 交互的地址查询服务和注册服务都需要依赖这个地址
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://eureka7001.com/eureka/
5、主启动
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7002 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7002.class,args);
}
}
测试:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pbnXUQRv-1597511041844)(F:笔记微服务架构概述:.assetsimage-20200813113533281.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SgOJCmpv-1597511041844)(F:笔记微服务架构概述:.assetsimage-20200813113616126.png)]
集群搭建成功
Zookeeper
2.1、建module
建立cloud-provider-payment8004模块
2.2、导入pom依赖
<description>Zookeeper服务提供者</description>
<dependencies>
<dependency>
<groupId>com.xiaohe</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringBoot整合Zookeeper客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<exclusions>
<!--先排除自带的zookeeper3.5.3-->
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<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>
<!--添加zookeeper3.4.9版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
</dependencies>
2.3、写yml
server:
# 8004表示注册到zookeeper服务器的支付服务提供者端口号
port: 8004
spring:
application:
# 服务别名---注册zookeeper到注册中心的名称
name: cloud-provider-payment
cloud:
zookeeper:
# 默认localhost:2181
connect-string: 192.168.238.129:2181
2.4、启动项
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@RequestMapping(value = "/payment/zk")
public String paymentzk(){
return "springcloud with zookeeper :"+serverPort+" "+ UUID.randomUUID().toString();
}
}
首先下载zookeeper,我的是zookeeper3.4.9
[root@localhost ~]# cd ..
[root@localhost /]# ls
bin dev home lib64 mnt opt root sbin sys usr
boot etc lib media myzookeeper proc run srv tmp var
[root@localhost /]# cd myzookeeper
[root@localhost myzookeeper]# ls
[root@localhost zookeeper-3.4.9]# ls
bin dist-maven LICENSE.txt src
build.xml docs NOTICE.txt zookeeper-3.4.9.jar
CHANGES.txt ivysettings.xml README_packaging.txt zookeeper-3.4.9.jar.asc
conf ivy.xml README.txt zookeeper-3.4.9.jar.md5
contrib lib recipes zookeeper-3.4.9.jar.sha1
[root@localhost zookeeper-3.4.9]# cd bin
[root@localhost bin]# ls
README.txt zkCli.cmd zkEnv.cmd zkServer.cmd zookeeper.out
zkCleanup.sh zkCli.sh zkEnv.sh zkServer.sh
[root@localhost bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /myzookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg
Starting zookeeper ... zSTARTED
[root@localhost bin]# zkCli.sh
bash: zkCli.sh: 未找到命令...
[root@localhost bin]# ./zkCli.sh
[root@localhost bin]# ifconfig
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.238.129 netmask 255.255.255.0 broadcast 192.168.238.255
inet6 fe80::d96b:78df:1ac6:65 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:06:2b:e3 txqueuelen 1000 (Ethernet)
RX packets 145 bytes 11063 (10.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 84 bytes 9733 (9.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 220 bytes 18730 (18.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 220 bytes 18730 (18.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:3d:01:50 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost bin]# ls
README.txt zkCleanup.sh zkCli.cmd zkCli.sh zkEnv.cmd zkEnv.sh zkServer.cmd zkServer.sh zookeeper.out
[root@localhost bin]# ./zkCli.sh
Connecting to localhost:2181
####
[zk: localhost:2181(CONNECTED) 0] ls /
[services, zookeeper]
##3
[zk: localhost:2181(CONNECTED) 1] ls get service
Command failed: java.lang.IllegalArgumentException: Path must start with / character
###
[zk: localhost:2181(CONNECTED) 2] get service
Command failed: java.lang.IllegalArgumentException: Path must start with / character
[zk: localhost:2181(CONNECTED) 3] get /service
Node does not exist: /service
[zk: localhost:2181(CONNECTED) 4] ls /service
Node does not exist: /service
[zk: localhost:2181(CONNECTED) 5] ls /
[services, zookeeper]
###
[zk: localhost:2181(CONNECTED) 6] ls /services
[cloud-provider-payment]
[zk: localhost:2181(CONNECTED) 7]