现在网上大部分都是springcloud 1.5左右,很少有2.0往上版本的,到2018/11/05 最新版本为2.1.1
从eureka 搭建到config 然后验证config 动态刷新,然后到新建项目,然后到搭建hystir dash项目,然后到断路器实验
首先 eureka没有什么问题,之后我会把搭建好的项目放到网上,要的请评论 留下邮箱
1.eureka 没什么可说的
2.config 问题比较多,
1.贴代码
<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.springcloud.tech.base</groupId>
<artifactId>base</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>base-confignew</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
package com.springcloud.tech.base.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
然后还有application.yml bootstrap.yml 加载顺序 bootstap.yml 优先于 config ,config优先于application.yml
我在里面设置了rabbitmq,想用这个实现config自动更新变量
然后我还在applicaion.yml 中配置了
management:
endpoints:
web:
exposure:
include: health, info, refresh, bus-refresh
这是为了之后hystri dash的时候,自动刷新功能准备的。和2.0之前版本的不同哦
然后你可以新建一个项目,从@Value("${test}")
自定义一个变量,然后启动项目,然后改一下config远程的数值从1改为2,
这时候你请求项目还是原来的1,然后需要执行curl -X POST "http://localhost:8888/actuator/refresh"
然后在实验一下就好了(注意controller 要是@restController + @Value())
好了。config也好使了
3.base-hystrixNew 项目的配置,这里面坑就多了,首先按照原来的样子导入pom 写起始类
注意在application.yml 中要加入
endpoints:
restart:
enabled: true
shutdown:
enabled: true
然后启动发现能进入项目首页 但是连接不上报红字
最后根据前辈的文章,我也看了一下源码,最后发现需要设置一个servlet
然后加入以下代码
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
然后测试ok
对了 请注意 2.0之前版本都是/hystrix.stream ,2.0之后是/actuator/hystrix.stream
先到这,我去上传项目,然后有问题,可以评论