什么是Eureka组件

spring cloud Eureka ,提供服务注册和服务发现的功能。

一:spring cloud Eureka

Eureka Server 服务端

Eureka Client 客户端

服务注册

代码实现:

1.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">
<modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId>
<artifactId>springCloud01</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>EurekaServer</module>
</modules>
<!-- 父工程-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.0.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JDk9-->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency> </dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>

2.父工程下实现一个子模块Module实现Eureka Server

<?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>springCloud01</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>EurekaServer</artifactId> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies> </project>

3.配置文件

server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka/

4.创建启动类:

package com.southwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer
@SpringBootApplication
public class EuiekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EuiekaServerApplication.class,args);
}
}

5.检查打开

http://localhost:8761

注册第一个微服务

服务提供者

服务消费者和服务提供者都是通过Eureka Client 连接到Eureka Server完成注册。

1.创建一个模块,实现Eureka Client

<?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>springCloud01</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>provider</artifactId> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies> </project>

2.配置文件:

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

3.创建启动类:

package com.southwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class ProvideApplication {
public static void main(String[] args) {
SpringApplication.run(ProvideApplication.class,args);
}
}

4.数据持久化:

接口:

package com.southwind.repository;

import com.southwind.entity.Student;

import java.util.Collection;

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

实现类:

package com.southwind.repository.impl;

import com.southwind.entity.Student;
import com.southwind.repository.StudentRepository; import java.util.Collection;
import java.util.HashMap;
import java.util.Map; public class StudentRepositoryImpl implements StudentRepository {
private static Map<Integer,Student> map;
static {
map=new HashMap<>();
map.put(1,new Student(1,"张三"));
map.put(1,new Student(2,"李四"));
map.put(1,new Student(3,"王五"));
}
@Override
public Collection<Student> findAll() {
return map.values();
} @Override
public Student findById(Integer id) {
return map.get(id);
} @Override
public void saveOrUpdate(Student student) {
map.put(student.getId(),student);
} @Override
public void deleteById(Integer id) {
map.remove(id);
}
}

Handler:

package com.southwind.Handler;

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("/provider")
public class StudentHandler {
@Autowired
private StudentRepository studentRepository;
@GetMapping("/findall")
public Collection<Student> findall(){
return studentRepository.findAll();
}
@GetMapping("/findbyid/{id}")
public Student findById(@PathVariable("id") Integer id){
return studentRepository.findById(id);
}
@PostMapping("/save")
public void save(@RequestBody Student student){
studentRepository.saveOrUpdate(student);
}
@DeleteMapping("/delete")
public void deletebyid(Integer id){
studentRepository.deleteById(id);
} }

最新文章

  1. 免费馅饼——G
  2. 关于EM,REM,PX的几点注意
  3. php实现回复图文,图片,文字
  4. 模板-高精度BigInteger
  5. Android自定义ScrollView实现一键置顶功能
  6. Bad apple for CSharp
  7. always block內省略else所代表的電路
  8. Table显示滚动条
  9. django: urlconfig
  10. 《Microsoft SQL Server企业级平台管理实践》笔记
  11. NSIS:实现程序窗口逐渐透明的渐入渐出效果
  12. Linux压缩解压命令汇总
  13. opencv 3.1.0 访问像素值的三种方法(C++)
  14. 在MFC中通过访问IP地址下载文件到本地
  15. C 基于数组存储的堆栈实现
  16. 我与C++的初识
  17. 快速搭建ELK日志分析系统
  18. JQ 向上查找指定 同辈元素 找到后返回
  19. 基于Arcface Android平台的人脸识别实现
  20. mssql,mysql,Oracle 数据库中获得UUID字符串

热门文章

  1. zabbix6.0安装
  2. 基于python的数学建模---传染病六个模型
  3. Velocity模板引擎的的使用示例(入门级)
  4. python实现图片转换pdf
  5. MySQL的select for update用法
  6. 深入解读MySQL InnoDB存储引擎Update语句执行过程
  7. 开局一张图,构建神奇的 CSS 效果
  8. android studio 写一个桌球简单页面
  9. 网络监测工具之Zabbix的搭建与测试方法(二)-- SNMP、OID和MIB概述
  10. [编程基础] C++多线程入门10-packaged_task示例