一、eureka注册中心

1、创建一个工程

工程名:microservicecloud-eureka-7001

2、向pom文件中增加如下:

<dependencies>
<!--eureka-server服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>

3、添加application.yml文件

server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己。Eureka不响自己注册
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ (单机)
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

4、增加服务启动类,并在类上增加  @EnableEurekaServer 注解, 如下所示:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
@EnableEurekaServer // EurekaServer服务器端启动类,接受其它微服务注册进来
public class EurekaServer7001
{
public static void main(String[] args)
{
SpringApplication.run(EurekaServer7001.class, args);
}
}

5、启动服务,在浏览器中打开 http://localhost:7001,显示如下图

二、服务注册到Eureka

1、创建工程

工程名:microservicecloud-provider-dept-8001

2、在pom.xml文件,新增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>

3、配置文件application.yaml,新增如下内容:

eureka:
client: # 客户端注册进eureka内
service-url:
defaultZone: http://localhost:7001/eureka/

application.yaml中的全部内容如下:

server:
port: 8001 mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: com.yufeng.springcloud.entities # 所有Entity别名类所在包
mapper-locations:
- classpath:mybatis/mapper/**/*.xml # mapper映射文件 spring:
application:
name: microservicecloud-dept

datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
url: jdbc:mysql://192.168.172.20:3306/cloudDB01 # 数据库名称
username: root
password: root
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-total: 5 # 最大连接数
max-wait-millis: 200 # 等待连接获取的最大超时时间 eureka:
client: # 客户端注册进eureka内
service-url:
defaultZone: http://localhost:7001/eureka/

4、代码的启动类中增加 @EnableEurekaClient 注解;

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

5、按照顺序启动服务,先启动 eureka7001 服务,接着启动 microservicecloud-provider-dept-8001 服务,浏览器打开: http://localhost:7001

三、服务的发现

对于注册到Eureka里面的微服务,可以通过服务发现来获得该服务的信息。

1、在服务提供的微服务的controller注入DiscoveryClient类

@RestController
public class DeptController
{
@Autowired
//@Resource(name = "deptServiceImpl")
private DeptService service; @Autowired
private DiscoveryClient discoveryClient; @RequestMapping(value = "/dept/add", method = RequestMethod.POST)
public boolean add(@RequestBody Dept dept)
{
return service.add(dept);
} @RequestMapping(value = "/dept/discovery", method = RequestMethod.GET)
public Object discovery()
{
List<String> list = discoveryClient.getServices(); //发现eureka中的微服务列表
System.out.println("eureka中所有的微服务的name列表" + list); //从eureka中获取指定name的微服务的信息(yml文件中配置的 spring.application.name)
List<ServiceInstance> instances = discoveryClient.getInstances(list.get(0));
for(ServiceInstance instance : instances)
{
System.out.println(instance.getServiceId() + "\t" + instance.getHost() + "\t"
+ instance.getPort() + "\t" + instance.getUri());
}
return this
.discoveryClient;
}

}

2、启动类中增加 @EnableDiscoveryClient 注解

@SpringBootApplication
@EnableEurekaClient //本服务启动后自动注册到eureka中
@EnableDiscoveryClient //服务的发现, 暴露出来
public class DeptProvider8001_App
{
public static void main(String[] args)
{
SpringApplication.run(DeptProvider8001_App.class, args);
}
}

  

创建服务的注册与发现 Eureka (四)的更多相关文章

  1. SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(转载)

    SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本) 转载请标明出处:http://blog.csdn.net/forezp/article/details ...

  2. SpringCloud-微服务的注册与发现Eureka(二)

    一.SpringCloud简介 Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均 ...

  3. SpringCloud-微服务的注册与发现Eureka

    一.SpringCloud简介 Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均 ...

  4. 服务的注册与发现Eureka(二)

    1.服务治理概念 在传统rpc远程调用中,服务与服务依赖关系,管理比较复杂,所以需要使用服务治理,管理服务与服务之间依赖关系,可以实现服务调用.负载均衡.容错等,实现服务发现与注册. 2.服务的注册与 ...

  5. SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本)

    一.spring cloud简介 鉴于<史上最简单的Spring Cloud教程>很受读者欢迎,再次我特意升级了一下版本,目前支持的版本为Spring Boot版本2.0.3.RELEAS ...

  6. 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f1-eureka/ 本文出自方志朋的博客 一.spring ...

  7. 第一篇:服务的注册与发现Eureka(Finchley版本)

    一.创建服务注册中心(Eureka) 1. 首先创建一个maven主工程 创建一个主Maven工程,在其pom文件引入依赖,spring Boot版本为2.0.3.RELEASE,Spring Clo ...

  8. SpringCloud学习(一)服务的注册与发现Eureka(Finchley版本)

    创建服务注册中心 在这里,我还是采用Eureka作为服务注册与发现的组件. 首先创建一个空项目 首先创建一个空项目,再创建一个maven项目,首先创建一个主Maven工程,在其pom文件引入依赖,sp ...

  9. 微服务深入浅出(3)-- 服务的注册和发现Eureka

    现来说一些Eureka的概念: 1.服务注册 Register 就是Client向Server注册的时候提供自身元数据,比如IP和Port等信息. 2.服务续约 Renew Client默认每隔30s ...

随机推荐

  1. vue+vuex+axios实现登录、注册页权限拦截

    1.修改config文件夹里的dev.env.js里的BASE_API,把地址改成请求后端的公共部分 1 BASE_API: '"http://192.168.xx.xx"', 2 ...

  2. one-hot编码理解

    one-hot是比较常用的文本特征特征提取的方法. one-hot编码,又称“独热编码”.其实就是用N位状态寄存器编码N个状态,每个状态都有独立的寄存器位,且这些寄存器位中只有一位有效,说白了就是只能 ...

  3. Visual Studio中定义OVERFLOW不能用

    在Visual Studio中对OK.ERROR.OVERFLOW进行宏定义,但只有OVERFLOW不能正常使用为什么呢? #define OK 1: #define ERROR 0: #define ...

  4. LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...

  5. 关于python的多行注释,启动新浏览器,循环语句乘法口诀

    1,提问:如何将python写的多行代码改写成注释,进行写下一段代码?这样可以在多个脚本中写东西? 回答:百度了一下,还真有 选中所要注释的代码  CTRL + / 然后所选的代码前面都会出现#,编程 ...

  6. 看Spring注解之IOC记录

    首先看源码里有些是java的元注解记录的有如下几个: @Inherited注释:指明被注解的类会自动继承.更具体地说,如果定义注解时使用了 @Inherited 标记,然后用定义的注解来标注另一个父类 ...

  7. SpringBoot 配置Redis

    application.properties 文件内容 #Redis数据库索引(默认为0) spring.redis.database=0 #Redis服务器地址 spring.redis.host= ...

  8. python学习 生成随机函数 random模块的用法

    random模块是用于生成随机数 常用函数 函数 含义 random() 生成一个[0,1.0)之间的随机浮点数 uniform(a,b) 生成一个a到b之间的随机浮点数 randint(a,b) 生 ...

  9. node.js异步编程的几种模式

    Node.js异步编程的几种模式 以读取文件为例: 1.callback function const fs = require('fs'); //callback function fs.readF ...

  10. 前端页面JS和CSS以及图片加载nginx报错:net::ERR_CONTENT_LENGTH_MISMATCH的解决与检查

    首先检查nginx权限 具体可参考地址https://www.cnblogs.com/hooly/p/9951748.html 或者百度其他方法 还有种情况,之前是可以用的,突然出现这种加载报错的情况 ...