【Spring Cloud教程入门到精通】Eureka:Spring Cloud服务注册与发现组件(非常详细)二

4. 搭建服务提供者
  1. 在主工程下创建一个名为 micro-service-cloud-provider-dept-8001 的 Spring Boot Module,并在其 pom.xml 中引入以下依赖。
1.  <?xml version="1.0" encoding="UTF-8"?>
2.  
  1. 在 micro-service-cloud-provider-dept-8001 类路径(/resources 目录)下,添加配置文件 application.yml,配置内容如下。
1.  server:
2.  port: 8001 #服务端口号
3.  spring:
4.  application:
5.  name: microServiceCloudProviderDept  #微服务名称,对外暴漏的微服务名称,十分重要
6.  ################################################## JDBC 通用配置  ##########################################
7.  datasource:
8.  username: root        #数据库登陆用户名
9.  password: root        #数据库登陆密码
10.  url: jdbc:mysql://127.0.0.1:3306/bianchengbang_jdbc       #数据库url
11.  driver-class-name: com.mysql.jdbc.Driver                  #数据库驱动

13.  ############################### 不检查 spring.config.import=configserver:##################
14.  #  cloud:
15.  #    config:
16.  #      enabled: false
17.  ###################################### MyBatis 配置 ######################################
18.  mybatis:
19.  # 指定 mapper.xml 的位置
20.  mapper-locations: classpath:mybatis/mapper/*.xml
21.  #扫描实体类的位置,在此处指明扫描实体类的包,在 mapper.xml 中就可以不写实体类的全路径名
22.  type-aliases-package: net.biancheng.c.entity
23.  configuration:
24.  #默认开启驼峰命名法,可以不用设置该属性
25.  map-underscore-to-camel-case: true
26.  ########################################### Spring cloud 自定义服务名称和 ip 地址###############################################
27.  eureka:
28.  client: #将客户端注册到 eureka 服务列表内
29.  service-url:
30.  defaultZone: http://eureka7001.com:7001/eureka  #这个地址是 7001注册中心在 application.yml 中暴露出来额注册地址 (单机版)

32.  instance:
33.  instance-id: spring-cloud-provider-8001 #自定义服务名称信息
34.  prefer-ip-address: true  #显示访问路径的 ip 地址
35.  ########################################## spring cloud 使用 Spring Boot actuator 监控完善信息###################################
36.  # Spring Boot 2.50对 actuator 监控屏蔽了大多数的节点,只暴露了 heath 节点,本段配置(*)就是为了开启所有的节点
37.  management:
38.  endpoints:
39.  web:
40.  exposure:
41.  include: "*"   # * 在yaml 文件属于关键字,所以需要加引号
42.  info:
43.  app.name: micro-service-cloud-provider-dept
44.  company.name: c.biancheng.net
45.  build.aetifactId: @project.artifactId@
46.  build.version: @project.version@

  1. 在 net.biancheng.c.mapper 包下创建一个名为 DeptMapper 的接口,代码如下。

1.  package net.biancheng.c.mapper;

3.  import net.biancheng.c.entity.Dept;
4.  import org.apache.ibatis.annotations.Mapper;

6.  import java.util.List;

8.  @Mapper
9.  public interface  DeptMapper {
10.  //根据主键获取数据
11.  Dept  selectByPrimaryKey(Integer  deptNo);

13.  //获取表中的全部数据
14.  List  GetAll();
15.  }

  1. 在 resources/mybatis/mapper/ 目录下,创建一个名为 DeptMapper.xml 的 MyBatis 映射文件,配置内容如下。
1.  <?xml version="1.0" encoding="UTF-8"?>
2.  
3.  
4.  
5.  
6.  
7.  
8.  

10.  
11.  dept_no
12.  , dept_name, db_source
13.  

15.  
16.  select
17.  
18.  from dept
19.  where dept_no = #{deptNo,jdbcType=INTEGER}
20.  
21.  
22.  select *
23.  from dept;
24.  
25.  

  1. 在 net.biancheng.c.service 包下创建一个名为 DeptService 的接口,代码如下。

1.  package net.biancheng.c.service;

3.  import net.biancheng.c.entity.Dept;
4.  import java.util.List;

6.  public interface  DeptService {

8.  Dept  get(Integer  deptNo);

10.  List  selectAll();
11.  }

  1. 在 net.biancheng.c.service.impl 包下创建 DeptService 接口的实现类 DeptServiceImpl,代码如下。

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值