【springcloud学习(dalston.sr1)】Eureka 客户端服务注册(含源代码)(四)

[复制链接]
发表于 2025-9-1 01:55:41 | 显示全部楼层 |阅读模式
d该系列项目整体介绍及源代码请参照前面写的一篇文章【springcloud学习(dalston.sr1)】项目整体介绍(含源代码)(一)
 springcloud学习(dalston.sr1)系统文章汇总如下:
【springcloud学习(dalston.sr1)】项目整体介绍(含源代码)(一)
【springcloud学习(dalston.sr1)】Eureka服务端集群的搭建(含源代码)(二)
【springcloud学习(dalston.sr1)】Eureka单个服务端的搭建(含源代码)(三)
【springcloud学习(dalston.sr1)】Eureka 客户端服务注册(含源代码)(四)
【springcloud学习(dalston.sr1)】服务消耗者通过restTemplate来访问服务提供者(含源代码)(五)
【springcloud学习(dalston.sr1)】Eureka 服务发现(含源代码)(六)
【springcloud学习(dalston.sr1)】Ribbon负载平衡(含源代码)(七)
【springcloud学习(dalston.sr1)】利用Feign实现接口调用(含源代码)(八)
【springcloud学习(dalston.sr1)】Hystrix服务熔断(含源代码)(九)
【springcloud学习(dalston.sr1)】Hystrix服务降级(含源代码)(十)
【springcloud学习(dalston.sr1)】Hystrix Dashboard服务监控监控(含源代码)(十一)
【springcloud学习(dalston.sr1)】Zuul路由访问映射规则设置及利用(含源代码)(十二)
【springcloud学习(dalston.sr1)】Config设置中心-ConfigServer端与Git通信(含源代码)(十三)
【springcloud学习(dalston.sr1)】Config设置中心-Configclient端通过和Config server端通信来获取设置文件信息(含源代码)(十四)

这篇文章主要介绍Eureka客户端服务注册到eureka的server端。
上篇文章【springcloud学习(dalston.sr1)】Eureka单个服务端的搭建(含源代码)(三)里,我们已经创建好了eureka服务端工程。现在我们来创建一个简单的eureka客户端工程。
该项目是一个简单的spring boot微服务,主要是一个简单的接口,涉及到数据库的访问。通过访问数据库的商品表,来查询一个商品列表,并返回json数据给前端。该模块会用到microservicecloud-api项目的商品实体类,所以在pom文件中,必要引入该项目的依赖。
必要提前准备好数据库的一张表,这里我用的是postgres数据库,创建了一个商品表,包括商品ID和商品名称两个字段。如下图

该项目的整体代码结构如下:

(一)Eureka客户端创建
(1)在IEDA中springcloud2015项目下新建一个microservicecloud-provider-8001的模块,如下图:

(2)Maven依赖添加
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <parent>
  6.         <artifactId>springcloud2025</artifactId>
  7.         <groupId>com.company</groupId>
  8.         <version>1.0-SNAPSHOT</version>
  9.     </parent>
  10.     <modelVersion>4.0.0</modelVersion>
  11.     <artifactId>microservicecloud-provider-8001</artifactId>
  12.     <dependencies>
  13.         <dependency>
  14.             <groupId>com.company</groupId>
  15.             <artifactId>microservicecloud-api</artifactId>
  16.             <version>${project.version}</version>
  17.         </dependency>
  18.         <dependency>
  19.             <groupId>junit</groupId>
  20.             <artifactId>junit</artifactId>
  21.         </dependency>
  22.         <dependency>
  23.             <groupId>org.postgresql</groupId>
  24.             <artifactId>postgresql</artifactId>
  25.         </dependency>
  26.         <dependency>
  27.             <groupId>com.alibaba</groupId>
  28.             <artifactId>druid</artifactId>
  29.         </dependency>
  30.         <dependency>
  31.             <groupId>org.mybatis.spring.boot</groupId>
  32.             <artifactId>mybatis-spring-boot-starter</artifactId>
  33.         </dependency>
  34.         <dependency>
  35.             <groupId>org.springframework.boot</groupId>
  36.             <artifactId>spring-boot-starter-jetty</artifactId>
  37.         </dependency>
  38.         <dependency>
  39.             <groupId>org.springframework.boot</groupId>
  40.             <artifactId>spring-boot-starter-web</artifactId>
  41.         </dependency>
  42.         <dependency>
  43.             <groupId>org.springframework.boot</groupId>
  44.             <artifactId>spring-boot-starter-test</artifactId>
  45.         </dependency>
  46.         <!-- 将服务provider注册进eureka -->
  47.         <dependency>
  48.             <groupId>org.springframework.cloud</groupId>
  49.             <artifactId>spring-cloud-starter-eureka</artifactId>
  50.         </dependency>
  51.         <dependency>
  52.             <groupId>org.springframework.cloud</groupId>
  53.             <artifactId>spring-cloud-starter-config</artifactId>
  54.         </dependency>
  55.         <!-- actuator监控监控信息完善 -->
  56.         <dependency>
  57.             <groupId>org.springframework.boot</groupId>
  58.             <artifactId>spring-boot-starter-actuator</artifactId>
  59.         </dependency>
  60.     </dependencies>
  61. </project>
复制代码
可以看到POM文件用到了microservicecloud-api模块,该模块结构如下图:

(3)在application.yml中添加如下设置文件信息
  1. server:
  2.   port: 8001
  3. mybatis:
  4.   config-location: classpath:mybatis/mybatis.cfg.xml  #mybatis配置文件所在路径
  5.   type-aliases-package: com.company.api.entity  #所有Entity别名类所在包
  6.   mapper-locations: classpath:mybatis/mapper/*.xml #mapper映射文件
  7. spring:
  8.   application:
  9.     name: microservicecloud-goods
  10.   datasource:
  11.     type: com.alibaba.druid.pool.DruidDataSource  #当前数据源操作类型
  12.     driver-class-name: org.postgresql.Driver    #postgres驱动包
  13.     url: jdbc:postgresql://localhost:5432/postgres    #数据库名称
  14.     username: postgres
  15.     password: 123456
  16.     dbcp2:
  17.       min-idel: 5           #数据库连接池的最小维持连接数
  18.       initial-seze: 5       #初始化连接数
  19.       max-total: 5          #最大连接数
  20.       max-wait-millis: 200  #等待链接获取的最大超时时间
  21. eureka:
  22.   client: #客户端注册进eureka服务列表里
  23.     service-url:
  24.       defaultZone: http://localhost:7001/eureka/ #这里填eureka服务端的地址
  25.       #http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ #设置与erueka server交互的地址查询服务和注册服务,都需要依赖于这个地址
  26.   instance:
  27.     instance-id: microservicecloud-goods8001 #这里用于修改eureka服务注册列表的status字段值,替换默认的展示
  28.     prefer-ip-address: true #服务注册列表中的status字段值,其访问路径可以显示IP地址,而不是展示localhost
  29. info:
  30.   app.name: company-microservicecloud
  31.   company.name: www.company.com
  32.   build.artifactId: $project.artifactId$
  33.   build.version: $project.version$
复制代码

(4)创建mybatis mapper xml文件,dao接口,service接口,controller接口,启动类
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3.         PUBLIC "-//mybatis-org//DTD Config 3.0//EN"
  4.         "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6.     <settings>
  7.         <setting name="cacheEnabled" value="true"/> <!-- 二级缓存开启 -->
  8.     </settings>
  9. </configuration>
复制代码
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3.         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.company.provider.dao.GoodsDao">
  5.     <select id="getGoods" resultType="com.company.api.entity.Goods">
  6.         select good_id as goodId, good_name || '数据库1' as goodName from public.lc_good limit 5
  7.     </select>
  8. </mapper>
复制代码
  1. package com.company.provider.dao;
  2. import com.company.api.entity.Goods;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import java.util.List;
  5. @Mapper
  6. public interface GoodsDao {
  7.     List<Goods> getGoods();
  8. }
复制代码
  1. package com.company.provider.service;
  2. import com.company.api.entity.Goods;
  3. import java.util.List;
  4. public interface GoodsService {
  5.     List<Goods> getGoods();
  6. }
复制代码
  1. package com.company.provider.service.impl;
  2. import com.company.api.entity.Goods;
  3. import com.company.provider.dao.GoodsDao;
  4. import com.company.provider.service.GoodsService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import java.util.List;
  8. @Service
  9. public class GoodsServiceImpl implements GoodsService {
  10.     @Autowired
  11.     private GoodsDao goodsDao;
  12.     @Override
  13.     public List<Goods> getGoods() {
  14.         return goodsDao.getGoods();
  15.     }
  16. }
复制代码
  1. package com.company.provider.controller;
  2. import com.company.api.entity.Goods;
  3. import com.company.provider.service.GoodsService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.cloud.client.ServiceInstance;
  6. import org.springframework.cloud.client.discovery.DiscoveryClient;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import java.util.List;
  11. @RestController
  12. @RequestMapping("/goods")
  13. public class GoodsController {
  14.     @Autowired
  15.     private DiscoveryClient discoveryClient;
  16.     @Autowired
  17.     private GoodsService goodsService;
  18.     @GetMapping("/list")
  19.     public List<Goods> getGoods() {
  20.         return goodsService.getGoods();
  21.     }
  22.     /**
  23.      * 服务发现,提供一个接口可以查询当前组件提供了哪些服务
  24.      * @return
  25.      */
  26.     @GetMapping("/discovery")
  27.     public Object discovery() {
  28.         List<String> services = discoveryClient.getServices();
  29.         System.out.println("discovery服务列表" + services);
  30.         List<ServiceInstance> instances = discoveryClient.getInstances("microservicecloud-goods".toUpperCase());
  31.         instances.forEach(x ->
  32.                 System.out.println("serviceId:" + x.getServiceId()
  33.         + ",host:" + x.getHost()
  34.         + ",port:" + x.getPort()
  35.         + ",uri:" + x.getUri()));
  36.         return discoveryClient;
  37.     }
  38. }
复制代码
  1. package com.company.provider;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  6. @SpringBootApplication
  7. @EnableEurekaClient //服务启动后会注册到eureka服务中
  8. @EnableDiscoveryClient //用于服务发现
  9. public class Provider8001Application {
  10.     public static void main(String[] args) {
  11.         SpringApplication.run(Provider8001Application.class, args);
  12.     }
  13. }
复制代码
(5)项目整体结构如下(注意:该项目引用的实体类Goods来自于microservicecloud-api项目,文章前面已有提及,并且已在pom文件中进行了引用):

(二)验证eureka客户端将服务注册到eureka服务端的效果。
(1)首先我们启动eureka服务端EurekaServer7001Application。
(2)启动完成后,在欣赏器输入:http://localhost:7001查察如下,可以看到服务注册列表为空的。

(3)然后我们启动eureka客户端Provider8001Application

(4)启动完成后,我们首先访问客户端的一个接口,以确认客户端服务是正常的,如下图。这样阐明结果客户端服务是正常的。
在欣赏器输入http://localhost:8001/goods/list

(5)然后我们在eureka页面(http://localhost:7001/)革新该网页,可以看到该页面多了一行,阐明客户端已经乐成注册到,并能在服务端看到。如下图:

(6)必要阐明的是如下几项设置如果不添加,则eureka服务注册界面的status字段的展示名称也会有所变革,以及鼠标放上去时,展示的链接地址是localhost,而不是IP地址;还有点击链接时会跳转至错误页,可以自行尝试下,试试效果




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表