3.1 创建Eureka子工程

  • 完整工程源码:https://gitee.com/forwardxiang/spring-cloud-demo.git

3.1.1 IDEA操作

  • 鼠标选中父工程并且点击鼠标右键选择新建一个module

  • 新建包结构和必要的启动类和配置文件如下:

3.2 配置Eureka服务

3.2.1 pom引入依赖

  • 除了从父工程继承的依赖外,还需要在本工程pom里配置:

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

3.2.2 启动类EurekaServerApplication

  • 启动类编写(或者用工具自动生成,请自行百度):

    package cn.forwardxiang;import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer //这里是启动Eureka服务端
    @SpringBootApplication
    public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}
    }
    

3.2.3 配置文件application.yml

  • 用于配置本服务端口以及声明Eureka相关配置:

    server:port: 10086
    spring:application:name: eureka-server
    eureka:client:service-url:# eureka 服务地址,如果是集群的话;需要指定其它集群eureka地址defaultZone: HTTP://127.0.0.1:10086/eureka# 不注册自己register-with-eureka: false# 不拉取服务fetch-registry: falseserver:enable-self-preservation: false # 关闭自我保护模式(缺省为打开)
    

3.3 改造业务微服务

  • 业务微服务需要引入Eureka客户端依赖:

    <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    
  • 在业务微服务启动类上需要打开服务注册功能:

    //省略
    @EnableEurekaClient//开启服务注册
    public class BillManagerServiceApplication {//省略
    }
    
  • 在业务微服务配置文件里需要配置Eureka服务端信息:

    #省略
    eureka:client:service-url:defaultZone: HTTP://127.0.0.1:10086/eurekainstance:prefer-ip-address: true
    

3.4 改造消费微服务(可选)

  • 改造完成的业务微服务就会把自己注册到Eureka,供消费者消费。

3.4.1 消费者工程创建和配置

  • 作为一个子工程创建,未改造前除了继承的依赖外,只需要引入:

    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
  • 未改造前启动类:

    @SpringBootApplication
    public class ConsumerApplication {public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class, args);}@Bean //用于声明消费服务方的通信客户端,交给spring容器管理public RestTemplate restTemplate(){return new RestTemplate();}
    }
    
  • 未改造前消费服务方服务的接口:

    @RestController
    @RequestMapping("/consumer")
    public class ConsumerController {@Autowiredprivate RestTemplate restTemplate;@GetMapping("/{id}")public User queryById(@PathVariable Long id){String url = "HTTP://localhost:9091/user/" + id;return restTemplate.getForObject(url, User.class);}
    }
    

3.4.2 改造消费者

  • 新增Eureka客户端依赖,在yml配置文件里声明Eureka服务端地址。

  • 启动类改造:

    //省略
    @EnableDiscoveryClient //开启服务发现
    public class BillManagerServiceApplication {//省略
    }
    
  • 消费接口更改为依靠Eureka动态提供的url服务:

    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id) {List<ServiceInstance> serviceInstances = discoveryClient.getInstances("user-service");ServiceInstance serviceInstance = serviceInstances.get(0);String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/user/"+ id;return restTemplate.getForObject(url, User.class);
    }
    

SpringCloud工程搭建之Eureka服务搭建(必选)相关推荐

  1. SpringCloud五大神兽之Eureka服务注册(三)——Eureka的自我保护

    一.ACID与CAP 传统ACID: A:atomicity   原子性 C:consistency    一致性 I:isolation    独立性 D:durability    持久性 目前流 ...

  2. SpringCloud五大神兽之Eureka服务注册(一)

    一.什么是Eureka? 官方翻译:Eureka是一个基于Rest的服务,用于定位服务,以实现云端中间层服务发现和故障转移.即--Eureka主管服务的注册与发现. 在微服务架构中,只需要使用服务的标 ...

  3. 搭建 NPM 私有服务搭建入坑记

    前两天刚写完如何基于 CNPM 搭建一个私有服务,然后搭建到了线上正式服务器上,结果今天就栽坑里了.具体坑如下. 一.服务器磁盘空间被爆满 笔者搭建了两私有服务,一套在测试机器,一套在线上机器,然而自 ...

  4. NLB服务搭建与WDS服务搭建

    NLB服务搭建 两台2008虚拟机 搭建前准备: 先配置两台虚拟机IP,并更名 然后把IP与计算机名添加到hosts文件下,两台都需要添加 重启计算机 ping一下看两台虚拟机能否ping通 两台虚拟 ...

  5. mysql主从配置duxi_[从0到1搭建ABP微服务] - 搭建授权服务

    一.简介 授权中心是微服务架构中最为核心重要的环节,不仅为web.app等客户端提供身份授权服务,还对其他微服务提供身份认证服务.ABP微服务架构中使用identityServer4框架进行身份管理, ...

  6. Spring Cloud eureka服务搭建

    一.创建springboot工程.Eureka注册中心 1.引入maven依赖 <parent><groupId>org.springframework.boot</gr ...

  7. 服务部署在linux上,将springcloud工程中的一个服务部署到linux服务器上

    将工程打包成jar包,使用xshell登录到linux服务器上 ip:192.168.0.129 port:22 用户名:root 密码:123456 将jar包上传到linux中 使用脚本rrk-s ...

  8. NGINX服务搭建详解

    nginx安装配置 NGINX常用模块 nginx模块分为nginx官方模块以及nginx第三方模块 Nginx编译选项 模块作用 ngx_http_core_module 包含一些核心的http参数 ...

  9. 搭建TinyTiny rss服务

    搭建TinyTiny rss服务 搭建RSS订阅服务 1.准备一台云服务器 2.登录云服务器(用SHELL远程工具) 3.安装宝塔 4.访问宝塔官网(https://www.bt.cn/) 5.复制对 ...

最新文章

  1. 消息扩散(强连通分量)
  2. round--向最近的整数取整
  3. ci框架去除index.php,ci框架如何隐藏index.php
  4. 06--MySQL自学教程:DML(Data Manipulation Language:数据库操作语言),只操作表
  5. mysql-5.7.37-winx64解压版安装超详细图文教程
  6. STL源码剖析---空间配置器
  7. 视频编解码器常见问题介绍
  8. 给2021的Java一些建议,纯干货
  9. python ctp接口_Python-CTP PyCTP 接口
  10. vantui navbar底部出现白线修改之后仍存在白线问题
  11. Java实现简单计算器功能
  12. LIN、K、CAN、CAN-FD、FlexRay、Ethernet 、MOST、VAN常见汽车总线简介
  13. airtest获取手机分辨率,通过相对坐标定位元素
  14. python爬虫爬取百度文档
  15. LC145 Binary Tree Postorder Traversal
  16. laya的tween的坑
  17. 前5名最佳SQL数据库恢复软件
  18. Doxygen常用命令
  19. 容器和IaaS:谁动了谁的奶酪 ?
  20. Bookshop(一)数据库连接

热门文章

  1. 实证研究的步骤_【财务实证研究方法】SAS05反腐败复制流程介绍
  2. RockBrain USB Server-云计算虚拟化 USB设备集中管理、远程共享解决方案
  3. 03系统多界面_基于MATLAB的多方法车牌识别系统[带GUI界面+万字技术文档+直播]
  4. 华文教育计算机样卷,暨大召开《华文教师证书》考试题库建设协作会
  5. 贺利坚给编程初学者的金玉良言
  6. 最优控制电池储能模型 蓄电池储能模型的最优控制python源代码
  7. 金融科技系列 |如何 0-1 快速构建数字化客户运营体系
  8. 计算机关闭和注销的区别,你可知道windows系统中注销、关机、休眠之间的区别吗...
  9. iOS开发知识点总结(三)
  10. [转]关于男女两性关系的名言幽默!经典!!