淘先锋技术网

首页 1 2 3 4 5 6 7

springcloud微服务学习笔记目录:
springcloud微服务学习笔记

1.参考cloud-provider-payment8001构建cloud-provider-payment8002
2.pom文件

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <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>
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

3.yml文件

server:
  port: 8002

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: root

mybatis:
  type-aliases-package: com.atguigu.springcloud.entities
  mapper-locations: classpath:mapper/*.xml

eureka:
  client:
    #表示是否将自己注册进eurekaServer 默认为true
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

  instance:
    instance-id: payment8002
    prefer-ip-address: true


4.主启动类

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

5.业务类(参考8001)
6.修改8001,8002controller
8001

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Resource
    private DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/payment/creat")
    public CommonResult<Payment> create(@RequestBody Payment payment){

        int result=paymentService.create(payment);
        log.info("******插入结果"+result);
        if (result>0){
            return new CommonResult(200,"success",result);
        }else {
            return new CommonResult(400,"fail",null);
        }
    }

    @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        Payment payment=paymentService.getPaymentById(id);
        log.info("******查询结果"+payment);
        if(payment!=null) {
            return new CommonResult(200, "success,serverPort:"+serverPort, payment);
        }else {
            return new CommonResult(400,"fail",null);
        }

    }

    @GetMapping("/payment/discovery")
    public Object discovery(){
       List<String> services=discoveryClient.getServices();
       for (String service:services){
           log.info("***********service:"+service);
       }
       List<ServiceInstance> serviceInstances=discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
       for (ServiceInstance serviceInstance:serviceInstances){
           log.info(serviceInstance.getHost()+"\t"+serviceInstance.getInstanceId()+"\t"+serviceInstance.getPort()+"\t"+serviceInstance.getUri());
       }
       return this.discoveryClient;
    }
}

8002

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/payment/creat")
    public CommonResult<Payment> create(@RequestBody Payment payment){

        int result=paymentService.create(payment);
        log.info("******插入结果"+result);
        if (result>0){
            return new CommonResult(200,"success",result);
        }else {
            return new CommonResult(400,"fail",null);
        }
    }

    @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        Payment payment=paymentService.getPaymentById(id);
        log.info("******查询结果"+payment);
        if(payment!=null) {
            return new CommonResult(200, "success,serverPort:"+serverPort, payment);
        }else {
            return new CommonResult(400,"fail",null);
        }

    }
}

两台机器做负载均衡
orderController修改
在这里插入图片描述
ApplicationContextConfig修改
在这里插入图片描述

7.测试
http://localhost/consumer/payment/get/1
负载轮询,8001和8002端口轮询出现
在这里插入图片描述
在这里插入图片描述