从永远到永远-SpringCloud再学习(一)-HelloWorld

以代码为主:

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进行测试发现方法均可调用成功。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值