SpringCloud无介绍快使用,集群cloud-provider-payment8002搭建(十二)

SpringCloud无介绍快使用,集群cloud-provider-payment8002搭建(十二)

  • 问题背景
  • 服务集群搭建
  • Lyric: 接下来为您播放的是

问题背景

从零开始学springcloud微服务项目
注意事项:

  • 约定 > 配置 > 编码
  • IDEA版本2021.1
  • 这个项目,我分了很多篇章,每篇文章一个操作步骤,目的是显得更简单明了
  • controller调service,service调dao

服务集群搭建

1 cloud-provider-payment8002子模块module创建
SpringCloud无介绍快使用,集群cloud-provider-payment8002搭建(十二)_第1张图片
2 选择jdk1.8
SpringCloud无介绍快使用,集群cloud-provider-payment8002搭建(十二)_第2张图片
3 输入服务名:cloud-provider-payment8002
SpringCloud无介绍快使用,集群cloud-provider-payment8002搭建(十二)_第3张图片
4 导入依赖


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud2022artifactId>
        <groupId>com.yggroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>cloud-provider-payment8002artifactId>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        <dependency>
            <groupId>com.yggroupId>
            <artifactId>cloud-api-commonsartifactId>
            <version>${project.version}version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.10version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

project>

5 添加application.yml文件

server:
  port: 8002

spring:
  application:
    name: cloud-payment-service
  datasource:
    #type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: com.mysql.cj.jdbc.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/mysqltest?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123456


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

mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.yg.springcloud.entities    # 所有Entity别名类所在包

6 复制服务cloud-provider-payment8001的业务代码到cloud-provider-payment8002,更改启动类的类名
SpringCloud无介绍快使用,集群cloud-provider-payment8002搭建(十二)_第4张图片

7 修改cloud-provider-payment8001和cloud-provider-payment8002的PaymentController,更改从yml中获取服务端口

package com.yg.springcloud.controller;

import com.yg.springcloud.entities.CommonResult;
import com.yg.springcloud.entities.Payment;
import com.yg.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author suolong
 * @Date 2022/6/14 21:36
 * @Version 2.0
 */
@RestController
@Slf4j
public class PaymentController {

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

    @Resource
    private PaymentService paymentService;

    @PostMapping(value = "/payment/create")
    public CommonResult create(@RequestBody Payment payment) {
        int result = paymentService.create(payment);
        log.info("result哈哈哈: {}", result);
        if (result > 0) {
            return new CommonResult(200, "插入成功", result);
        }
        return new CommonResult(500, "插入失败", null);
    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById(id);
        log.info("查询结果哈哈哈hahah:{}", payment);
        if (payment != null) {
            return new CommonResult(200, "查询成功哈: " + serverPort, payment);
        } else {
            return new CommonResult(500, "没有对应记录,查询ID: " + id, null);
        }
    }

}

8 修改cloud-consumer-order80中的OrderController的PaymentSrv_URL的地址

public static final String PaymentSrv_URL = "http://CLOUD-PAYMENT-SERVICE";

在ApplicationContextConfig中添加注解@LoadBalanced,进行负载均衡Ribbon

package com.yg.springcloud.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @Author suolong
 * @Date 2022/6/15 20:53
 * @Version 2.0
 */
@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced // 使用@LoadBalanced注解赋予RestTemplate负载均衡的能力
    public RestTemplate restTemplate()
    {
        return new RestTemplate();
    }

}

9 先启动EurekaService,在启动其他所有服务:http://eureka7001.com:7001/
SpringCloud无介绍快使用,集群cloud-provider-payment8002搭建(十二)_第5张图片

10 查询两次,实现负载均衡:http://localhost/consumer/payment/get/1
第一次使用的8001端口
SpringCloud无介绍快使用,集群cloud-provider-payment8002搭建(十二)_第6张图片
第二次使用的8002端口
SpringCloud无介绍快使用,集群cloud-provider-payment8002搭建(十二)_第7张图片

11 由此可见restTemplate可以直接使用服务名进行调用

public String PaymentSrv_URL = "http://CLOUD-PAYMENT-SERVICE";
restTemplate.postForObject(PaymentSrv_URL + "/payment/create", payment, CommonResult.class);

12 actuator更改主机名称,显示ip,更改yml
cloud-consumer-order80

server:
  port: 80


spring:
  application:
    name: cloud-consumer-order80


eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      #defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  instance:
    instance-id: order80
    prefer-ip-address: true     #访问路径可以显示IP地址

cloud-payment-service8002

server:
  port: 8002

spring:
  application:
    name: cloud-payment-service
  datasource:
    #type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: com.mysql.cj.jdbc.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/mysqltest?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123456


eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      #defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  instance:
    instance-id: payment8002
    prefer-ip-address: true     #访问路径可以显示IP地址

mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.yg.springcloud.entities    # 所有Entity别名类所在包

cloud-payment-service8001

server:
  port: 8001

spring:
  application:
    name: cloud-payment-service
  datasource:
    #type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: com.mysql.cj.jdbc.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/mysqltest?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123456


eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      #defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  instance:
    instance-id: payment8001
    prefer-ip-address: true     #访问路径可以显示IP地址

mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.yg.springcloud.entities    # 所有Entity别名类所在包

12 更改之后,查看服务名变化:http://eureka7001.com:7001/




作为程序员第 178 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …

Lyric: 接下来为您播放的是

你可能感兴趣的:(spring,cloud,spring,boot,微服务)