spring-cloud 日记
-服务注册发现
@EnableEurekaServer
spring-cloud client
@EnableEurekaClient
spring-cloud config server 配置服务器
@EnableConfigServer //也可以用@EnableEurekaServer 注册到注册服务器上
基本配置
@EnableEurekaServer
@SpringBootApplication
@RestController
@EnableEurekaServer
public class SpringCloudApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudApplication.class, args);
}
}`
yml配置
#eureka服务发现
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
#客户端可用此地址将服务注册到注册服务器上
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
@EnableEurekaClient 客户端配置
@SpringBootApplication
@EnableEurekaClient
@RestController
@RefreshScope
public class SpringCloudConfigServerApplication {
//这里是读取的远程git上的配置文件 在bootstrap.yml中配置好, config server的地址
@Value("${spring.datasource.username}")
String username;
@Value("${spring.datasource.url}")
String url;
@RequestMapping("/hello")
public String home() {
return "username =="+username+"////name="+url;}
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerApplication.class, args);
}
}
yml 配置
#服务注册中心
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring-boot 会先加载bootstrap.yml 文件
这里是通过配置服务器读取git上的配置文件
server:
port: 8762
spring:
application:
name: application-YaoZhiYang
cloud:
config:
#配置服务器的地址
uri: http://localhost:8091/
profile: dev
#git分支
label: master
//关闭security的默认加载 不然refresh时会401错误 这里是临时配置
management:
security:
enabled: false
@EnableConfigServer 配置服务器
配置git项目路径 等配置
server:
port: 8091
spring:
cloud:
config:
server:
git:
uri: http://10.73.1.87/gsaves/springbootStudy.git
#git项目中的路径
search-paths: /src/main/resources
default-label: master
username: yaozhiyang
password: yangzai666
#spring-cloud config提供的 访问格式 profile dev
#/{application}/{profile}[/{label}]
#/{application}-{profile}.yml
#/{label}/{application}-{profile}.yml
#/{application}-{profile}.properties
#/{label}/{application}-{profile}.properties
application:
name: config-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
—–待修改