Spring Cloud Combat Tiny-service provider

Source: Internet
Author: User
Tags findone ticket

Catalogue Explanation:

 I. The concept of service providers and service consumers

Ii. writing a test class for a service provider (code)

1.1, the use of micro-services to build a distributed system, micro-services across the network to communicate. We use micro-service providers and service consumers to describe the invocation relationship between microservices.

As follows:

There are examples in life when we watch a movie, the user initiates a ticket purchase request. Before the business operation of ticket purchase, the film micro service needs to invoke the interface of the user microservices,

Query the current user's balance is how much, is not in line with the purchase of the standard and so on. In this scenario, the user micro-service is a service provider , and the film micro-service is a service consumer .

2.2 Writing a service provider

The framework is structured as follows:

    

    

    Steps:

2.1 Create a Maven project, the schema above, pom.xml the content as follows:

<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "http://maven.apache.org/POM/4.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelversion>4.0.0</modelversion>  <groupId>Com.itmuch.cloud</groupId>  <Artifactid>Microservice-simple-provider-user</Artifactid>  <version>0.0.1-snapshot</version>  <Packaging>Jar</Packaging>  <!--introduction of Spring boot dependencies -  <Parent>    <groupId>Org.springframework.boot</groupId>    <Artifactid>Spring-boot-starter-parent</Artifactid>    <version>1.4.3.RELEASE</version>  </Parent>  <Properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    <java.version>1.8</java.version>  </Properties>  <Dependencies>    <Dependency>      <groupId>Org.springframework.boot</groupId>      <Artifactid>Spring-boot-starter-web</Artifactid>    </Dependency>    <Dependency>      <groupId>Org.springframework.boot</groupId>      <Artifactid>Spring-boot-starter-data-jpa</Artifactid>    </Dependency>    <Dependency>      <groupId>Com.h2database</groupId>      <Artifactid>H2</Artifactid>    </Dependency>    <Dependency>      <groupId>Org.springframework.boot</groupId>      <Artifactid>Spring-boot-starter-actuator</Artifactid>    </Dependency>  </Dependencies>  <!--the introduction of Spring Cloud dependencies -  <dependencymanagement>    <Dependencies>      <Dependency>        <groupId>Org.springframework.cloud</groupId>        <Artifactid>Spring-cloud-dependencies</Artifactid>        <version>Camden.sr4</version>        <type>Pom</type>        <Scope>Import</Scope>      </Dependency>    </Dependencies>  </dependencymanagement>  <!--add Spring-boot's maven plugin -  <Build>    <Plugins>      <plugin>        <groupId>Org.springframework.boot</groupId>        <Artifactid>Spring-boot-maven-plugin</Artifactid>      </plugin>    </Plugins>  </Build></Project>

2.2 Ready to build the table statement

drop table User if Exists;create table user (id bigint generated by default as identity, username varchar, name Varcha R (), age int (3), Balance decimal (10,2), primary key (ID));

2.2 Prepare several statements.

INSERT into user (ID, username, name, age, Balance) VALUES (1, ' account1 ', ' Zhang San ', ' 100.00 '); INSERT into user (ID, usernam  E, name, age, Balance) values (2, ' Account2 ', ' John Doe ', D, 180.00); INSERT into user (ID, username, name, age, balance) values (3, ' Account3 ', ' Harry ', 32, 280.00);

  

2.3 Preparing entity classes

Package Com.itmuch.cloud.study.entity;import Java.math.bigdecimal;import Javax.persistence.column;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import Javax.persistence.generationtype;import  javax.persistence.Id; @Entitypublic class User {@Id @GeneratedValue (strategy = generationtype.auto) Private Long Id;  @Column private String username;  @Column private String name;  @Column private Integer age;  @Column private BigDecimal balance;  Public Long GetId () {return this.id;  } public void SetId (Long id) {this.id = ID;  } public String GetUserName () {return this.username;  } public void Setusername (String username) {this.username = username;  } public String GetName () {return this.name;  } public void SetName (String name) {this.name = name;  } public Integer Getage () {return this.age;  public void Setage (Integer age) {this.age = age;  } public BigDecimal GetBalance () {return this.balance; } public void Setbalance (BigDecimal balance) {this.balance = balance; }}

  

2.3 Writing DAO classes

2.3 Writing Controller Classes

Package Com.itmuch.cloud.study.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.restcontroller;import Com.itmuch.cloud.study.entity.user;import Com.itmuch.cloud.study.repository.UserRepository, @RestControllerpublic class Usercontroller {  @Autowired  Private Userrepository userrepository;  @GetMapping ("/{id}") Public  User FindByID (@PathVariable Long ID) {    user FindOne = This.userRepository.findOne ( ID);    return findOne;}  }

2.4 Writing the Startup class

Package Com.itmuch.cloud.study;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplicationpublic class provideruserapplication {public  static void Main (string[] args) {    Springapplication.run ( Provideruserapplication.class, args);}  }

2.5 Writing the configuration file

Server:  port:8000spring:  JPA:    generate-ddl:false    show-sql:true    hibernate:      Ddl-auto: None  DataSource:                           # Specify data source    platform:h2                        # Specify data source Type    schema:classpath:schema.sql        # Specify the H2 database script    data:classpath:data.sql            # Specify the data script for the H2 database logging:                                # Configure the log level to let hibernate print out the executed SQL  Level:    root:info    org.hibernate:INFO    Org.hibernate.type.descriptor.sql.BasicBinder:TRACE    Org.hibernate.type.descriptor.sql.BasicExtractor:TRACE    # # Infoinfo:   app:     name: @[email protected]    encoding: @[email protected]    java:      Source: @[email protected]      target: @[email protected]

Finally in the browser input: LOCALHOST:8000/1 saw the user information, then succeeded

Follow up for everyone to summarize, there is not thoughtful to write the place, welcome everyone to put forward

Spring Cloud Combat Tiny-service provider

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.