1、继之前的eureka服务中心项目添加依赖
<!-- 停止springboot项目 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 安全验证 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2、修改application.yml配置
server: port: 8761 spring: profiles: eureka-colony1 eureka: instance: hostname: eureka-colony1 client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://admin:admin@192.168.1.108:8762/eureka/ server: # 关闭自我保护 enable-self-preservation: false # 清理服务器 eviction-interval-timer-in-ms: 60000 # 日志输出 logging: file: log-tms-eureka1/logger.log level: org.springframework.web: DEBUG # 关闭程序 endpoints: shutdown: enabled: true sensitive: true # 关闭项目的安全验证 security: user: name: admin password: admin role: SUPERUSER # 配置关闭程序的路径 management: context-path: /tms-eureka security: roles: SUPERUSER #角色 port: 8761 # 指定端口 address: 192.168.1.108 # 指定地址 --- server: port: 8762 spring: profiles: eureka-colony2 eureka: instance: hostname: eureka-colony2 client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://admin:admin@192.168.1.108:8761/eureka/ server: # 关闭自我保护 enable-self-preservation: false # 清理服务器 eviction-interval-timer-in-ms: 60000 # 日志输出 logging: file: log-tms-eureka2/logger.log level: org.springframework.web: DEBUG # 关闭程序 endpoints: shutdown: enabled: true sensitive: true # 关闭项目的安全验证 security: user: name: admin password: admin role: SUPERUSER # 配置关闭程序的路径 management: context-path: /tms-eureka security: roles: SUPERUSER #角色 port: 8762 # 指定端口 address: 192.168.1.108 # 指定地址
3、修改启动类
@SpringBootApplication @EnableEurekaServer public class TmsEurekaApplication { public static void main(String[] args) { SpringApplication.run(TmsEurekaApplication.class, args); } }
4、服务提供者与服务客户端要修改application.yml,修改注册链接,带上用户名密码
# eureka注册中心使用用户名密码,所以这里要带上用户名密码向eureka注册 defaultZone: http://admin:admin@192.168.1.108:8761/eureka/, http://admin:admin@192.168.1.108:8762/eureka/
5、测试,把项目打包为jar文件,本例使用虚拟机linux系统来跑eureka服务注册中心,将jar文件上传linux服务器。
启动2次项目:
nohup java -jar /opt/springcloud/tms-eureka/tms-eureka.jar --spring.profiles.active=eureka-colony1 > eureka-colony1.out &
nohup java -jar /opt/springcloud/tms-eureka/tms-eureka.jar --spring.profiles.active=eureka-colony2 > eureka-colony2.out &
再启动2个服务提供者,已在eureka注册中心注册
停止eureka服务注册中心:
curl -u admin:admin -X POST http://192.168.1.108:8761/tms-eureka/shutdown
curl -u admin:admin -X POST http://192.168.1.108:8762/tms-eureka/shutdown