eureka注册中心pom配置
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Finchley.M7
pom
import
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone
false
application.yml
###服务端口号
server:
port: 8100
##定义服务名称
spring:
application:
name: app-hbk-eureka
eureka:
instance:
###注册中心ip地址
hostname: 127.0.0.1
client:
serviceUrl:
##注册地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
####因为自己是注册中心,是否需要将自己注册给自己的注册中心(集群的时候是需要是为true)
register-with-eureka: false
###因为自己是注册中心, 不需要去检索服务信息
fetch-registry: false
# 测试时关闭自我保护机制,保证不可用服务及时踢出
server:
# 测试时关闭自我保护机制,保证不可用服务及时踢出
enable-self-preservation: false
### ##剔除失效服务间隔
#eviction-interval-timer-in-ms: 2000
启动类
@SpringBootApplication
@EnableEurekaServer
public class AppEureka8100 {
// @EnableEurekaServer 表示开启EurekaServer服务 开启注册中心
public static void main(String[] args) {
SpringApplication.run(AppEureka8100.class, args);
}
}
pom文件
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Finchley.M7
pom
import
org.springframework.cloud
spring-cloud-config-server
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone
false
application.yml,其中git为我码云搭建的公开仓库
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
spring:
application:
####注册中心应用名称
name: config-server
cloud:
config:
server:
git:
###git环境地址
uri: https://gitee.com/huang_baokang_daxian/microservices.git
####搜索目录
search-paths:
- gkconfig
####读取分支
label: master
####端口号
server:
port: 8888
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
pom文件,增加spring-boot-starter-actuator依赖
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Finchley.M7
pom
import
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-config-client
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-actuator
spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone
false
bootstrap.yml
spring:
application:
####注册中心应用名称
name: config-client
cloud:
config:
####读取后缀
profile: dev
####读取config-server注册地址
discovery:
service-id: config-server
enabled: true
##### eureka服务注册地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
server:
port: 8882
## 开启所有端点
management:
endpoints:
web:
exposure:
include: "*"
启动类,增加@EnableEurekaClient注解
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class AppClient {
public static void main(String[] args) {
SpringApplication.run(AppClient.class, args);
}
}
测试控制器类,需要增加@RefreshScope注解,注意需要设置proxyMode,否则单单一个@RefreshScope会获取name为null,
参考文章 https://blog.csdn.net/Tomsidi/article/details/107208459?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param
https://blog.csdn.net/soulsda/article/details/89642241
@RestController
@RefreshScope(proxyMode = ScopedProxyMode.DEFAULT)
public class IndexController {
@Value("${name}")
private String name;
@RequestMapping("/name")
private String name() {
return name;
}
}
码云配置文件的命名需要满足一定的规范
应用名称-profile.properties
浏览器直接访问配置文件地址,服务端跑的是8888端口
http://127.0.0.1:8888/config-client-dev.properties
使用客户端调用访问配置name
http://localhost:8882/name
如果修改码云配置文件,不想重启,则使用手动刷新,可以使用postman发送post请求http://127.0.0.1:8882/actuator/refresh
该地址路由可以在项目启动时看到。