版本:
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR6</spring-cloud.version> (admin server 不需要)
docker 安装:
spingcloud admin:
sudo docker run -itd --restart unless-stopped --name my_admin -p 9000:9000 --net host -e JAVA_OPTS="-Xmx128m -Xss512k" -e ADMIN_USERNAME=sea -e ADMIN_PASSWORD=sea happysea/micro:admin
依赖:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.1.5</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
主类:
@SpringBootApplication @EnableAdminServer public class AdminServceApplication { public static void main(String[] args) { SpringApplication.run(AdminServceApplication.class, args); } }
添加配置类: springcloud升级到2.x后Eureka安全配置与1.x有部分变动,新版本的security默认开启csrf了,这里我们先关掉它,否则 服务端注册不上
新建一个配置类:
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//关闭csrf
http.csrf().disable();
//开启认证
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
配置文件: application.yml
server: port: 9000 #需要导入spring-boot-starter-security #为admin server 添加一个认证,当访问localhost:8761会要求验证 management: endpoints: web: exposure: include: '*' endpoint: health: show-details: always spring: application: name: micro-admin security: #安全配置 basic: enabled: true user: name: root password: root
##################### 至此 , admin server 搭建完毕 ########################
下面是使用:
依赖:
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.1.5</version> </dependency>
application.yml:
spring: application: name: MICRO-CLENT1-USER boot: admin: client: url: http://root:root@localhost:9000 management: endpoints: web: exposure: include: '*' endpoint: health: #健康检测 查看 http://localhost:8761/actuator/health show-details: always
或者 application.properties
eureka.client.service-url.defaultZone = http://root:root@192.168.25.140:8761/eureka/,http://root:root@192.168.25.141:8761/eureka/ eureka.instance.prefer-ip-address = true spring.boot.admin.client.url = http://192.168.25.143:9000 spring.boot.admin.client.username=root spring.boot.admin.client.password=root spring.boot.admin.client.instance.service-base-url: http://${spring.cloud.client.ip-address}:${server.port} #spring.boot.admin.client.instance.service-base-url = http://192.168.18.129:7089 //自动申明IP management.endpoints.web.exposure.include =* management.endpoint.health.show-details= ALWAYS
check:
http://127.0.0.1:9000/#/applications