欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Cloud入門系列服務(wù)提供者總結(jié)

 更新時間:2021年06月16日 14:43:54   作者:全棧阿文  
這篇文章主要介紹了Spring Cloud入門系列之服務(wù)提供者總結(jié),服務(wù)提供者使用Eureka Client組件創(chuàng)建 ,創(chuàng)建完成以后修改某文件,具體操作方法及實例代碼跟隨小編一起看看吧

服務(wù)提供者使用Eureka Client組件創(chuàng)建
首先在父項目中創(chuàng)建Eureka Client Server項目(Maven)
創(chuàng)建完成以后,修改pom.xml文件

<?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">
    <parent>
        <artifactId>com.wenqi.springcloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>EurekaClient</artifactId>

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

</project>

編寫application.yml文件

server:
  port: 8010
  # 當(dāng)前服務(wù)注冊在eureka server上的名稱
spring:
  application:
    name: provider
  # 當(dāng)前注冊中心的訪問地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  # 是否將當(dāng)前服務(wù)的ip注冊到Eureka Server
  instance:
    prefer-ip-address: true

創(chuàng)建啟動類

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

啟動啟動類,這時候訪問http://localhost:8761/ 可以看到服務(wù)注冊進(jìn)來了。
接下來進(jìn)行增刪改查的簡單業(yè)務(wù)服務(wù)操作

創(chuàng)建實體類

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

創(chuàng)建repository

public interface StudentRepository {
    public Collection<Student> findAll();
    public Student findById(long id);
    public void saveOrUpdate(Student student);
    public void deleteById(long id);
}

創(chuàng)建repository實現(xiàn)類

@Repository
public class StudentRepositoryimpl implements StudentRepository {
    private static Map<Long,Student> studentMap;
    static {
        studentMap = new HashMap<Long, Student>();
        studentMap.put(1L,new Student(1L,"張三",22));
        studentMap.put(2L,new Student(2L,"王五",20));
        studentMap.put(3L,new Student(3L,"李四",21));
    }


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

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

    public void saveOrUpdate(Student student) {
        studentMap.put(student.getId(),student);
    }

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

實現(xiàn)增刪改查操作 controller

@RestController
@RequestMapping("/student")
public class StudentController {
    @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 student){
        studentRepository.saveOrUpdate(student);
    }
    @PutMapping("/update")
    public void update(@RequestBody Student student){
        studentRepository.saveOrUpdate(student);
    }
    @DeleteMapping("/delete/{id}")
    public void deleteById(@PathVariable("id") long id){
        studentRepository.deleteById(id);
    }
}

啟動Eureka Client,并啟動Eureka Server。
使用postman工具進(jìn)行測試

在這里插入圖片描述
在這里插入圖片描述

postman的使用自己下去進(jìn)行了練習(xí),不得不說測試還是很有必要的。

以上就是Spring Cloud入門服務(wù)提供者總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Spring Cloud服務(wù)提供者的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • java agent 使用及實現(xiàn)代碼

    java agent 使用及實現(xiàn)代碼

    java agent的作用可以在字節(jié)碼這個層面對類和方法進(jìn)行修改的技術(shù),能夠在不影響編譯的情況下,修改字節(jié)碼。本文主要給大家講解java agent 使用及實現(xiàn)代碼,感興趣的朋友一起看看吧
    2018-07-07
  • Java 并發(fā)編程學(xué)習(xí)筆記之Synchronized底層優(yōu)化

    Java 并發(fā)編程學(xué)習(xí)筆記之Synchronized底層優(yōu)化

    這篇文章主要介紹了Java 并發(fā)編程學(xué)習(xí)筆記之Synchronized底層優(yōu)化的相關(guān)資料,主要包含了重量級鎖,輕量級鎖,偏向鎖和其他優(yōu)化等方面,有需要的小伙伴可以參考下
    2016-05-05
  • ShardingSphere JDBC強(qiáng)制路由使用的項目實踐

    ShardingSphere JDBC強(qiáng)制路由使用的項目實踐

    在某些特定場景下,可能需要繞過分片規(guī)則直接定位到特定的數(shù)據(jù)庫或表,這種情況下就可以使用HintRouting,本文就來介紹一下ShardingSphere JDBC強(qiáng)制路由使用的項目實踐,感興趣的可以了解一下
    2024-06-06
  • springsecurity基于token的認(rèn)證方式

    springsecurity基于token的認(rèn)證方式

    本文主要介紹了springsecurity基于token的認(rèn)證方式,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • SpringBoot跨域Access-Control-Allow-Origin實現(xiàn)解析

    SpringBoot跨域Access-Control-Allow-Origin實現(xiàn)解析

    這篇文章主要介紹了SpringBoot跨域Access-Control-Allow-Origin實現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • java實現(xiàn)求只出現(xiàn)一次的數(shù)字

    java實現(xiàn)求只出現(xiàn)一次的數(shù)字

    本文主要介紹了java實現(xiàn)求只出現(xiàn)一次的數(shù)字,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Java反射機(jī)制如何解決數(shù)據(jù)傳值為空的問題

    Java反射機(jī)制如何解決數(shù)據(jù)傳值為空的問題

    這篇文章主要介紹了Java反射機(jī)制如何解決數(shù)據(jù)傳值為空的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • MyBatisPlus查詢報錯Unknow?column?‘id‘?in?‘field?list‘解決分析

    MyBatisPlus查詢報錯Unknow?column?‘id‘?in?‘field?list‘解決分析

    這篇文章主要為大家介紹了MyBatisPlus查詢報錯Unknow?column?‘id‘?in?‘field?list‘解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • 基于request.getAttribute與request.getParameter的區(qū)別詳解

    基于request.getAttribute與request.getParameter的區(qū)別詳解

    本篇文章小編為大家介紹,基于request.getAttribute與request.getParameter的區(qū)別詳解。需要的朋友參考下
    2013-04-04
  • Java數(shù)據(jù)結(jié)構(gòu)之散列表詳解

    Java數(shù)據(jù)結(jié)構(gòu)之散列表詳解

    散列表(Hash table,也叫哈希表),是根據(jù)關(guān)鍵碼值(Key value)而直接進(jìn)行訪問的數(shù)據(jù)結(jié)構(gòu)。本文將為大家具體介紹一下散列表的原理及其代碼實現(xiàn)
    2022-01-01

最新評論