• ss-4.2 多个eureka集群案例


    cloud-eureka-server7001

    2个服务 2个提供器 1个消费者 http://127.0.0.1:8001/actuator/health //查看当前服务状态 http://127.0.0.1:8001/payment/discovery 发现服务

    │  pom.xml
    │
    ├─src
    │  ├─main
    │  │  ├─java
    │  │  │  └─com
    │  │  │      └─cmk
    │  │  │          └─springCloud
    │  │  │                  EurekaMain7001.java  //jetbrains://idea/navigate/reference?project=zy2020&path=com/cmk/springCloud/EurekaMain7001.java:12:1
    │  │  │
    │  │  └─resources
    │  │          application.yml
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    pom.xml

    <dependencies>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
            dependency>
            <dependency>
                <groupId>org.cmk.springcoloudgroupId>
                <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.springframework.bootgroupId>
                <artifactId>spring-boot-devtoolsartifactId>
                <scope>runtimescope>
                <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    EurekaMain7001.java

    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaMain7001 {
        public static void main(String[] args) {
            SpringApplication.run(EurekaMain7001.class,args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    application.yml

    server:
      port: 7001
    
    eureka:
      instance:
        hostname: eureka7001.com  #eureka服务端的实例名称
      client:
        register-with-eureka: false  #false表示不向注册中心注册自己
        fetch-registry: false  #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
        service-url:
          #设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
          defaultZone: http://eureka7002.com:7002/eureka/
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    cloud-eureka-server7002

    │  pom.xml
    │
    ├─src
    │  ├─main
    │  │  ├─java
    │  │  │  └─com
    │  │  │      └─cmk
    │  │  │          └─springCloud
    │  │  │                  EurekaMain7002.java //jetbrains://idea/navigate/reference?project=zy2020&path=com/cmk/springCloud/EurekaMain7002.java:14:1
    │  │  │
    │  │  └─resources
    │  │          application.yml
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    pom.xml

        <dependencies>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
            dependency>
            <dependency>
                <groupId>org.cmk.springcoloudgroupId>
                <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.springframework.bootgroupId>
                <artifactId>spring-boot-devtoolsartifactId>
                <scope>runtimescope>
                <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    EurekaMain7002.java

    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaMain7002 {
        public static void main(String[] args) {
           SpringApplication.run(EurekaMain7002.class,args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    application.yml

    server:
      port: 7002
    
    eureka:
      instance:
        hostname: eureka7002.com  #eureka服务端的实例名称
      client:
        register-with-eureka: false  #false表示不向注册中心注册自己
        fetch-registry: false  #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
        service-url:
          #设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
          defaultZone: http://eureka7001.com:7001/eureka/
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    cloud-provider-payment8001

    │  pom.xml
    │
    ├─src
    │  ├─main
    │  │  ├─java
    │  │  │  └─com
    │  │  │      └─cmk
    │  │  │          └─springCloud
    │  │  │              │  PaymentMain8001.java  //jetbrains://idea/navigate/reference?project=zy2020&path=com/cmk/springCloud/PaymentMain8001.java:14:1
    │  │  │              │
    │  │  │              ├─controller
    │  │  │              │      PaymentController.java
    │  │  │              │
    │  │  │              ├─dao
    │  │  │              │      PaymentDao.java
    │  │  │              │
    │  │  │              └─service
    │  │  │                  │  PaymentService.java
    │  │  │                  │
    │  │  │                  └─impl
    │  │  │                          PaymentServiceImpl.java
    │  │  │
    │  │  └─resources
    │  │      │  application.yml
    │  │      │
    │  │      └─mapper
    │  │              PaymentMapper.xml
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    pom.xml

       <dependencies>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
            dependency>
            <dependency>
                <groupId>org.cmk.springcoloudgroupId>
                <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>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>
                <scope>runtimescope>
                <optional>trueoptional>
            dependency>
    
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
            dependency>
    
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    PaymentMain8001.java

    @SpringBootApplication
    @EnableEurekaClient
    @EnableDiscoveryClient
    public class PaymentMain8001 {
        public static void main(String[] args) {
            SpringApplication.run(PaymentMain8001.class,args);
    
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    PaymentController.java

    @RestController
    @Slf4j
    public class PaymentController {
    
    
        @Value("${server.port}")
        private String serverPort;
    
        @Resource
        private DiscoveryClient discoveryClient;
    
        @Resource
        private PaymentService paymentService;
    
        @PostMapping("/payment/create")
        public CommonResult create(@RequestBody Payment payment){
            int result = paymentService.create(payment);
            log.info("****插入结果"+result);
    
            if (result>0){
                return  new CommonResult(200,"插入数据库成功,服务端口:"+serverPort,result);
            }else{
               return  new CommonResult(400,"插入数据库失败",null);
            }
        }
    
        @GetMapping("/payment/get/{id}")
        public CommonResult getPaymentById(@PathVariable("id") long id){
            Payment result = paymentService.getPaymentById(id);
            log.info("****查询结果"+result+"456");
            if (result !=null){
                return  new CommonResult(200,"查询数据库成功,服务端口:"+serverPort,result);
            }else{
                return  new CommonResult(400,"查询数据库失败",null);
            }
        }
    
        @GetMapping("/payment/discovery")
        public Object discovery(){
            List<String> services = discoveryClient.getServices();
            for (String element : services) {
                log.info("*****:"+element);
            }
            List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
            for (ServiceInstance instance : instances) {
                log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
            }
            return this.discoveryClient;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    PaymentDao.java

    
    @Mapper
    public interface PaymentDao {
         int create(Payment payment);
    
         Payment getPaymentById(@Param("id") long id);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    service

    PaymentService.java

    
    @Mapper
    public interface PaymentService {
         int create(Payment payment);
    
         Payment getPaymentById(@Param("id") long id);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    impl > PaymentServiceImpl.java

    @Service
    public class PaymentServiceImpl implements PaymentService {
    
        @Resource
        private PaymentDao paymentDao;
    
        public int create(Payment payment){
            return  paymentDao.create(payment);
        }
    
        public Payment getPaymentById(@Param("id") long id){
            return paymentDao.getPaymentById(id);
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    resources

    application.yml

    server:
      port: 8001
    
    spring:
      application:
        name: cloud-payment-service
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
        username: root
        password: 123456
    
    
    mybatis:
      mapper-locations: classpath:mapper/*.xml
      type-aliases-package: com.cmk.springCloud.entities
    
    
    eureka:
      client:
        #表示是否将自己注册进 EurekaServer默认为true
        register-with-eureka: true
        #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
        fetch-registry: 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地址
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    mapper > PaymentMapper.xml

    
    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    
    <mapper namespace="com.cmk.springCloud.service.PaymentService">
        <insert id="create" parameterType="com.cmk.springCloud.entities.Payment">
            insert into payment(serial) values (#{serial});
        insert>
    
        <select id="getPaymentById" parameterType="long" resultMap="BaseResultMap">
            select * from payment where id=#{id}
        select>
    
        <resultMap id="BaseResultMap" type="com.cmk.springCloud.entities.Payment">
            <id column="id" property="id" jdbcType="BIGINT"/>
            <id column="serial" property="serial" jdbcType="VARCHAR"/>
        resultMap>
    
    
    mapper>
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    cloud-provider-payment8002

    │  pom.xml
    │
    ├─src
    │  ├─main
    │  │  ├─java
    │  │  │  └─com
    │  │  │      └─cmk
    │  │  │          └─springCloud
    │  │  │              │  PaymentMain8002.java  //jetbrains://idea/navigate/reference?project=zy2020&path=com/cmk/springCloud/PaymentMain8002.java:13:1
    │  │  │              │
    │  │  │              ├─controller
    │  │  │              │      PaymentController.java
    │  │  │              │
    │  │  │              ├─dao
    │  │  │              │      PaymentDao.java
    │  │  │              │
    │  │  │              └─service
    │  │  │                  │  PaymentService.java
    │  │  │                  │
    │  │  │                  └─impl
    │  │  │                          PaymentServiceImpl.java
    │  │  │
    │  │  └─resources
    │  │      │  application.yml
    │  │      │
    │  │      └─mapper
    │  │              PaymentMapper.xml
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    pom.xml

       <dependencies>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
            dependency>
            <dependency>
                <groupId>org.cmk.springcoloudgroupId>
                <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>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>
                <scope>runtimescope>
                <optional>trueoptional>
            dependency>
    
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
            dependency>
    
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    PaymentMain8002.java

    
    @SpringBootApplication
    @EnableEurekaClient
    public class PaymentMain8002 {
        public static void main(String[] args) {
            SpringApplication.run(PaymentMain8002.class,args);
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    PaymentController.java

    @RestController
    @Slf4j
    public class PaymentController {
    
        @Value("${server.port}")
        private String serverPort;
    
        @Resource
        private PaymentService paymentService;
    
        @PostMapping("/payment/create")
        public CommonResult create(@RequestBody Payment payment){
            int result = paymentService.create(payment);
            log.info("****插入结果"+result);
    
            if (result>0){
                return  new CommonResult(200,"插入数据库成功,服务端口:"+serverPort,result);
            }else{
               return  new CommonResult(400,"插入数据库失败",null);
            }
        }
    
        @GetMapping("/payment/get/{id}")
        public CommonResult getPaymentById(@PathVariable("id") long id){
            Payment result = paymentService.getPaymentById(id);
            log.info("****查询结果"+result+"456");
            if (result !=null){
                return  new CommonResult(200,"查询数据库成功,服务端口:"+serverPort,result);
            }else{
                return  new CommonResult(400,"查询数据库失败",null);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    PaymentDao.java

    @Mapper
    public interface PaymentDao {
         int create(Payment payment);
    
         Payment getPaymentById(@Param("id") long id);
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    service

    PaymentService.java

    @Mapper
    public interface PaymentService {
         int create(Payment payment);
    
         Payment getPaymentById(@Param("id") long id);
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    impl > PaymentServiceImpl.java

    @Service
    public class PaymentServiceImpl implements PaymentService {
    
        @Resource
        private PaymentDao paymentDao;
    
        public int create(Payment payment){
            return  paymentDao.create(payment);
        }
    
        public Payment getPaymentById(@Param("id") long id){
            return paymentDao.getPaymentById(id);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    resources

    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
        url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
        username: root
        password: 123456
    
    
    mybatis:
      mapper-locations: classpath:mapper/*.xml
      type-aliases-package: com.cmk.springCloud.entities
    
    
    eureka:
      client:
        #表示是否将自己注册进 EurekaServer默认为true
        register-with-eureka: true
        #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
        fetch-registry: 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地址
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    mapper > PaymentMapper.xml

    
    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    
    <mapper namespace="com.cmk.springCloud.service.PaymentService">
        <insert id="create" parameterType="com.cmk.springCloud.entities.Payment">
            insert into payment(serial) values (#{serial});
        insert>
    
        <select id="getPaymentById" parameterType="long" resultMap="BaseResultMap">
            select * from payment where id=#{id}
        select>
    
        <resultMap id="BaseResultMap" type="com.cmk.springCloud.entities.Payment">
            <id column="id" property="id" jdbcType="BIGINT"/>
            <id column="serial" property="serial" jdbcType="VARCHAR"/>
        resultMap>
    
    
    mapper>
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    com-consumer-order80

    
    │  pom.xml
    │
    ├─src
    │  ├─main
    │  │  ├─java
    │  │  │  └─com
    │  │  │      └─cmk
    │  │  │          └─consumerOrder80
    │  │  │              │  OrderMain80.java   //jetbrains://idea/navigate/reference?project=zy2020&path=com/cmk/consumerOrder80/OrderMain80.java:13:1
    │  │  │              │
    │  │  │              ├─config
    │  │  │              │      ApplicationContextConfig.java
    │  │  │              │
    │  │  │              └─controller
    │  │  │                      OrderController.java
    │  │  │
    │  │  └─resources
    │  │          application.yml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    pom.xml

        <dependencies>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
            dependency>
            <dependency>
                <groupId>org.cmk.springcoloudgroupId>
                <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.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-devtoolsartifactId>
                <scope>runtimescope>
                <optional>trueoptional>
            dependency>
    
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    OrderMain80.java

    @SpringBootApplication
    @EnableEurekaClient
    public class OrderMain80 {
        public static void main(String[] args) {
            SpringApplication.run(OrderMain80.class,args);
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    ApplicationContextConfig.java

    
    @Configuration
    public class ApplicationContextConfig {
    
        @Bean
        @LoadBalanced //使用@Loaibalanced注解赋予RestTemplate负载均衡的能力
        public RestTemplate getRestTemplate(){
            return  new RestTemplate();
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    OrderController.java

    @RestController
    @Slf4j
    public class OrderController {
    
    //    public static final  String PAYMENT_URL = "http://localhost:8001";
        public static final  String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
        @Resource
        private RestTemplate restTemplate;
    
        @GetMapping("/consumer/payment/create")
        public CommonResult<Payment> create(Payment payment){
            System.out.println(payment);
            return  restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
        }
    
        @GetMapping("consumer/payment/get/{id}")
        public CommonResult getPayment(@PathVariable("id") long id){
            return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    application.yml

    
    server:
      port: 80
    
    spring:
      application:
        name: cloud-order-service
    
    eureka:
      client:
        register-with-eureka: true
        fetch-registry: true
        service-url:
    #      defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
  • 相关阅读:
    MHDNet
    Redis夺命十二问,差点没抗住
    【DS】Java实现顺序表
    牛客网verilog刷题知识点盘点(75道题的版本)
    java源码系列:HashMap源码验证,自己手写一个HashMap之01-基本框架
    [职场] Android是什么?Android行业有哪些- #媒体#经验分享
    《Design by Contract for Embedded Software》 翻译
    漫画 | 永不过时的编程语言终于出现了!
    Humanoid-Gym 开源人形机器人端到端强化学习训练框架!星动纪元联合清华大学、上海期智研究院发布!
    AcWing 4505. 最大子集
  • 原文地址:https://blog.csdn.net/lhorse003/article/details/126034560