springcloud-eureka集群-整合security安全验证框架

1、继之前的eureka服务中心项目添加依赖



   org.springframework.boot
   spring-boot-starter-actuator




   org.springframework.boot
   spring-boot-starter-security

2、修改application.yml配置

server:
  port: 8761
spring:
  profiles: eureka-colony1
eureka:
  instance:
    hostname: eureka-colony1
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://admin:[email protected]:8762/eureka/
  server:
    # 关闭自我保护
    enable-self-preservation: false
    # 清理服务器
    eviction-interval-timer-in-ms: 5000
# 日志输出
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.20.211 # 指定地址
  
---

server:
  port: 8762
spring:
  profiles: eureka-colony2
eureka:
  instance:
    hostname: eureka-colony2
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://admin:[email protected]:8761/eureka/
  server:
    # 关闭自我保护
    enable-self-preservation: false
    # 清理服务器
    eviction-interval-timer-in-ms: 5000
# 日志输出
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.20.211 # 指定地址

3、修改启动类

public static void main(String[] args) {
	new SpringApplicationBuilder(EurekaColonyApplication.class).run(args);
}

4、服务提供者与服务客户端要修改application.yml,修改注册链接,带上用户名密码

# eureka注册中心使用用户名密码,所以这里要带上用户名密码向eureka注册
defaultZone: http://admin:[email protected]:8761/eureka/, http://admin:[email protected]:8762/eureka/

5、测试,把项目打包为jar文件,本例使用虚拟机linux系统来跑eureka服务注册中心,将jar文件上传linux服务器。

在pom.xml 中添加


	>
	tms-eureka
	
		
			org.springframework.boot
			spring-boot-maven-plugin
			
				true
				
				com.csdn.EurekaColonyApplication
			
			
				
					
						repackage
					
				
			
		
	

启动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 &

springcloud-eureka集群-整合security安全验证框架_第1张图片

再启动2个服务提供者,已在eureka注册中心注册

springcloud-eureka集群-整合security安全验证框架_第2张图片

停止eureka服务注册中心:

curl -u admin:admin -X POST http://192.168.20.211:8761/tms-eureka/shutdown

curl -u admin:admin -X POST http://192.168.20.211:8762/tms-eureka/shutdown

springcloud-eureka集群-整合security安全验证框架_第3张图片

 

你可能感兴趣的:(Java)