1.cloud-微服务架构编码构建

1.微服务cloud整体聚合父工程

  1.1 New Project

1.cloud-微服务架构编码构建_第1张图片

  1.2 Maven选版本

1.cloud-微服务架构编码构建_第2张图片

  1.3 字符编码

1.cloud-微服务架构编码构建_第3张图片

  1.4 注解生效激活

 主要为lombok中的@Data

1.cloud-微服务架构编码构建_第4张图片

  1.5 java编译版本选8

1.cloud-微服务架构编码构建_第5张图片

  1.6 File Type过滤

*.hprof;*.idea;*.iml;*.pyc;*.pyo;*.rbc;*.yarb;*~;.DS_Store;.git;.hg;.svn;CVS;__pycache__;_svn;vssver.scc;vssver2.scc;

1.cloud-微服务架构编码构建_第6张图片

  1.7 父工程POM



    4.0.0

    org.example.cloud
    cloud2020
    1.0
    pom

    
    
        UTF-8
        8
        8
        4.12
        1.2.17
        1.16.18
        5.1.49
        1.1.16
        1.3.0
    

    
    
        

            
            
                org.springframework.boot
                spring-boot-dependencies
                2.3.12.RELEASE
                pom
                import
            

            
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR1
                pom
                import
            

            
            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                2.1.0.RELEASE
                pom
                import
            

            
                mysql
                mysql-connector-java
                ${mysql.version}
            
            
                com.alibaba
                druid
                ${druid.version}
            
            
                org.mybatis.spring.boot
                mybatis-spring-boot-starter
                ${mybatis.spring.boot.version}
            
            
                junit
                junit
                ${junit.version}
            
            
                log4j
                log4j
                ${log4j.version}
            
            
                org.projectlombok
                lombok
                ${lombok.version}
                true
            
        
    

  

2.微服务订单

  2.1 建cloud-order80

1.cloud-微服务架构编码构建_第7张图片

  2.2 改POM文件



    
        cloud2020
        org.example.cloud
        1.0
    
    4.0.0

    cloud-order80

    
        8
        8
    

    
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
        
            org.projectlombok
            lombok
            true
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.3.12.RELEASE
                
                    
                        
                            repackage
                        
                    
                
            
        
    

  2.3 写YML

server:
  port: 80

  2.4 主启动

package org.example.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

  2.5 config配置类

package org.example.cloud.config;

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

@Configuration
public class ApplicationContextConfig {

    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

  2.6 创建controller


@RestController
@RequestMapping("order")
@Slf4j
public class OrderController {

    public static final String PAYMENT_URL = "http://localhost:8001";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("payment/create")
    public CommonResult   create(Payment payment){

        CommonResult commonResult = restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
        return  commonResult; //写操作
    }

    @GetMapping("get/{id}")
    public CommonResult getPayment(@PathVariable("id") Long id){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
    }

}

3.微服务支付

  3.1 建cloud-payment8001

1.cloud-微服务架构编码构建_第8张图片

  3.2 改POM文件



    
        cloud2020
        org.example.cloud
        1.0
    
    4.0.0

    cloud-payment8001

    
        8
        8
    

    

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
        
            org.projectlombok
            lombok
            true
        

        
            org.example.cloud
            cloud-api-common
            1.0
            compile
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.3.12.RELEASE
                
                    
                        
                            repackage
                        
                    
                
            
        
    

  3.3 写YML

server:
  port: 8001

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:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.example.cloud.entities

  3.4 主启动

@SpringBootApplication
public class PaymentMain {

    public static void main(String[] args) {
        SpringApplication.run(PaymentMain.class, args);
    }
}

4. 下一盘服务注册到Eureka

你可能感兴趣的:(SpringCloud,架构,微服务,java)