j9九游会登录/ 应用平台 appstage/ / / / 构建spring cloud工程
更新时间:2024-03-07 gmt 08:00

构建spring cloud工程-j9九游会登录

创建父工程

  1. 创建maven工程。
    图1 创建maven工程
  2. 父pom添加依赖。
    
        
            
                org.springframework.boot
                spring-boot-dependencies
                2.2.8.release
                pom
                import
            
        
    

创建子工程servicea

  1. 创建maven工程。
    图2 创建maven工程
  2. 新建src目录。
    图3 新建src目录
  3. 编写业务代码。
    图4 业务代码文件
    1. 编写启动类
      package com.huawei.demo.servicea;
      import org.springframework.boot.springapplication;
      import org.springframework.boot.autoconfigure.springbootapplication;
      import org.springframework.cloud.netflix.eureka.enableeurekaclient;
      /**
       * 启动类
       *
       * @author xxx
       * @since 2023-12-05
       */
      @springbootapplication
      @enableeurekaclient
      public class serviceaspringbootapplication {
          public static void main(string[] args) {
              springapplication.run(serviceaspringbootapplication.class, args);
          }
      }
    2. 编写controller类
      package com.huawei.demo.servicea.controller;
      import org.springframework.beans.factory.annotation.autowired;
      import org.springframework.web.bind.annotation.getmapping;
      import org.springframework.web.bind.annotation.pathvariable;
      import org.springframework.web.bind.annotation.requestmapping;
      import org.springframework.web.bind.annotation.restcontroller;
      import com.huawei.demo.servicea.pojo.userinfo;
      import com.huawei.demo.servicea.service.userservice;
      /**
       * 用户对外接口
       *
       * @author xxx
       * @since 2023-12-06
       */
      @restcontroller
      @requestmapping("/user")
      public class usercontroller {
          @autowired
          private userservice userservice;
          @getmapping("/{userid}")
          public userinfo getuserbyname(@pathvariable string userid) {
              return userservice.getuserbyid(userid);
          }
      }
    3. 编写mapper类
      package com.huawei.demo.servicea.mapper;
      import org.apache.ibatis.annotations.mapper;
      import org.apache.ibatis.annotations.select;
      import com.huawei.demo.servicea.pojo.userinfo;
      /**
       * 用户查询
       *
       * @author xxx
       * @since 2023-12-06
       */
      @mapper
      public interface usermapper {
          @select("select * from demo_user_info where user_id = #{userid}")
          userinfo getuserbyid(string userid);
      }
    4. 编写pojo类
      package com.huawei.demo.servicea.pojo;
      import lombok.data;
      /**
       * user信息
       *
       * @author xxx
       * @since 2023-12-06
       */
      @data
      public class userinfo {
          private string userid;
          private string username;
          private string phone;
          private string address;
      }
    5. 编写service类
      package com.huawei.demo.servicea.service;
      import org.springframework.beans.factory.annotation.autowired;
      import org.springframework.stereotype.service;
      import com.huawei.demo.servicea.mapper.usermapper;
      import com.huawei.demo.servicea.pojo.userinfo;
      /**
       * userservice
       *
       * @author xxx
       * @since 2023-12-06
       */
      @service
      public class userservice {
          @autowired
          private usermapper usermapper;
          public userinfo getuserbyid(string userid) {
              return usermapper.getuserbyid(userid);
          }
      }
    6. 配置微服务
      server:
        port: 8081
      spring:
        application:
          name: demoservicea
        datasource:
          url: jdbc:mysql://127.0.0.1:3306/spring_cloud_demo?useunicode=true&characterencoding=utf-8&servertimezone=asia/shanghai
          username: root
          password: ***
          driver-class-name: com.mysql.jdbc.driver
      mybatis:
        configuration:
          map-underscore-to-camel-case: true
        type-aliases-package: com.huawei.dmo.servicea

创建子工程serviceb

  1. 创建maven工程。
    图5 创建maven工程
  2. 新建src目录。
    图6 新建src目录
  3. 编写业务代码。
    图7 业务代码文件
    1. 编写启动类
      package com.huawei.demo.serviceb;
      import org.springframework.boot.springapplication;
      import org.springframework.boot.autoconfigure.springbootapplication;
      import org.springframework.cloud.client.loadbalancer.loadbalanced;
      import org.springframework.cloud.netflix.eureka.enableeurekaclient;
      import org.springframework.context.annotation.bean;
      import org.springframework.web.client.resttemplate;
      /**
       * 功能描述
       *
       * @author xxx
       * @since 2023-12-05
       */
      @springbootapplication
      @enableeurekaclient
      public class servicebspringbootapplication {
          public static void main(string[] args) {
              springapplication.run(servicebspringbootapplication.class, args);
          }
          @bean
          @loadbalanced
          public resttemplate resttemplate() {
              return new resttemplate();
          }
      }
    2. 编写controller类
      package com.huawei.demo.serviceb.controller;
      import org.springframework.beans.factory.annotation.autowired;
      import org.springframework.web.bind.annotation.getmapping;
      import org.springframework.web.bind.annotation.pathvariable;
      import org.springframework.web.bind.annotation.requestmapping;
      import org.springframework.web.bind.annotation.restcontroller;
      import com.huawei.demo.serviceb.pojo.orderinfo;
      import com.huawei.demo.serviceb.service.orderservice;
      /**
       * 用户对外接口
       *
       * @author xxx
       * @since 2023-12-06
       */
      @restcontroller
      @requestmapping("/order")
      public class ordercontroller {
          @autowired
          private orderservice orderservice;
          @getmapping("/{orderid}")
          public orderinfo findorder(@pathvariable string orderid) {
              return orderservice.findorder(orderid);
          }
      }
    3. 编写domain类
      package com.huawei.demo.serviceb.domain;
      import lombok.data;
      /**
       * user信息
       *
       * @author xxx
       * @since 2023-12-06
       */
      @data
      public class userinfo {
          private string userid;
          private string username;
          private string phone;
          private string address;
      }
    4. 编写mapper类
      package com.huawei.demo.serviceb.mapper;
      import org.apache.ibatis.annotations.mapper;
      import org.apache.ibatis.annotations.select;
      import com.huawei.demo.serviceb.pojo.orderinfo;
      /**
       * 用户查询
       *
       * @author xxx
       * @since 2023-12-06
       */
      @mapper
      public interface ordermapper {
          @select("select * from demo_order_info where order_id = #{orderid}")
          orderinfo getorderbyid(string orderid);
      }
    5. 编写pojo类
      package com.huawei.demo.serviceb.pojo;
      import com.huawei.demo.serviceb.domain.userinfo;
      import lombok.data;
      /**
       * order信息
       *
       * @author xxx
       * @since 2023-12-06
       */
      @data
      public class orderinfo {
          private string orderid;
          private string ordername;
          private string price;
          private string userid;
          private userinfo userinfo;
      }
    6. 编写service类
      package com.huawei.demo.serviceb.service;
      import org.springframework.beans.factory.annotation.autowired;
      import org.springframework.stereotype.service;
      import org.springframework.web.client.resttemplate;
      import com.huawei.demo.serviceb.domain.userinfo;
      import com.huawei.demo.serviceb.mapper.ordermapper;
      import com.huawei.demo.serviceb.pojo.orderinfo;
      /**
       * 功能描述
       *
       * @author xxx
       * @since 2023-12-06
       */
      @service
      public class orderservice {
          @autowired
          private resttemplate resttemplate;
          @autowired
          private ordermapper ordermapper;
          public orderinfo findorder(string orderid) {
              orderinfo orderinfo=ordermapper.getorderbyid(orderid);
              string url = "http://demoservicea/user/"   orderinfo.getuserid();
              userinfo userinfo = resttemplate.getforobject(url, userinfo.class);
              orderinfo.setuserinfo(userinfo);
              return orderinfo;
          }
      }
    7. 配置微服务
      server:
        port: 8082
      spring:
        application:
          name: demoserviceb
        datasource:
          url: jdbc:mysql://127.0.0.1:3306/spring_cloud_demo?useunicode=true&characterencoding=utf-8&servertimezone=asia/shanghai
          username: root
          password: ***
          driver-class-name: com.mysql.jdbc.driver
      eureka:
        client:
          service-url:
            defaultzone: http://localhost:11011/eureka/
      mybatis:
        configuration:
          map-underscore-to-camel-case: true
        type-aliases-package: com.huawei.dmo.servicea

创建eureka注册中心

  1. 创建eureka微服务。
  2. 添加依赖。
    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-server
    
    
        org.springframework.cloud
        spring-cloud-starter-eureka-server
    
    
        org.springframework.boot
        spring-boot-starter-web
    
  3. 新建启动类。
    图8 新建启动类
  4. 启动类中增加注解@enableeurekaserver,标明注册中心微服务。
    @springbootapplication
    @enableeurekaserver
    public class eurekaspringbootapplication {
        public static void main(string[] args) {
            springapplication.run(eurekaspringbootapplication.class, args);
        }
    }
  5. 在eureka微服务中增加配置文件。
    图9 增加配置文件
  6. 配置注册中心微服务端口等。
    server:
      port: 11011
    spring:
      application:
        name: eureka
    eureka:
      client:
        register-with-eureka: false
        fetch-registry: false
  7. 其他微服务中增加注册中心配置。
    eureka:
      client:
        service-url:
          defaultzone: http://localhost:11011/eureka/
  8. 依次启动eureka、demoservicea、demoserviceb,访问http://localhost:11011/,可以看到demoservicea、demoserviceb的信息。
    图10 查看微服务注册信息
  9. 调用接口:http://localhost:8082/order/1。
    图11 调用接口

相关文档

网站地图