在Eureka架构中,微服务角色有两类:
EurekaServer:服务端,注册中心
记录服务信息
心跳监控
EurekaClient:客户端
Provider:服务提供者,例如案例中的user-service
注册自己的信息到EurekaServer
每隔30s向EurekaServer发送心跳
consumer:服务消费者,例如案例中的order-service
根据服务名称从EurekaServer拉取服务列表
基于服务列表做负载均衡,选中一个微服务后发起远程调用
具体注册中心流程
- 0: 实例化提供者,消费者,EureKa.
- 1:将提供者注册到服务中心
- 2.注册中心收录
- 3.提供者通过EureKa地址,获取提供者列表
- 4.基于负载均衡算法选择列表中任一地址使用
- 5.提供者每隔30s,向EureKa发送一次健康状态
- 6.如果异常被EureKa剔除
搭建eureka模块
首先注册中心服务端:eureka,这必须是一个独立的微服务
1.服务端依赖如下
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.配置POM文件中EureKa地址及其名称
server:
port: 8082 #端口号
spring:
application:
# 服务名称
name: eurekaserever
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8082/eureka/
# 地址信息
3.在启动类添加注解
@SpringBootApplication
//添加注解开关
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
提供两个角色(boot+mybatis-plus+MVC): 消费者,提供者
消费者需要添加EureKa客户端依赖
<!-- Eureka客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
1.在yml文件写入如下配置,如果运行时抛Public Key Retrieval is not allowed异常,在datasource下的url 的数据库名拼接
?&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
如果没有解决,有可能数据库密码错误
server:
port: 8080
spring:
application:
name: order #当前模块名称,在EureKa中显示
datasource:
url: jdbc:mysql://localhost:3306/atguigudb
username: root
password: ******
driver-class-name: com.mysql.jdbc.Driver
mybatis:
type-aliases-package: cn.itcast.user.pojo
configuration:
map-underscore-to-camel-case: true
logging:
level:
cn.itcast: debug
pattern:
dateformat: MM-dd HH:mm:ss:SSS
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8082/eureka/ #EureKa模块定义的地址一致
2.使用配置类
//声明配置类
@Configuration
public class configuration {
//添加负载均衡注解
@LoadBalanced
//可以用RestTemplate对象发http请求
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
3.Service层逻辑
http://user/user/id?id="+ order.getUserId(); 第一个user:为在EurreKa的提供者名称,之后的wed就是调用该接口即可
@Service
public class OrderService {
@Autowired
private OrderMapper orderMapper;
//注入RestTemplate
@Autowired
private RestTemplate RestTemplate;
public Order queryOrderById(Long orderId) {
// 1.查询订单
Order order = orderMapper.findById(orderId);
//2调用api
String u="http://user/user/id?id="+ order.getUserId();
//3形参2 :JOSN序列化指定对象
User forObject = RestTemplate.getForObject(u, User.class);
order.setUser(forObject);
// 4.返回
return order;
}
}
二:声明提供者
1.引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.配置yml文件,后面的Service层,wed层正常写就可以,不用必须按上面逻辑
server:
port: 8080
spring:
application:
name: order
datasource:
url: jdbc:mysql://localhost:3306/atguigudb?&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: Q132457q
driver-class-name: com.mysql.jdbc.Driver
mybatis:
type-aliases-package: cn.itcast.user.pojo
configuration:
map-underscore-to-camel-case: true
logging:
level:
cn.itcast: debug
pattern:
dateformat: MM-dd HH:mm:ss:SSS
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8082/eureka/
3.点击服务->点击提供者对象Ctrl+D 进入,填写-Dserver.port=8083,这个是为了体现负载均衡算法
4,4个项目全部启动,之后在http://localhost/8082
就是你在第一个项目EureKa的yml的地址