springcloud learning 2-- service provider

Learn from the station b springcloud, now summarize the summary removal of a small error appearing in the video, some of the error-prone places were reminded
b outbound links: https://www.bilibili.com/video/av55304977

data link:
https://pan.baidu.com/s/1o0Aju3IydKA15Vo1pP4z5w
extraction code: 21ru

On a link: https://blog.csdn.net/qq_40893824/article/details/103323782

The next section link: https://blog.csdn.net/qq_40893824/article/details/103326409

The following list summarizes: sub-project → pom → application → entity of Student → → handler → Interface and Implementation classes start

Implementation details:
1. Create a maven project --eurekaclient (service provider)

to fill eurekaclient after next

after Finish

visibility successfully created

2. Fill in the code pom file of the project:

Code:

	<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
    </dependencies>

3. The creation of application.yml



create success, fill in the code:

server:
  port: 8010
spring:
  application:
    name: provider
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
#      是否将当前服务的 IP 注册到 Eureka Server。

4. Create a startup class:
create com.southwind package is created under java, I do not know see the first article of 8. There say:
https://blog.csdn.net/qq_40893824/article/details/103323782

Create the following southwind start

entering ProviderApplication.java

fill in the code:

package com.southwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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


5. Create entity classes: entity → Student
Here Insert Picture Description
Here Insert Picture Description


into the Student in

which there is the code @Data, this needs to be added in dependence of the parent pom

<!--下面这个依赖对应于eurekaclient中entity的@Data-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>

<Scope> provided </ scope> indicates that the provider of the package is only used when compiling and testing
in the tagging Student:

package com.southwind.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private long id;
    private String name;
    private int age;
}

6. Create Interface:
Here Insert Picture Description
Package: repository
Here Insert Picture Description
Here Insert Picture Description
name: StudentRepository, its type to an interface type
Here Insert Picture Description
into the StuentRepository
fill:

package com.southwind.repository;

import com.southwind.entity.Student;
import java.util.Collection;

public interface StudentRepository {
    /*增 改*/
    public void saveorupdate(Student student);
    /*删*/
    public void deleteById(long id);
    /*查*/
    public Collection<Student> findAll();
    /*查*/
    public Student findById(long id);
}

7. impl create a package at repotory
create --StudentRepositoryImpl StudentRepository implementation classes in the impl
Here Insert Picture Description
Here Insert Picture Description
to implement operation of the interface to implements StudentRepository
mouseover StudentRepositoryImpl, alt + after enter, fantastic scene:
Here Insert Picture Description
entering arrows:

autocomplete Code:

fill in the code:
Do not forget @Repository

package com.southwind.repository.impl;

import com.southwind.entity.Student;
import com.southwind.repository.StudentRepository;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Repository
public class StudentRepositoryImpl implements StudentRepository{

    private static Map<Long,Student> studentMap;

    static{
        studentMap = new HashMap<>();
        studentMap.put(1L,new Student(1L,"张三",12));
        studentMap.put(2L,new Student(2L,"李四",22));
        studentMap.put(3L,new Student(3L,"王五",15));
    }

    @Override
    public void saveorupdate(Student student) {
        studentMap.put(student.getId(),student);
    }

    @Override
    public void deleteById(long id) {
        studentMap.remove(id);
    }

    @Override
    public Collection<Student> findAll() {
        return studentMap.values();
    }

    @Override
    public Student findById(long id) {
        return studentMap.get(id);
    }
}

8. Create Handler
created at southwind package Controller
Controller created StudentHandler
Here Insert Picture Description
fill codes:

package com.southwind.controll;

import com.southwind.entity.Student;
import com.southwind.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@RestController
@RequestMapping("/student")
public class StudentHandler {

    @Autowired
    private StudentRepository studentRepository;

    /*增 改*/
    @PostMapping("/save")
    public void save(@RequestBody Student student){
        studentRepository.saveorupdate(student);
    }
    @PutMapping("/update")
    public void update(@RequestBody Student student){
        studentRepository.saveorupdate(student);
    }

    /*删*/
    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") long id){
        studentRepository.deleteById(id);
    }

    /*查*/
    @GetMapping("/findAll")
    public Collection<Student> findAll(){
        return studentRepository.findAll();
    }

    /*查*/
    @GetMapping("/findById/{id}")
    public Student findById(@PathVariable("id") long id){
        return studentRepository.findById(id);
    }

}

Open class start
Here Insert Picture Description
Here Insert Picture Description
operating normally
enter http: // localhost: 8761
Here Insert Picture Description
service providers have registered came
into the HTTP: // localhost: 8010 / Student / findAll
Here Insert Picture Description
9. detailed testing in Postman in
a full investigation get → http:. // localhost: 8010 / Student / findAll
Here Insert Picture Description
b → Find HTTP GET:. // localhost: 8010 / Student / findById / 3
Here Insert Picture Description
c into a person's information.
POST → HTTP: // localhost: 8010 / Student / the Save
Here Insert Picture Description
d have to see. no deposit into
get → http: // localhost: 8010 / student / findAll
Here Insert Picture Description
successful deposit into!
e updates.
PUT → HTTP: // localhost: 8010 / Student / Update
Here Insert Picture Description
. f look no updates
get → http: // localhost: 8010 / student / findAll
Here Insert Picture Description
successfully updated!

On a link: https://blog.csdn.net/qq_40893824/article/details/103323782

The next section link: https://blog.csdn.net/qq_40893824/article/details/103326409

Published 42 original articles · won praise 2 · Views 1191

Guess you like

Origin blog.csdn.net/qq_40893824/article/details/103324622