《SpringCloud超级入门》使用Eureka编写服务提供者《十》
创建项目注册到 Eureka
将一个服务提供者 eureka-client-user-service 注册到 Eureka 中,并提供一个接口给其他服务调用。
创建一个 Maven 项目,在 pom.xml 中增加相关依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath />
</parent><dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency><!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies><!-- Spring Cloud -->
<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>
创建一个启动类 App
@SpringBootApplication
@EnableDiscoveryClient
public class App {public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
注解换成 @EnableDiscoveryClient,表示当前服务是一个 Eureka 的客户端。
在 src/main/resources 下面创建一个 application.properties 属性文件,增加下面的配置:
spring.application.name= eureka-client-user-service
server.port=8081
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
# 采用IP注册
eureka.instance.preferIpAddress=true
# 定义实例ID格式
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
执行 App 启动服务,我们可以看到控制台中有输出注册信息的日志:
DiscoveryClient_EUREKA-CLIENT-USER-SERVICE/eureka-client-user-service:192.168.31.245:8081 - registration status: 204
2)编写提供接口
创建一个 Controller,提供一个接口给其他服务查询,代码如下所示。
@RestController
public class UserController {
@GetMapping("/user/hello")
public String hello() {
return “hello”;
}
}
重启服务,访问 http://localhost:8081/user/hello
看到我们返回的 Hello 字符串,证明接口成功。
上一篇 使用Eureka编写注册中心服务
下一篇介绍 使用Eureka编写服务消费者
《SpringCloud超级入门》使用Eureka编写服务提供者《十》相关推荐
- 《SpringCloud超级入门》Eureka注册中心开启密码认证《十二》
Eureka 自带了一个 Web 的管理页面,方便我们查询注册到上面的实例信息,但是有一个问题:如果在实际使用中,注册中心地址有公网 IP 的话,必然能直接访问到,这样是不安全的.所以我们需要对 Eu ...
- 《SpringCloud超级入门》Eureka自我保护模式和InstanceID的配置《十四》
关闭自我保护 保护模式主要在一组客户端和 Eureka Server 之间存在网络分区场景时使用.一旦进入保护模式,Eureka Server 将会尝试保护其服务的注册表中的信息,不再删除服务注册表中 ...
- 《SpringCloud超级入门》使用Eureka编写服务消费者《十一》
我们先从 Nginx 说起,了解为什么需要微服务.最初的服务化解决方案是给相同服务提供一个统一的域名,然后服务调用者向这个域发送 HTTP 请求,由 Nginx 负责请求的分发和跳转. 这种架构存在很 ...
- 《SpringCloud超级入门》使用Eureka编写注册中心服务《九》
首先创建一个 Maven项目,取名为 eureka-server,在 pom.xml 中配置 Eureka 的依赖信息,代码如下所示. <!-- Spring Boot --> <p ...
- 《SpringCloud超级入门》Spring Boot Starter的介绍及使用《七》
目录 Spring Boot Starter项目创建 自动创建客户端 使用 Starter 使用注解开启 Starter 自动构建 使用配置开启 Starter 自动构建 配置 Starter 内容提 ...
- 《springcloud超级入门》Spring Cloud和Dubbo的区别及各自的优缺点《三》
了解为什么需要微服务.最初的服务化解决方案是给相同服务提供一个统一的域名,然后服务调用者向这个域发送 HTTP 请求,由 Nginx 负责请求的分发和跳转. 这种架构存在很多问题:Nginx 作为中间 ...
- 《SpringCloud超级入门》Spring Cloud Eureka是什么?《八》
Spring cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责实现微服务架构中的服务治理功能. S ...
- 《SpringCloud超级入门》使用Eureka集群搭建《十三》
前面我们搭建的注册中心只适合本地开发使用,在生产环境中必须搭建一个集群来保证高可用.Eureka 的集群搭建方法很简单:每一台 Eureka 只需要在配置中指定另外多个 Eureka 的地址就可以实现 ...
- 《springcloud超级入门》Spring Cloud是什么?Spring Cloud版本介绍《二》
Spring cloud是一系列框架的有序集合.它利用 Spring Boot 的开发便利性,巧妙地简化了分布式系统基础设施的开发,如服务注册.服务发现.配置中心.消息总线.负载均衡.断路器.数据监控 ...
最新文章
- 挥手送别 2019,翘首期待 2020
- linux mac中实现类似secureCRT的clone session
- byte 和16进制关系
- 1730: 数区间(线段覆盖,贪心)
- 好玩gan_效果超赞服务器挤爆!用GAN生成人像油画火了,带你一秒回到文艺复兴...
- Mysql 中is null 和 =null 的区别
- C语言 FileStreaming 键盘与屏幕IO
- inheritConstructorStealing.js
- springboot中spring.profiles.active来引入多个properties文件 Springboot获取容器中对象
- WebService框架——CXF介绍
- SIP协议详解(中文)-5
- 【Java】29.常用API之lang.Throwable(异常情况大总结)
- 腾讯互娱开源分布式开发框架Pebble
- ps技巧:自动选择工具的使用
- DIB迪博数据库(2000-2018年)
- VMWare中ubuntu打开终端方法
- 搞IT的不可不知道的仙童“八叛徒”的故事(转)
- 测试笔试题汇总(测试开发)
- 基于内容的图片检索CBIR简介
- 中国人民大学计算机考研贴吧,中国人民大学计算机系统结构考研经验-人大信息学院考研辅导班...
热门文章
- 匿名内部类探究——它是一个实例
- C++PrimerPlus学习——第四章编程练习
- kafka集群脚本启动失败,在kafkaServer.out中提示nohup: failed to run command `java’: No such file or directory
- rootfs 制作ubuntu_Ubuntu12笔记: 基于busybox的Linux小系统制作
- 给mysql数据库设计编码_MYSQL数据库编码原理
- r导出html怎么保存,做植物谱系图,用Phylomatic软件将网页中的输出结果拷贝到文本文件中, 并另存为phylo...
- win7系统设置开机自动联网的方法
- TIM怎么设置禁止窗口抖动 TIM防抖设置技巧
- vue-cli搭建和“Cannot find module npm-cli.js” 、“operation not permitted” 、 deprecated coffee-script等错误
- php求北京时间方法,php怎么将获得的时间转换为北京时间