14 micro electric service providers [Mall] excellent dark horse music: day02-springcloud (in theory papers)

Download notes and information on the project, please click on this sentence itself acquired.

day01-springboot (theory papers)  ; day01-springboot (practice papers)

day02-springcloud (Part theory)   ; day02-springcloud (in theory part)   ;

14 micro electric service providers [Mall] excellent dark horse music: day02-springcloud


0. learning objectives

  • Understand the evolution of the system architecture
  • Http understand the difference between RPC and the
  • Master the simple use of HttpClient
  • You know what is SpringCloud
  • Eureka registration center set up independent
  • Robbin independently configured load balancing

 


 

4. acquaintance SpringCloud

Service is a way of micro-architecture, technical architecture will eventually need to implement.

Implementation of micro-services a lot, but most of the fire than the Spring Cloud. why?

  • Background Hard: Spring as a family, there is a whole family bucket Spring patron, very strong background.
  • Strong technology: Spring field as a senior Java, it can be said to be profound skill. There are strong technical support team, most people really than not.
  • A good mass base: You can say that most programmers are accompanied by the growth of the Spring Framework, we ask: Now there are several companies to develop without Spring? Each frame SpringCloud seamless integration with Spring, for all of us everything is familiar.
  • Easy to use: I think we all appreciate the convenience SpringBoot brought to our development, and SpringCloud fully support the development of SpringBoot, with very little configuration can be done to build a micro-service framework. 

4.1 Introduction

SpringCloud is one of Spring's project, the official website address: http: //projects.spring.io/spring-cloud/

Spring is the best at integration of the world's best framework to take over, into their own projects.

SpringCloud is the same, it is now very popular in some of the technology integration together to achieve such as: configuration management, service discovery, intelligent routing, load balancing, fuses, control bus, cluster status and more. Its main components includes:

netflix

  • Eureka: Registry
  • Zuul: Services Gateway
  • Ribbon: Load Balancing
  • Feign: service call
  • Hystix: Fuse

These are just part of the architecture diagram:

 

4.2 version

SpringCloud version of the name is rather special, because it is not a component, but a collection of many components, and its name is the A to Z of some words composed of letters headed

 

We in the project, will be based on the version of Finchley.

Which contains the components also have their own version.

 Greenwich builds and works with Spring Boot 2.1.x, and is not expected to work with Spring Boot 1.5.x.

Next, we learn eleven key components of SpringCloud.


 

The micro-simulation scenario service

First, we need to simulate the scene of a service call . Easy to learn behind micro Services Architecture

5.1. Service Provider

We create a new project, provide access to outside users of the service.

5.1.1.Spring scaffolding Create Project

By means of quickly build tools Spring provides:

 

 Fill Project Information:

 

 Adding web-dependent:

 Add mybatis dependence:

 Spring Boot version can be manually modified version not shown in the low pom.xml automatically build in after generation!

 

 Fill Project Location:

 

Generated project structure:

 

 Dependence has all the automatic introduction:

 Of course, due to the use of generic mapper, so we need to manually add a dependent:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.leyou.demo</groupId>
    <artifactId>user-service-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>user-service-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--springboot测试的起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--SpringBoot热部署开发模式启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <!--DAO层相关的启动器-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- 通用mapper启动器 -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

 

Very fast ah!

 

5.1.2 write code.

Add a Foreign query (micro Services Architecture) Interface :

Controller layer 

@RestController  //等同于@Controller+@ResponseBody
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService userService;
    
    @GetMapping("/{id}")  //
    public User queryById(@PathVariable("id") Long id){
        return this.userService.queryById(id);
    }
}

 

Service implementation class interface layer

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    public User queryById(Long id) {
        return this.userMapper.selectByPrimaryKey(id);
    }
}

 

DAO layer to provide universal Interface Mapper query

@Mapper 
@Repository   // comment action here to be tested, after addition of IDEA Service layer not being given 
public  interface UserMapper the extends  tk.mybatis.mapper.common.Mapper <the User> { 
    
}

Entity classes:

@Table(name = "tb_user")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    //@GeneratedValue(strategy = GenerationType.IDENTITY)
    @KeySql(useGeneratedKeys = true)
    private long id;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    // 用户名
    privateUserName String;
     // password 
    Private String password;
     // Name 
    Private String name;
     // Age 
    Private Integer Age;
     // Sex, 1 male, 2 females 
    Private Integer Sex;
     // date of birth 
    Private a Date Birthday;
     // Remarks 
    Private String Note;
     // Create time 
    Private a Date created;
     // update time 
    Private a Date updated; 

    // ===== omitted below GET / set methods and toString () method ===== //

 

Properties profile, here we use the yaml grammar, rather than properties:

application-demo.yml

the Spring: 
  the DataSource: 
    # four connection configuration 
    Driver - class - name: com.mysql.cj.jdbc.Driver 
    url: jdbc: MySQL: // ? localhost: 3306 / leyou serverTimezone = Asia / on Shanghai 
    username: root 
    password: root 
    # type connection pool 
    type: com.zaxxer.hikari.HikariDataSource 
    # connection pool customized configuration 
    Hikari: 
      maximum -pool-size: 20 is 
      Minimum -idle: 10 


MyBatis: 
  # configure scan package entity classes entity 
  type -aliases- package : cn.bjut .pojo, cn.bjut.model, cn.bjut.domain 


#tomcat server port 
server: 
  port: 8081
The servlet #: 
# @RequestMapping annotation corresponds to the view layer method ( "/*.do" ) 
# path: "* .do" 


# log4j log output control 
#logging: 
# Level: 
# cn.bjut.interceptor: Debug

application.yml

 

spring:
  profiles:
    active: demo

5.1.3 Start and test:

Start the project, access interface: HTTP: // localhost: 8081 / the User / 7

 

 

 

 

 

================================================

References:

 

 

end

Guess you like

Origin www.cnblogs.com/MarlonKang/p/11621292.html