(7)SpringCloud - 将已有的部门微服务注册进Eureka服务中,主机映射修改,微服务info内容详细信息

1 理解

在前面 (6)我们构建了Eureka Server 服务注册成功,也就是下图的蓝色模块。(物业公司)

在这里插入图片描述

现在我们要做的事情就是把8001入住到7001项目中(Eureka Server 物业公司)中
如果入住成功,那么我们访问localhost:7001 显得页面中,红色区域是有东西的。
在这里插入图片描述
既然8001要入住到楼层中,那么我们是要交物业费给物业公司的。
那么我们怎么交物业费呢?
我们要把8001转变成Service Provider
在这里插入图片描述

2 POM设置

在8001中注入配置
需要添加的部分:


  <!-- 将微服务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>

完整的部分:

<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.atguigu.springcloud</groupId>
   <artifactId>microservicecloud</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </parent>


  <artifactId>microservicecloud-provider-dept-8001</artifactId>


  <dependencies>
   <!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->
   <dependency>
     <groupId>com.atguigu.springcloud</groupId>
     <artifactId>microservicecloud-api</artifactId>
     <version>${project.version}</version>
   </dependency>
   <!-- 将微服务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>
   <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
   </dependency>
   <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
   </dependency>
   <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid</artifactId>
   </dependency>
   <dependency>
     <groupId>ch.qos.logback</groupId>
     <artifactId>logback-core</artifactId>
   </dependency>
   <dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jetty</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</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>

3 YML

我们要入住到Eureka, 我们是不是首先要交物业费呢?
那么物业费的银行卡是多少呢?
我们需要给一个明确的地址。
在这里插入图片描述

我们在resources底下新建一个applicaition.yml

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

完整的YML内容

server:
  port: 8001
  
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
  type-aliases-package: com.atguigu.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://localhost:3306/cloudDB01              # 数据库名称
    username: root
    password: 123456
    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 DeptProvider8001_App主启动类

表明自己的身份,自己是Client端。
在这里插入图片描述

package com.atguigu.springcloud;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 
@SpringBootApplication
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中
public class DeptProvider8001_App
{
  public static void main(String[] args)
  {
   SpringApplication.run(DeptProvider8001_App.class, args);
  }
}
 
 

看两者的关系。 C-S结构
在这里插入图片描述

5 测试

运行7001和8001项目。
在这里插入图片描述
再次访问
localhost:7001
我们的物业中心。

在这里插入图片描述
发现该楼层已经入住了一个。
那么这个名字是怎么来的呢?
打开我们的8001的YML文件。在这里插入图片描述
但是注册到物业公司又Cloud 自动的把所有名称都弄成了大写。

6 注意

当我们启动程序之后,我们放在那里不需要,等10分钟之后刷新我们的端口,发现报错?
在这里插入图片描述
原来Eureka有自我保护机制

我们重新运行一下7001和8001端口。
再次访问端口,发现没有什么错误了。
在这里插入图片描述

7 主机服务名称修改

在这里插入图片描述
我们每次问哪个服务端的问题的时候,难道每次都说这个一长串的IP嘛?
接下来我们修改映射名字。

在这里插入图片描述
接下来,我们点击这个超链接,在这里插入图片描述发现报错?
在这里插入图片描述
当我们入住了楼层之后,我的队友想看一下入住了哪些微服务,我们点击超链接,发现端口自动变成localhost:8001/info

接下来,我们对info 信息的说明和主机映射名称的修改
打开8001的配置文件YML
修改
在这里插入图片描述
重新启动,发现名字修改完成。
在这里插入图片描述

8 主机IP信息提示

我们希望在最左下角那里,不是显示localhost:8001/info
我们希望显示它的IP地址。
在这里插入图片描述

进入8001的YML文件中。
添加如下语句即可。
在这里插入图片描述
再次运行之后,成功。
在这里插入图片描述

9 微服务info内容详细信息

当前问题:超链接点击服务报告ErrorPage

9.1 修改microservicecloud-provider-dept-8001的pom文件

打开我们的POM文件
添加配置
actuator监控信息完善



<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-actuator</artifactId>
   </dependency>

完整内容:

<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.atguigu.springcloud</groupId>
   <artifactId>microservicecloud</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </parent>


  <artifactId>microservicecloud-provider-dept-8001</artifactId>


  <dependencies>
   <!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->
   <dependency>
     <groupId>com.atguigu.springcloud</groupId>
     <artifactId>microservicecloud-api</artifactId>
     <version>${project.version}</version>
   </dependency>
   <!-- 将微服务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>
   <!-- actuator监控信息完善 -->
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-actuator</artifactId>
   </dependency>   
   <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
   </dependency>
   <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
   </dependency>
   <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid</artifactId>
   </dependency>
   <dependency>
     <groupId>ch.qos.logback</groupId>
     <artifactId>logback-core</artifactId>
   </dependency>
   <dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jetty</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</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>

9.2 总的父工程microservicecloud修改pom.xml添加构建build信息

打开父工程的POM文件。
在这里插入图片描述
在这里插入图片描述
添加如下内容

<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>

来解释一下父工程POM中添加的内容的具体意思。

父工程的名字:

< finalName >microservicecloud< /finalName >

现在容许你访问所有工程下的src/main/resources

     <resource>
       <directory>src/main/resources</directory>
       <filtering>true</filtering>
     </resource>

访问了过滤开启

<filtering>true</filtering>

这个插件就是为了解析解读的

<groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-resources-plugin</artifactId>

$符号结尾的,在resource指定的src/main/resources路径下配置文件信息,我就能够读取。

         <delimiters>
          <delimit>$</delimit>
         </delimiters>

在src/main/resources中放的yml
在yml中放入info信息
我们要添加一些信息,那么我怎么知道是你添加的呢?
以$开头 这个结尾

9.3 修改microservicecloud-provider-dept-8001

打开8001的YML文件内容:
在这里插入图片描述
添加内容

info:
  app.name: atguigu-microservicecloud
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$
 
 

完整内容:

server:
  port: 8001
  
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml  #mybatis所在路径
  type-aliases-package: com.atguigu.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
    url: jdbc:mysql://localhost:3306/cloudDB01
    username: root
    password: 123456
    dbcp2:
      min-idle: 5
      initial-size: 5
      max-total: 5
      max-wait-millis: 200
      
eureka:
  client: #客户端注册进eureka服务列表内
    service-url: 
      defaultZone: http://localhost:7001/eureka
  instance:
    instance-id: microservicecloud-dept8001   #自定义服务名称信息
    prefer-ip-address: true     #访问路径可以显示IP地址
      
info:
  app.name: atguigu-microservicecloud
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$
      
      
      

9.4 再次运行测试效果

启动8001,7001工程。
再次访问Localhost:7001
在这里插入图片描述
点击超级链接: 显示内容成功
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值