五,Eureka 第五章

五,Eureka 第五章_第1张图片

五,Eureka 第五章_第2张图片

五,Eureka 第五章_第3张图片

五,Eureka 第五章_第4张图片

 5.3.2 修改pom添加依赖


        
        
            cn.bdqn
            springcloud-api-commons
            ${project.version}
        

            
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.0
        

        
        
            com.alibaba
            druid-spring-boot-starter
        

        
        
            mysql
            mysql-connector-java
        

        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        

        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        


        
            org.projectlombok
            lombok
            true
        

        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

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地址注册

 五,Eureka 第五章_第5张图片

五,Eureka 第五章_第6张图片

5.3.4编写启动类 


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

 五,Eureka 第五章_第7张图片

5.3.5编写paymentMapper接口


public interface PaymentServer {

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

5.3.6编写映射文件




    
        
        
    
    
        insert into t_payment(flow_number)values(#{flowNumber})
    
    

五,Eureka 第五章_第8张图片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);
    }
}

五,Eureka 第五章_第9张图片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);
        }
    }
}

五,Eureka 第五章_第10张图片

五,Eureka 第五章_第11张图片

五,Eureka 第五章_第12张图片

你可能感兴趣的:(eureka,云原生)