《 Rest 微服务加入 Eureka(单机) 》
前言
在上一篇博文中,主要对 “ 微服务基础模块 ” 进行创建,并在 《 Eureka 基本理论概述 》 中对 Eureka 进行了介绍,本篇主要在微服务基础模块之上再加入Eureka实现服务的注册与发现,Eureka 项目子模块包括:
- 新增服务发现与注册中心模块,服务名称为 “ microservice-config-eureka-7001 ”;
- 修改的微服提供者模块,服务名称 “ microservice-provider-8001 ”;
- 修改的微服务消费者模块,服务名称 “ microservice-consumer-80 ”;
Rest 微服务加入 Eureka(单机)
1、微服务注册中心子模块 “ microservice-eureka-7001 ” ,如下图:
POM 内容如下:
<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>
<parent>
<groupId>com.huazai.springcloud</groupId>
<artifactId>microservice</artifactId>
<version>${project.version}</version>
</parent>
<artifactId>microservice-eureka-7001</artifactId>
<dependencies>
<!--eureka-server服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
YML 内容如下:
server:
port: 7001 # 自定义服务端口
eureka:
instance:
hostname: www.eureka7001.com # eureka服务端的实例名称
client:
register-with-eureka: false # false表示不向注册中心注册自己。
fetch-registry: false # false表示自己端就是注册中心,其职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 设置与 Eureka Server 交互的地址查询服务和注册服务都需要依赖这个地址(单机时使用的)。
主启动类 “ MicroserviceEurekaApp_7001 ” ,内容如下:
package com.huazai.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
*
* @author HuaZai
* @contact [email protected]
* <ul>
* @description
* <li>Eureka 注册中心1号服务器
* </ul>
* @className MicroserviceEurekaApp_7001
* @package com.huazai.springcloud
* @createdTime 2018年05月26日 下午5:46:11
*
* @version V1.0.0
*/
@SpringBootApplication
@EnableEurekaServer // 表示 EurekaServer 服务器端启动类,并接受其它微服务注册进来
public class MicroserviceEurekaApp_7001
{
public static void main(String[] args)
{
SpringApplication.run(MicroserviceEurekaApp_7001.class, args);
}
}
微服务子模块注册中心项目创建完成后,项目工程概览如下图:
测试,输入 Eureka 注册中心服务地址,会成功出现 Eureka 相关界面,如下图:
2、将微服务提供者子模块 “ microservice-provider-8001 ” 注册到 Eureka 服务注册中心,在改服务提供者子模块中修改的内容如下:
POM 新增内容:
<!-- 将微服务provider侧注册进eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
YML 新增内容:
eureka:
client: #客户端注册进eureka服务列表内
service-url:
defaultZone: http://注册中心服务地址/eureka # 单机的使用的
在启动内容新增 “ @EnableEurekaClient ” 的注解,内容如下:
package com.huazai.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
*
* @author HuaZai
* @contact [email protected]
* <ul>
* @description
* <li>服务提供者1号服务器
* </ul>
* @className MicroserviceProviderApp_8001
* @package com.huazai.springcloud
* @createdTime 2018年05月6日 下午2:21:44
*
* @version V1.0.0
*/
@SpringBootApplication
@EnableEurekaClient // 本服务启动后会自动注册进eureka服务中
public class MicroserviceProviderApp_8001
{
public static void main(String[] args)
{
SpringApplication.run(MicroserviceProviderApp_8001.class, args);
}
}
测试
先启动 Eureka 微服务注册中心,再启动微服务提供者子模块,这时访问 Eureka 注册中心服务时,在其注册列表中就可以看到微服务提供者子模块注册进来了,如下图:
3、补充微服务信息,这儿以微服务提供者子模块 “ microservice-provider-8001 ” 为例,
修改(自定义)注册中心服务列表中的服务名称,自定义服务名称,需要在该模块中使用 “ eureka.instance.instance-id ” 来指定,如下图:
显示某个服务提供者的IP地址,需要使用 “ eureka.instance.prefer-ip-address ” 来配置,其值默认为false,修改为 ture 即可显示个微服务模块的IP地址了,如下图:
自定义微服务的 info 内容,当配置完成后,点击注册中心列表中的每个微服务,就会看到定义的 info 内容了,使用 “ actuator ” 实现,
POM 新增内容:
<!-- actuator与注册微服务信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
* 友情提示:这儿使用 actuator 只是实现了微服务信息的补充,使用 actuator 还能实现服务的健康检查、审计、统计以及HTTP的追踪等操作,有没有很 6 的感觉呀。
在父级项目 “ microservice ” 的 POM 的 dependencyManagement 同级添加如下内容:
<!-- 配置加载的资源以及插件,否则后面项目会报错,无法启动 -->
<build>
<finalName>microservicecloud</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimit>$</delimit>
</delimiters>
</configuration>
</plugin>
</plugins>
</build>
测试:
关于 info 内容的定义,主要便于系统维护与运营,如果那个子模块出现了问题,就可以快速找到或者联系相关的负责人进行处理,所以 info 的内容越详细越好。
4、服务发现(Discovery)
修改微服务提供者模块 “ microservice-provider-8001 ” ,通过 DiscoveryClient 组件实现,在 DepartmentController 中新增 getDiscoverList 方法来获取,内容如下:
package com.huazai.springcloud.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.huazai.springcloud.entity.Department;
import com.huazai.springcloud.service.DepartmentService;
@RestController
@RequestMapping(value = "/department")
public class DepartmentController
{
@Autowired
private DepartmentService departmentService;
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping(value = "/add", method = RequestMethod.POST)
public boolean add(@RequestBody Department department)
{
// Department department = new Department(name);
return departmentService.add(department);
}
@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
public void delete(@PathVariable Long id)
{
System.out.println(id);
}
@RequestMapping(value = "/update", method = RequestMethod.PUT)
public void update(@RequestBody Department department)
{
}
@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
public Department get(@PathVariable Long id)
{
return departmentService.get(id);
}
@RequestMapping(value = "/list")
public List<Department> list()
{
return departmentService.list();
}
@RequestMapping(value = "/getDiscoverList", method = RequestMethod.GET)
public Object getDiscoverList()
{
// 发现服务列表
List<String> list = discoveryClient.getServices();
list.stream().forEach(item -> System.out.println(item));
// 输出服务的详细信息
List<ServiceInstance> instances = discoveryClient.getInstances("MICROSERVICE-PROVIDER");
instances.stream().forEach(inst -> System.out.println("serviceId:" + inst.getServiceId() + ",host:"
+ inst.getHost() + ",port:" + inst.getPort() + ",uri:" + inst.getUri()));
return discoveryClient;
}
}
开启对服务发现的支持,该启动类中新增 “ @EnableDiscoveryClient ” 的注解,内容如下:
package com.huazai.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
*
* @author HuaZai
* @contact [email protected]
* <ul>
* @description
* <li>服务提供者1号服务器
* </ul>
* @className MicroserviceProviderApp_8001
* @package com.huazai.springcloud
* @createdTime 2019年1月6日 下午2:21:44
*
* @version V1.0.0
*/
@SpringBootApplication
@EnableEurekaClient // 本服务启动后会自动注册进eureka服务中
@EnableDiscoveryClient // 开启服务发现
public class MicroserviceProviderApp_8001
{
public static void main(String[] args)
{
SpringApplication.run(MicroserviceProviderApp_8001.class, args);
}
}
测试,先启动 Eureka 服务注册中心服务器,再启服务提供者模块服务器,访问并获取服务列表信息 服务地址 + Uri ,成功获取到了列表信息,如下图:
GitLab 源码地址:
项目源码地址(zip格式的工程包):
好了,关于 Spring Cloud 进阶--Eureka服务注册与服务发现(单机版) 就写到这儿了,如果还有什么疑问或遇到什么问题欢迎扫码提问,也可以给我留言哦,我会一一详细的解答的。
歇后语:“ 共同学习,共同进步 ”,也希望大家多多关注CSND的IT社区。
作 者: | 华 仔 |
联系作者: | [email protected] |
来 源: | CSDN (Chinese Software Developer Network) |
原 文: | https://blog.csdn.net/Hello_World_QWP/article/details/85913125 |
版权声明: | 本文为博主原创文章,请在转载时务必注明博文出处! |