• Nacos注册中心之搭建工程


    创建maven项目,父工程

    File=>New => Project...

    pom配置

    使用SpringBoot,需要添加父级:SpringBoot:2.3.9.RELEASE

    Nacos依赖管理

                <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                    <version>2.2.6.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>

    主要配置

        <parent>
            <artifactId>spring-boot-starter-parent</artifactId>
            <groupId>org.springframework.boot</groupId>
            <version>2.3.9.RELEASE</version>
        </parent>
    
        <properties>
            <java.version>1.8</java.version>
            <mybatis.version>2.1.1</mybatis.version>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                    <version>2.2.6.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <dependency>
                    <groupId>org.mybatis.spring.boot</groupId>
                    <artifactId>mybatis-spring-boot-starter</artifactId>
                    <version>${mybatis.version}</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
        </dependencies>
    View Code

    创建子模块

    pom配置

    添加web、MySQL、mybatis和nacos依赖、maven插件(打包成可直接运行的 JAR 文件)

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <!--mybatis-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
        </dependencies>
        <build>
            <!--jar包名称-->
            <finalName>user-service</finalName>
            <plugins>
                <!--打包成可以直接运行的 JAR 文件(使用“java -jar”命令就可以直接运行)-->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    View Code

    编写user-service

    配置application.yaml(配置服务器端口、数据库连接、mybatis、logging、nacos服务端地址)

    server:
      port: 8081
    spring:
      datasource:
        url: jdbc:mysql://192.168.223.129:3306/cloud-user?useSSL=false
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver
      application:
        name: userservice
      cloud:
        nacos:
          server-addr: localhost:8848 # nacos 服务端地址
    mybatis:
      type-aliases-package: com.marw.pojo
      configuration:
        map-underscore-to-camel-case: true
    logging:
      level:
        com.marw: debug
      pattern:
        dateformat: MM-dd HH:mm:ss:SSS
    View Code

    启动类

    @SpringBootApplication
    @MapperScan("com.marw.mapper")
    public class UserServiceApplication {
        public static void main(String[] args) {
            SpringApplication.run(UserServiceApplication.class,args);
        }
    }
    View Code

    pojo(Java Bean)

    @Data
    public class User {
        private Long id;
        private String username;
        private String address;
    }
    View Code

    mapper(数据库操作)

    public interface UserMapper {
        @Select("select * from tb_user where id = #{id}")
        User findById(@Param("id") Long id);
    }
    View Code

    service(业务逻辑)

    @Service
    public class UserService {
        @Autowired
        private UserMapper userMapper;
    
        public User queryById(Long id){
            return userMapper.findById(id);
        }
    }
    View Code

    controller(暴露服务)

    @Slf4j
    @RestController
    @RequestMapping("/user")
    public class UserController {
        @Autowired
        private UserService userService;
    
        @GetMapping("/{id}")
        public User queryByid(@PathVariable("id") Long id) {
            return userService.queryById(id);
        }
    }
    View Code

    启动服务:服务注册成功

    编写order-service

    配置application.yaml(配置服务器端口、数据库连接、mybatis、logging、nacos服务端地址)

    server:
      port: 8082
    spring:
      datasource:
        url: jdbc:mysql://192.168.223.129:3306/cloud-order?useSSL=false
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver
      application:
        name: orderservice
      cloud:
        nacos:
          server-addr: localhost:8848 # nacos 服务端地址
    mybatis:
      type-aliases-package: com.marw.pojo
      configuration:
        map-underscore-to-camel-case: true
    View Code

    启动类

    @SpringBootApplication
    @MapperScan("com.marw.mapper")
    public class OrderServiceApplication {
        public static void main(String[] args) {
            SpringApplication.run(OrderServiceApplication.class, args);
        }
    
        @Bean
        @LoadBalanced
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }
    View Code

    pojo

    @Data
    public class Order {
        private Long id;
        private Long price;
        private String name;
        private Integer num;
        private Long userId;
        private User user;
    }
    
    @Data
    public class User {
        private Long id;
        private String username;
        private String address;
    }
    View Code

    mapper

    public interface OrderMapper {
    
        @Select("select * from tb_order where id = #{id}")
        Order findById(Long id);
    }
    View Code

    service

    @Service
    public class OrderService {
        @Autowired
        private OrderMapper orderMapper;
        @Autowired
        private RestTemplate restTemplate;
        public Order queryById(Long id){
            Order order = orderMapper.findById(id);
            String url = "http://userservice/user/"+order.getUserId();
            User user = restTemplate.getForObject(url, User.class);
            order.setUser(user);
            return order;
        }
    }
    View Code

    controller

    @RestController
    @RequestMapping("/order")
    public class OrderController {
    
        @Autowired
        private OrderService orderService;
    
        @GetMapping("/{id}")
        public Order queryById(@PathVariable("id") Long id){
           return orderService.queryById(id);
        }
    }
    View Code

    启动服务:服务注册成功

  • 相关阅读:
    WebBrowser
    hibernate关系映射(多对多)
    hibernate关系映射(多对一)
    使用maven构建hibernate
    使用maven构建web项目
    Chapter 6 ViewController
    Chapter 5 带颜色的同心圆
    Symbol(s) not found for architecture arm64
    反射实现NSCodingProtocol 帮助类
    UITableView默认选中第一行
  • 原文地址:https://www.cnblogs.com/WarBlog/p/15400851.html
Copyright © 2020-2023  润新知