前言:在上一章节中我们把服务提供者做成了集群,而本章节则是一些关于服务信息的配置,这部分知识对集群整体影响不大,不过最好还是掌握,毕竟万一有用到的地方呢
有时候我们想要修改服务的名称,让服务名称按照我们提供的名称来显示,这时候就可以通过修改配置来实现修改服务名称
例:
provider-payment8001
- #服务端口号
- server:
- port: 8001
-
- #服务名称
- spring:
- application:
- name: cloud-payment-service
- datasource:
- type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型
- driver-class-name: com.mysql.cj.jdbc.Driver #mysql驱动包(mysql驱动包版本是5的要写成com.mysql.jdbc.Driver)
- url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8 #mysql连接路径,记得把cloud这个数据库名改成自己的,或者新建名为cloud的数据库
- username: root
- password: 123456
-
- eureka:
- client:
- #表示是否将自己注册进Eureka Server里,默认为true
- register-with-eureka: true
- #是否从Eureka Server抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
- fetch-registry: true
- service-url:
- #defaultZone: http://localhost:7001/eureka
- defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
- instance:
- instance-id: payment8001
-
- #mybatis配置
- mybatis:
- mapper-locations: classpath:mapper/*.xml #扫描类路径下的mapper文件夹下所有的.xml配置文件
- type-aliases-package: com.ken.springcloud.entities #该包下的所有Entity类都取默认别名
效果图:

provider-payment8002
- #服务端口号
- server:
- port: 8002
-
- #服务名称
- spring:
- application:
- name: cloud-payment-service
- datasource:
- type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型
- driver-class-name: com.mysql.cj.jdbc.Driver #mysql驱动包(mysql驱动包版本是5的要写成com.mysql.jdbc.Driver)
- url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8 #mysql连接路径,记得把cloud这个数据库名改成自己的,或者新建名为cloud的数据库
- username: root
- password: 123456
-
- eureka:
- client:
- #表示是否将自己注册进Eureka Server里,默认为true
- register-with-eureka: true
- #是否从Eureka Server抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
- fetch-registry: true
- service-url:
- #defaultZone: http://localhost:7001/eureka
- defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
- instance:
- instance-id: payment8002
-
- #mybatis配置
- mybatis:
- mapper-locations: classpath:mapper/*.xml #扫描类路径下的mapper文件夹下所有的.xml配置文件
- type-aliases-package: com.ken.springcloud.entities #该包下的所有Entity类都取默认别名
效果图:

重新启动provider-payment8001模块和provider-payment8001模块,然后分别进入http://eureka7001.com:7001/和http://eureka7002.com:7002/查看效果,如果服务名称按照我们配置的来显示,则证明配置生效,服务名修改成功
(注意:在这两个模块启动前要先启动eureka-server7001模块和eureka-server7002模块)
效果图:
eureka7001

eureka7002

不知道大家有没有发现当光标移到服务名称时左下角会出现一个IP地址,而这个IP地址显示是电脑名称加上端口号,这对于我们来说是不方便的,因为不能直观的了解这个服务所在的IP,如果服务出现问题,需要排错,那这时候也没办法快速的定位服务所在服务器的IP地址,这时候就可以通过修改配置可以实现IP地址的显示

例:
provider-payment8001
- #服务端口号
- server:
- port: 8001
-
- #服务名称
- spring:
- application:
- name: cloud-payment-service
- datasource:
- type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型
- driver-class-name: com.mysql.cj.jdbc.Driver #mysql驱动包(mysql驱动包版本是5的要写成com.mysql.jdbc.Driver)
- url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8 #mysql连接路径,记得把cloud这个数据库名改成自己的,或者新建名为cloud的数据库
- username: root
- password: 123456
-
- eureka:
- client:
- #表示是否将自己注册进Eureka Server里,默认为true
- register-with-eureka: true
- #是否从Eureka Server抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
- fetch-registry: true
- service-url:
- #defaultZone: http://localhost:7001/eureka
- defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
- instance:
- instance-id: payment8001
- #访问路径可以显示IP地址
- prefer-ip-address: true
-
- #mybatis配置
- mybatis:
- mapper-locations: classpath:mapper/*.xml #扫描类路径下的mapper文件夹下所有的.xml配置文件
- type-aliases-package: com.ken.springcloud.entities #该包下的所有Entity类都取默认别名
效果图:

provider-payment8002
- #服务端口号
- server:
- port: 8002
-
- #服务名称
- spring:
- application:
- name: cloud-payment-service
- datasource:
- type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型
- driver-class-name: com.mysql.cj.jdbc.Driver #mysql驱动包(mysql驱动包版本是5的要写成com.mysql.jdbc.Driver)
- url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8 #mysql连接路径,记得把cloud这个数据库名改成自己的,或者新建名为cloud的数据库
- username: root
- password: 123456
-
- eureka:
- client:
- #表示是否将自己注册进Eureka Server里,默认为true
- register-with-eureka: true
- #是否从Eureka Server抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
- fetch-registry: true
- service-url:
- #defaultZone: http://localhost:7001/eureka
- defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
- instance:
- instance-id: payment8002
- #访问路径可以显示IP地址
- prefer-ip-address: true
-
- #mybatis配置
- mybatis:
- mapper-locations: classpath:mapper/*.xml #扫描类路径下的mapper文件夹下所有的.xml配置文件
- type-aliases-package: com.ken.springcloud.entities #该包下的所有Entity类都取默认别名
效果图:

重新启动provider-payment8001模块和provider-payment8001模块,然后分别进入http://eureka7001.com:7001/和http://eureka7002.com:7002/查看效果,如果光标移到到服务名称上时,左下角显示出来的是真实IP,则证明配置生效,访问路径可以显示IP地址
(注意:在这两个模块启动前要先启动eureka-server7001模块和eureka-server7002模块)
效果图:
eureka7001


eureka7002


注意:其实服务名称的修改和IP地址的显示需要依赖以下这两个依赖,如果没有引入这两个依赖,那可能会导致配置失效
