config server
在config-server项目基础上修改
- 添加依赖
<dependencies>
<!-- config server依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- eureka-client依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
- 修改application.yml
server:
port: 8888
spring:
application:
name: config-server
cloud:
config:
server:
git:
#git仓库地址
uri: https://github.com/dean4lee/springcloud-demo
#仓库地址下的路径,多个用,隔开
search-paths: conf
#用户名
username:
#密码
password:
eureka:
client:
#是否将自己注册到Eureka服务中
register-with-eureka: true
#是否从Eureka服务中获取注册信息
fetch-registry: false
#Eureka注册中心的地址,多个注册中心用,隔开
serviceUrl:
defaultZone: http://localhost:8761/eureka/
- 修改启动类
@SpringBootApplication
//将config-server服务注册到注册中心
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServer2Application {
public static void main(String[] args) {
SpringApplication.run(ConfigServer2Application.class, args);
}
}
先启动eureka-server服务,再启动config-server服务
config client
在config-client项目基础上修改
- 添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
- 修改bootstrap.yml
spring:
cloud:
config:
#配置服务器地址
#uri: http://localhost:8888
#对应{application}
name: hello
#对应{profile}
profile: dev
#git分支
label: master
#如果有密码验证
username:
password:
discovery:
#开启config配置中心发现
enabled: true
#config配置中心的service-id,即服务名
service-id: config-server
- 修改application.yml
server:
port: 8880
spring:
application:
name: config-client
eureka:
client:
#是否将自己注册到Eureka服务中
register-with-eureka: true
#是否从Eureka服务中获取注册信息
fetch-registry: true
#Eureka注册中心的地址,多个注册中心用,隔开
serviceUrl:
defaultZone: http://localhost:8761/eureka/
- 修改启动类
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClient2Application {
public static void main(String[] args) {
SpringApplication.run(ConfigClient2Application.class, args);
}
}
- 测试
浏览器访问http://localhost:8880/getUsername
admin
作者公众号