006 SpringCloud study notes 2 ----- SpringCloud Basics

1.SpringCloud Overview

 

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.
SpringCloud advantages:

 

  - hard back: 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
  - 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 formula, familiar taste.
  - Easy to use: I think we all appreciate the convenience SpringBoot brought to our development, and fully supports the development of SpringBoot SpringCloud, the configuration can be done with very little micro-services framework to build

(1 Introduction

Spring is the best at integration , 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. The main components involved include:
- Eureka: service management components, including service registry, service registration and discovery mechanism to achieve. (Service management, service registry / discovery)
- Zuul: Gateway component that provides intelligent routing, access filtering
- Ribbon: Client load balancing service invocation component (client load)
- Feign: Ribbon and Hystrix statement service call, give style service invocation component (declarative service call)
- Hystrix: fault-tolerant management components, to achieve a circuit breaker model to help delay the emergence of service-dependent and robust fault tolerance to provide fault. (Fuse, circuit breakers, fault-tolerant)

Chart:

 

2. Micro scene simulation service

 

First, we need a service call simulation scenarios, to build two projects: lucky-service-provider (service provider) and lucky-service-consumer (caller to the service). Easy to learn behind micro Services Architecture
Service Provider: Using mybatis operation of the database, the data additions and deletions to achieve change search; and to provide external rest interface services.
Service consumer: using restTemplate remote call service provider rest interface services, access to data.

(1) Service Provider

We create a new project: lucky-service-provider, to provide services based on the user's query id.

<1> Spring Scaffolding (Spring Initializr) Create Project

 

The final resulting project structure:

(2) coding

<1> Configuration

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

server:
  port: 8081
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/springboot
    username: root
    password: plj824
mybatis:
  type-aliases-package: lucky.service.domain

<2> class entity

Package lucky.service.domain; 

Import javax.persistence.GeneratedValue;
 Import javax.persistence.GenerationType;
 Import javax.persistence.Id;
 Import javax.persistence.Table; 

/ ** 
 * entity class users table corresponding to 
 * Note: Users this class adds @ Table, @ Id, @ GeneratedValue other annotation 
 * using these annotations mapper needs to be added depends in pom file 
 * / 
the @Table (name = "Users" )
 public  class the Users { 

    @Id 
    @GeneratedValue (Strategy = GenerationType.IDENTITY)
     Private Integer ID; // primary key 
    Private String username; // 用户名
    private String password; // 密 码
    private String name; // 姓 名

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

<3>UsersMapper

package lucky.service.mapper;

import lucky.service.domain.Users;

public interface UsersMapper extends tk.mybatis.mapper.common.Mapper<Users>{

}

Note: mapper interface class needs to add annotations in springboot guide class ----LuckyServiceProviderApplication.java class @MapperScan

Package lucky.service; 

Import org.springframework.boot.SpringApplication;
 Import org.springframework.boot.autoconfigure.SpringBootApplication;
 Import tk.mybatis.spring.annotation.MapperScan; 

// declare a class is the bootstrap class springboot: springboot applications inlet 
@SpringBootApplication 
@MapperScan ( "lucky.service.mapper")   // pack scan mapper interface 
public  class LuckyServiceProviderApplication { 

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

}

<4>UsersService.java

package lucky.service;

import lucky.domain.Users;
import lucky.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UsersService {

    @Autowired
    private UsersMapper usersMapper;

    public Users queryUsersById(Integer id){
        return this.usersMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public void deleteUserById(Long id){
        this.usersMapper.deleteByPrimaryKey(id);
    }

    public List<Users> queryAllUsers() {
        return this.usersMapper.selectAll();
    }
}

<5>UsersController.java

package lucky.controller;

import lucky.domain.Users;
import lucky.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(path = "/users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    @RequestMapping(path = "/query")
    @ResponseBody
    public String queryUsers(){
        return "hello users";
    }

    @RequestMapping(path = "/queryUsersById")
    @ResponseBody
    public Users queryUsersById(@RequestParam("id") Integer id){
        return this.usersService.queryUsersById(id);

    @param
     *
     * query for all users, and displays at the front end/ **
    }model model object is used to pass data to the distal 
     * @return returns the view name
      * / 
    @RequestMapping (path = "/ queryAllUsers" )
     public String queryAllUsers (the Model Model) {
         // 1. User Search 
        List <the Users> Users = the this . usersService.queryAllUsers ();
         // 2. into the model 
        model.addAttribute ( "the Users" , the Users);
         // 3. return the template name (that is, classpath: / templates / html directory under the file name) 
        return "the Users" ; 
    } 

}

<6> Test results

(3) Service caller

Build a lucky-service-consumer service consumer project.

<1> Spring Scaffolding (Spring Initializr) to create a project (create different modules in the same engineering project)

(4) coding

<1> First class is registered in the guide RestTemplate:

package lucky.service.luckyserviceconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class LuckyServiceConsumerApplication {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
    
    public static void main(String[] args) {
        SpringApplication.run(LuckyServiceConsumerApplication.class, args);
    }

}

<2> write configuration (application.yml):

server:
  port: 8080

<3> Write UserController:

package lucky.service.controller;

import lucky.service.domain.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
@RequestMapping(path = "/consumer/user")
public class UserController {
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(path = "/queryUsersById")
    @ResponseBody
    public Users queryUserById(@RequestParam("id") Integer id){
        return this.restTemplate.getForObject("http://localhost:8081/users/queryUsersById?id="+id,Users.class);
    }


}

<4> entity class

Package lucky.service.domain; 

Import the java.io.Serializable; 

/ ** 
 * entity class corresponding to the users table 
 * / 

public  class the Users   the implements the Serializable { 

    Private Integer ID; // primary key 
    Private String username; // username 
    Private String password; // password 
    Private String name; // name 

    public Integer getId () {
         return ID; 
    } 

    public  void the setId (Integer ID) {
         the this .id =  ID;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

<5> Test results

 

Guess you like

Origin www.cnblogs.com/luckyplj/p/11447840.html