五,Eureka 第五章

5.3.2 修改pom添加依赖

复制代码
<dependencies>
        <!--公共部门-->
        <dependency>
            <groupId>cn.bdqn</groupId>
            <artifactId>springcloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!--eureka client-->    
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        
        <!--actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

        <!--连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>

        <!--mysql连接-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--jdbc-->
        <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>

5.3.3编写yml

复制代码
server:
  port: 8003

spring:
  application:
    name: springcloud-payment-provider-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springcloud_db?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
    username: root
    password: xiaoduo456new


mybatis:
  type-aliases-package: cn.bdqn.domain
  mapper-locations: classpath:mapper/*.xml

eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-width-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true 单节点无所谓,集群必须设置true 才能配合ribbon  使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,
                   http://eureka7002:com:7002/eureka,
                   http://eureka7003:com:7003/eureka,

    instances:
      prefer-ip-address: true  #使用ip地址注册

5.3.4编写启动类

复制代码
@SpringBootApplication
@EnableEurekaClient
public class Payment8003Application {
    public static void main(String[] args) {
        SpringApplication.run(Payment8003Application.class, args);
    }
}

5.3.5编写paymentMapper接口

复制代码
public interface PaymentServer {

    //保存一个支付流水
    public void save(Payment payment);
    //根据id获取具体的支付信息
    public Payment selectById(Integer id);
}

5.3.6编写映射文件

复制代码
<?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="cn.bdqn.Mapper.PaymentMapper">
    <resultMap id="PaymentResultMap" type="cn.bdqn.domain.Payment">
        <id column="id" property="id"></id>
        <id column="flow_number" property="flowNumber"></id>
    </resultMap>
    <insert id="insert" parameterType="cn.bdqn.Mapper.PaymentMapper">
        insert into t_payment(flow_number)values(#{flowNumber})
    </insert>
    <select id="selectById" resultMap="PaymentResultMap">
        SELECT id,flow_number from t_payment where  id="#{id}"
    </select>
</mapper>

5.3.6编写Payment业务接口以及实现类型

复制代码
public interface PaymentServer {

    //保存一个支付流水
    public void save(Payment payment);
    //根据id获取具体的支付信息
    public Payment selectById(Integer id);
}


@Service
public class PaymentServerImpl implements PaymentServer {

    @Autowired
    private PaymentMapper paymentMapper;

    @Override
    public void save(Payment payment) {
        paymentMapper.insert(payment);
    }

    @Override
    public Payment selectById(Integer id) {
        return paymentMapper.QueryById(id);
    }
}

5.3.8编写paymentController控制器

复制代码
@RestController
@EnableEurekaClient
public class PaymentController {

    @Autowired
    private PaymentServer paymentServer;

    @GetMapping("/payment/save")
    public ResponseResult save(Payment payment){
        try {
            paymentServer.save(payment);
            return  new ResponseResult(200,"插入成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return  new ResponseResult(404,"插入失败",null);
        }
    }

    @PostMapping("/payment/id/{id}")
    public ResponseResult selectById(@RequestBody Integer id) {
        Payment payment = paymentServer.selectById(id);
        if (payment != null) {
            return new ResponseResult(200, "成功", payment);
        } else {
            return new ResponseResult(200, "失败", null);
        }
    }
}
相关推荐
哲Zheᗜe༘5 分钟前
K8S-Service资源对象
云原生·容器·kubernetes
阿里云云原生26 分钟前
Entity Explorer 在云原生监控中的落地:USearch/SPL 查询应用
阿里云·云原生·可观测·umodel
java_logo30 分钟前
GITLAB Docker 容器化部署指南
linux·运维·数据库·docker·容器·eureka·gitlab
小二·31 分钟前
从割裂到融合:基于 DevUI 与 MateChat 构建新一代云原生智能控制台
云原生
sf_jiang34 分钟前
K8s HPA的原理
云原生·容器·kubernetes
会飞的小蛮猪37 分钟前
RKE2 部署K8S v1.34.2+rke2r1(Ubuntu2204)离线安装
云原生·容器·kubernetes
小坏讲微服务40 分钟前
K8S 部署 Spring Cloud Alibaba 微服务企业实战完整使用
spring cloud·docker·微服务·云原生·容器·kubernetes·k8s
阿里云云原生1 小时前
Nginx Ingress 退役:阿里云云原生 API 网关的迁移方案与实操详解
nginx·阿里云·云原生
阿里云云原生1 小时前
阿里云 ARMS 自定义指标采集:打破传统 APM 局限,实现业务可视化监控
数据库·阿里云·云原生·oracle·arms
biubiubiu07061 小时前
常用Docker命令
docker·容器·eureka