以代码为主:
1.创建maven父工程
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.scbg</groupId>
<artifactId>springcloud_parent</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version><!--针对springboot2.0最新版本的spring cloud-->
<type>pom</type><!--pom方式构建工程-->
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2.创建EurekaServer子模块
1)并在子模块pom增加如下:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
2)配置application.yml
3)启动类
/**
* @program: springcloud_parent
* @description: 启动类
* @author: 三层饼干儿
* @create: 2019-08-27 19:05
**/
@SpringBootApplication//声明其为SpringBoot服务入口
@EnableEurekaServer//声明该类是Eureka注册中心
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
启动后浏览器可以看到界面
3.创建客户端(其实是一个服务提供者,但是对于eureka而言提供者消费者都是客户端,而且消费者同时也可能是提供者)
1)创建模块
pom中添加:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
2)配置文件
3)启动类
/**
* @program: springcloud_parent
* @description: 客户端启动类
* @author: 三层饼干儿
* @create: 2019-08-27 19:25
**/
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class,args);
}
}
4)模拟一个功能,毕竟要提供服务嘛
实体类
dao接口及实现类,这里用用map模拟数据库
package com.scbg.repository;
import com.scbg.pojo.Student;
import java.util.Collection;
/**
* @program: springcloud_parent
* @description: dao层接口
* @author: 三层饼干儿
* @create: 2019-08-27 19:32
**/
public interface StudentRepository {
public Collection<Student> findAll();
public Student findById(long id);
public void save(Student student);
public void deleteById(long id);
}
package com.scbg.repository.impl;
import com.scbg.pojo.Student;
import com.scbg.repository.StudentRepository;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* @program: springcloud_parent
* @description:
* @author: 三层饼干儿
* @create: 2019-08-27 19:35
**/
@Repository
public class StudentRepositoryImpl implements StudentRepository {
private static Map<Long,Student> map;
static {
map= new HashMap();
Student student1= new Student(1L,"饼干",27);
Student student2= new Student(2L,"蛋蛋",30);
Student student3= new Student(3L,"山山",28);
map.put(1L,student1);
map.put(2L,student2);
map.put(3L,student3);
}
@Override
public Collection<Student> findAll() {
return map.values();
}
@Override
public Student findById(long id) {
return map.get(id);
}
@Override
public void save(Student student) {
map.put(student.getId(),student);
}
@Override
public void deleteById(long id) {
map.remove(id);
}
}
contoller
package com.scbg.Controller;
import com.scbg.pojo.Student;
import com.scbg.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
/**
* @program: springcloud_parent
* @description:
* @author: 三层饼干儿
* @create: 2019-08-27 19:46
**/
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentRepository studentRepository;
@RequestMapping("/findAll")
public Collection<Student> findAll(){
return studentRepository.findAll();
}
@PostMapping("/findById/{id}")
public Student findById(@PathVariable("id") long id){
return studentRepository.findById(id);
}
@PostMapping("/save")
public void save(@RequestBody Student student){
studentRepository.save(student);
}
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id") long id){
studentRepository.deleteById(id);
}
}
完事postman测试一下接口能否正常访问。
4.同上方法创建客户端模块
1)引入依赖,同上
2)配置文件,基本同上
3)创建启动类,这里因为要实现访问服务,使用到了RestTemplate,启动类要加一点东西
4)完善controller
package com.scbg.controller;
import com.scbg.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.Collection;
import java.util.Map;
/**
* @program: springcloud_parent
* @description:
* @author: 三层饼干儿
* @create: 2019-08-27 21:42
**/
@RestController
@RequestMapping("/student")
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/findAll")
public Collection<Student> findAll(){
return restTemplate.getForObject("http://localhost:8010/student/findAll", Collection.class);
}
@RequestMapping("/findById/{id}")
public Student findById(@PathVariable("id")long id){
return restTemplate.getForObject("http://localhost:8010/student/findById/{id}",Student.class,id);
}
@RequestMapping("/findById2/{id}")
public Student findById2(@PathVariable("id")long id){
ResponseEntity<Student> forEntity = restTemplate.getForEntity("http://localhost:8010/student/findById/{id}", Student.class, id);
System.out.println("----forEntity---->"+forEntity);
Student student = forEntity.getBody();
System.out.println("---student------>"+student);
return student;
}
@PostMapping("/save")
public void save(@RequestBody Student student){
restTemplate.postForEntity("http://localhost:8010/student/save",student,null);
}
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id")long id){
restTemplate.delete("http://localhost:8010/student/deleteById/{id}",id);
}
}
5)使用postman进行测试发现方法均可调用成功。