Spring Cloud (3): Eureka Client service provider

1. Create a subproject and import the configuration as follows:

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

2. Create application.yml and add related configurations as follows:

server:
  port: 8010
spring:
  application:
    name: provider
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address:  true

property description

*  spring.application.name : 当前服务注册在 Eureka Server 上的名称。

*  eureka.client.service-url.defaultZone : 注册中⼼的访问地址。  

*  eureka.instance.prefer-ip-address : 是否将当前服务的 IP 注册到 Eureka Server。

3. Create a startup class, the code is as follows

package com.frr;

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

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

}

4. Start the registration center, and then start the service provider just written. When you visit the address of the registration center, you can see the following interface, and there are more provider services that have just been successfully registered.

insert image description here
#5. Actual combat! Simulate the addition, deletion and modification of students

  1. Create the student entity class as follows:
package com.frr.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data //生成Getter,Setter,equals,canEqual,hasCode,toString等方法
@AllArgsConstructor //添加一个构造函数,该构造函数含有所有已声明字段属性参数
@NoArgsConstructor //创建一个无参构造函数
public class Student {
    
    
	private long id;
	private String name;
	private int age;
}

Notes

*@Data    生成 Getter,Setter,equals,canEqual,hasCode,toString 等方法

*@AllArgsConstructor    添加一个构造函数,该构造函数含有所有已声明字段属性参数

*@NoArgsConstructor    创建一个无参构造函数
  1. Create an interface, the code is as follows:
package com.frr.repository;

import java.util.Collection;

import com.frr.entity.Student;

public interface StudentRepository {
    
    
	public Collection<Student> findAll();
	public Student findById(long id);
	public void saveOrUpdate(Student s);
	public void deleteById(long id);
}
  1. Create an implementation class and use static code blocks to realize data initialization. The code is as follows:
package com.frr.repository.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.frr.entity.Student;
import com.frr.repository.StudentRepository;

@Repository
public class StudentRepositoryImpl implements StudentRepository{
    
    
	
	private static Map<Long,Student> studentMap;

	static {
    
    
		studentMap = new HashMap<>();
		studentMap.put(1L, new Student(1L, "张三", 11));
		studentMap.put(2L, new Student(1L, "李四", 11));
		studentMap.put(3L, new Student(1L, "王五", 11));
	}
	
	@Override
	public Collection<Student> findAll() {
    
    
		return studentMap.values();
	}

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

	@Override
	public void saveOrUpdate(Student s) {
    
    
		studentMap.put(s.getId(), s);
	}

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

}
  1. Create a controller with the following code:
package com.frr.controller;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.frr.entity.Student;
import com.frr.repository.StudentRepository;

@RestController //在Spring中@RestController的作用等同于@Controller + @ResponseBody。
@RequestMapping("/student")
public class StudentHandler {
    
    
	@Autowired
	private StudentRepository studentRepository;
	
	@GetMapping("/findAll")
	public Collection<Student> findAll(){
    
    
		return studentRepository.findAll();
	}
	
	@GetMapping("/findById/{id}")
	public Student findById(@PathVariable("id") long id) {
    
    
		return studentRepository.findById(id);
	}
	
	@PostMapping("/save")
	public void save(@RequestBody Student s) {
    
    
		studentRepository.saveOrUpdate(s);
	}
	
	@PutMapping("/Update")
	public void Update(@RequestBody Student s) {
    
    
		studentRepository.saveOrUpdate(s);
	}
	
	@DeleteMapping("/deleteById/{id}")
	public void deleteById(@PathVariable("id") long id) {
    
    
		studentRepository.deleteById(id);
	}
}
  1. Restart the startup class created above
  2. Use the Postman tool to test each method, and the data is consistent to indicate success!
    insert image description here

6. Summary

A very basic microservice architecture has been built. The building process is simple and clear. It is to add some dependencies of spring cloud and some related configurations of eureka on the basis of the original spring boot. So to sum up, based on spring boot, spring cloud can make it have the functions of various components. So how does the service consumer call the interface of the service provider? Let us look forward to the next article Spring Cloud (4): the use of RestTemplate

One development engineer is also in the continuous learning stage, and the usual small experiences are shared from time to time. I hope that those who read the text I wrote can avoid detours and wish you success in work and study.
Bloggers have limited experience, if there are any shortcomings, welcome to communicate and improve together~ I hope to make progress together with you who are also in CSDN.

Author | Sweet Little Sweet Potato
Produced | Little Sweet Potato

Guess you like

Origin blog.csdn.net/qq_36836370/article/details/130870446