pmo文件依赖包
org.springframework.cloud spring-cloud-starter-netflix-eureka-server
1.1什么是服务治理org.springframework.cloud spring-cloud-starter-netflix-eureka-client
1.2什么是服务注册
- 架构图
1.4 构建单机euk服务注册
说明
我们构建3个模块模拟场景
80端口模拟消费者调用
8001端口模拟生产者生产
7001做euerka做服务注册
现在需要80端口跨模块调用8001端口的内容
1.4.1 构建7001EUK服务模块添加主类启动
@EnableEurekaServer//启动eureak注册中心
server:
port: 7001
eureka:
server:
# enable-self-preservation: false #关闭自我保护机制及时清理不可以用的服务机器
instance:
hostname: localhost
client:
register-with-eureka: false #表识不向注册中心注册自己
fetch-registry: false
service-url:
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ 单机版指向自己
defaultZone: http://www.fupengdeng.top:7002/eureka/ #设置与eureka集群版本 指向对方地址
1.4.2 配置8001
视图层业务
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
mybatis:
mapper-locations: classpath*:mapper
public interface LoadBalancer {
ServiceInstance instances(List serviceInstances);
}
在80端口创建轮训算法实体实现上述接口
@Component
public class MyLB implements LoadBalancer{
//
private AtomicInteger atomicInteger=new AtomicInteger(0);
//坐标
private final Integer getAndIncrement(){
Integer current = null;//当前的访问次数
Integer next=null;//下一次访问次数
do {
current=this.atomicInteger.get();
//整型越界重新从0开始
next=current>=2147483647?0:current+1;
}while (!this.atomicInteger.compareAndSet(current,next));
return next;
}
@Override
public ServiceInstance instances(List serviceInstances) {
//得到机器的列表
Integer index=getAndIncrement()%serviceInstances.size();//得到服务器的下标位置
return serviceInstances.get(index);
}
}
在80端口调用
@GetMapping(value = "/consumer/payment/lb")
public String getPaymentLB(){
//获取当前服务的实例
List instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
if (instances == null || instances.size() <= 0){
//判断是否有效实例
return null;
}
//调用自己写的轮训算法
ServiceInstance serviceInstance = loadBalancer.instances(instances);
URI uri = serviceInstance.getUri();
return restTemplate.getForObject(uri+"/payment/lb",String.class);
}
注释掉负载均衡注解
测试
可以实现轮训
5.OpenFeign服务接口调用 5.1 简介
- Feign和OpenFeign两者区别
根据我们之前讲的8001 8002 7001 7002的案例修改80端口做演示
- 依赖
org.springframework.cloud spring-cloud-starter-openfeign
- 配置信息
server:
port: 80
spring:
application:
name:
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://localhost:7001/eureka,http://www.fupengdeng.top:7002/eureka #如果是集群指向多个地址
#设置feign客户端超时时间openfeign默认支持ribbon
ribbon:
#建立连接超时时间
ReadTimeout: 5000
#建立连接后读取资源超时时间
ConnectTimeout: 5000
设置超时时间避免 时间差问题报错
- 启动类
注意在需要调用其他模块的 本模块的启动类上要添加EnableFeignClients注解
- 调用对外模块接口
接口的方法要和被调用模块的视图层方法一致不用写实体类。我们要显示的指明调用服务的服务名称。
- 视图层业务
先后启动7001 7002 8001 8002 80 端口 测试
并且帮我妈实现负载均衡的轮训效果
5.3 日志监控日志级别
- 设置日志配置类
package com.dfp.cloud.feign.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.Logger;
@Configuration
public class FeignLogConfig {
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
- 修改配置文件
重启80服务访问业务地址多次访问
控制台打印



