Spring Cloud Initialization Project

Posted by     "Jordon Li" on Wednesday, February 19, 2020

TOC

Spring Cloud Init Project

Description

Registration Center: netflix-eureka-server
Configuration Center: config-server
Service Provider: netflix-eureka-client starter-config
Serving Consumer: netflix-eureka-client starter-config starter-openfeign

Load balance by feign.

Create maven project

  • Introduce dependencies spring-cloud-demo\pom.xml:
      <parent>
          <artifactId>spring-boot-starter-parent</artifactId>
          <groupId>org.springframework.boot</groupId>
          <version>2.0.7.RELEASE</version>
      </parent>
    
      <dependencies>
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
    
          <!-- JDK 9 缺失jar -->
          <dependency>
              <groupId>javax.xml.bind</groupId>
              <artifactId>jaxb-api</artifactId>
              <version>2.3.0</version>
          </dependency>
    
          <dependency>
              <groupId>com.sun.xml.bind</groupId>
              <artifactId>jaxb-impl</artifactId>
              <version>2.3.0</version>
          </dependency>
    
          <dependency>
              <groupId>com.sun.xml.bind</groupId>
              <artifactId>jaxb-core</artifactId>
              <version>2.3.0</version>
          </dependency>
    
          <dependency>
              <groupId>javax.activation</groupId>
              <artifactId>activation</artifactId>
              <version>1.1.1</version>
          </dependency>
    
          <dependency>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
              <optional>true</optional>
          </dependency>
      </dependencies>
    
      <dependencyManagement>
          <dependencies>
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-dependencies</artifactId>
                  <version>Finchley.SR2</version>
                  <type>pom</type>
                  <scope>import</scope>
              </dependency>
          </dependencies>
      </dependencyManagement>
    

Add Registration Center Module

  • Introduce dependencies spring-cloud-demo\eurekaserver\pom.xml:

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
    </dependencies>
    
  • Add config eurekaserver\src\main\resources\application.yml:

    server:
      port: 8761
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
        register-with-eureka: false
        fetch-registry: false
    
  • Add startup class eurekaserver\src\main\java\com\augmentum\EurekaServerApplication.java:

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

Add Configuration Center Module

  • Introduce dependencies spring-cloud-demo\configserver\pom.xml:

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
    </dependencies>
    
  • Add config configserver\src\main\resources\application.yml:

    server:
      port: 8762
    spring:
      application:
        name: configserver
      profiles:
        active: native  #Get service provider configs from native or git.
    
      cloud:
        config:
          server:
            native:
              search-locations: classpath:/shared    #Service provider native config path.
    
    
  • Add startup class configserver\src\main\java\com\augmentum\ConfigServerApplication.java:

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

Add Service Provider Module

  • Introduce dependencies spring-cloud-demo\menu\pom.xml:

      <dependencies>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
              <version>2.0.2.RELEASE</version>
          </dependency>
    
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-config</artifactId>
              <version>2.0.2.RELEASE</version>
          </dependency>
    
          <dependency>
              <groupId>org.mybatis.spring.boot</groupId>
              <artifactId>mybatis-spring-boot-starter</artifactId>
              <version>1.3.1</version>
          </dependency>
    
          <dependency>
              <groupId>mysql</groupId>
              <artifactId>mysql-connector-java</artifactId>
              <version>8.0.11</version>
          </dependency>
      </dependencies>
    
  • Add entity menu\src\main\java\com\augmentum\entity\Menu.java:

    import lombok.Data;
    
    @Data
    public class Menu {
        private long id;
        private String name;
        private double price;
        private String flavor;
    }
    
  • Add Repository interface menu\src\main\java\com\augmentum\repository\MenuRepository.java:

    public interface MenuRepository {
        public List<Menu> findAll(int index, int limit);
        public int count();
        public Menu findById(long id);
        public void save(Menu menu);
        public void update(Menu menu);
        public void delete(long id);
    }
    
  • Add Mapper menu\src\main\resources\mapping\MenuRepository.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.augmentum.repository.MenuRepository">
        <select id="findAll" resultType="Menu">
            select * from t_menu limit #{param1}, #{param2}
        </select>
    
        <select id="count" resultType="int">
            select count(id) from t_menu
        </select>
    
        <select id="findById" parameterType="long" resultType="Menu">
            select * from t_menu where id = #{id}
        </select>
    
        <insert id="save" parameterType="Menu">
            insert into t_menu(name, price, flavor) values(#{name}, #{price}, #{flavor})
        </insert>
    
        <update id="update" parameterType="Menu">
            update t_menu set name = #{name}, price = #{price}, flavor = #{flavor} where id = #{id}
        </update>
    
        <delete id="delete" parameterType="long">
            delete from t_menu where id = #{id}
        </delete>
    </mapper>
    
  • Add Handler menu\src\main\java\com\augmentum\controller\MenuHandler.java:

    @RestController
    @RequestMapping("/menu")
    public class MenuHandler {
    
        @Value("${server.port}")
        private String port;
    
        @Autowired
        private MenuRepository menuRepository;
    
        @GetMapping("/index")
        public String index() {
            return "menu's port : " + port;
        }
    
        @GetMapping("/findAll/{index}/{limit}")
        public List<Menu> findAll(@PathVariable("index") int index, @PathVariable("limit") int limit) {
            return menuRepository.findAll(index, limit);
        }
    }
    
  • Add Service Provider config to Configuration Cneter configserver\src\main\resources\shared\menu-dev.yml:

    server:
      port: 8020
    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/springclouddemo
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver
    mybatis:
      mapper-locations: classpath:/mapping/*.xml
      type-aliases-package: com.augmentum.entity
    
  • Add config to read Configuration Center config menu\src\main\resources\bootstrap.yml:

    spring:
      application:
        name: menu
      profiles:
        active: dev
      cloud:
        config:
          uri: http://localhost:8762    # Configuration center uri
    
          fail-fast: true
    
  • Add startup class menu\src\main\java\com\augmentum\MenuApplication.java:

    @SpringBootApplication
    @MapperScan("com.augmentum.repository")
    public class MenuApplication {
        public static void main(String[] args) {
            SpringApplication.run(MenuApplication.class, args);
        }
    }
    

Add Service Consumer Module

  • Introduce dependencies spring-cloud-demo\client\pom.xml:

      <dependencies>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
              <version>2.0.2.RELEASE</version>
          </dependency>
    
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-config</artifactId>
              <version>2.0.2.RELEASE</version>
          </dependency>
    
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-openfeign</artifactId>
              <version>2.0.2.RELEASE</version>
          </dependency>
    
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-thymeleaf</artifactId>
          </dependency>
      </dependencies>
    
  • Add entity client\src\main\java\com\augmentum\entity\Menu.java:

    import lombok.Data;
    
    @Data
    public class Menu {
        private long id;
        private String name;
        private double price;
        private String flavor;
    }
    
  • Add Feign interface client\src\main\java\com\augmentum\feign\MenuFeign.java:

    @FeignClient(value = "menu")
    public interface MenuFeign {
        @GetMapping("/menu/findAll/{index}/{limit}")
        public List<Menu> findAll(@PathVariable("index") int index, @PathVariable("limit") int limit);
    }
    
  • Add Handler client\src\main\java\com\augmentum\controller\ClientHandler.java:

    @RestController
    @RequestMapping("/client")
    public class ClientHandler {
    
        @Autowired
        private MenuFeign menuFeign;
    
        @GetMapping("/findAll/{index}/{limit}")
        public List<Menu> findAll(@PathVariable("index") int index, @PathVariable("limit") int limit) {
            return menuFeign.findAll(index, limit);
        }
    }
    
  • Add Service Consumer config to Configuration Cneter configserver\src\main\resources\shared\client-dev.yml:

    server:
      port: 8030
    spring:
      thymeleaf:
        prefix: classpath:/static/
        suffix: .html
    
  • Add config to read Configuration Center config client\src\main\resources\bootstrap.yml:

    spring:
      application:
        name: client
      profiles:
        active: dev
      cloud:
        config:
          uri: http://localhost:8762    # Configuration center uri
    
          fail-fast: true
    
  • Add startup class client\src\main\java\com\augmentum\ClientApplication.java:

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

Issue

github: https://github.com/leezhuang96/spring-cloud-demo

「下次一定」

下次一定

使用微信扫描二维码完成支付