config-server-eureka project
1. File-->new spring project
2.add dependency
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> <version>2.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> spring-cloud-starter-netflix-eureka-client </artifactId> <version>2.1.1.RELEASE</version> </dependency>
3.Edit application.yml
server: port: 8000 #spring cloud config native spring: profiles: active: native application: name: config-server-eureka cloud: config: server: native: search-locations: /home/smile/workspace-sts/config-repo #spring cloud config git #spring: # application: # name: config-server-eureka # cloud: # config: # server: # git: # uri: http://localhost:3380/root/smile.git # search-paths: config-repo # username: root # password: root123456 eureka: client: service-url: defaultZone: http://localhost:8761/eureka/
4.program
package com.smile; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient @EnableConfigServer public class ConfigServerEurekaApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerEurekaApplication.class, args); } }
5.Run
visit : http://localhost:8000/smile/config-dev
config-client-eureka
1. File-->new spring project
2.add dependency
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
spring-boot-starter-actuator refresh 需要用到
3.Edit application.yml
bootstrap.yml
server: port: 8003 spring: application: name: config-client-eureka cloud: config: # uri: http://localhost:8000/ discovery: enabled: true service-id: config-server-eureka name: smile profile: config-dev # dev 开发环境配置文件 | test 测试环境 | pro 正式环境 smile-config-dev.properties {name}-{profile}.properties eureka: client: # registerWithEureka: false # fetchRegistry: false serviceUrl: defaultZone: http://localhost:8761/eureka/
application.yml
management: # server: # port: 8003 endpoint: refresh: enabled: true endpoints: web: exposure: include: - info - health - refresh
4.program
ConfigClientEurekaApplication
package com.smile; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient @RefreshScope public class ConfigClientEurekaApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientEurekaApplication.class, args); } }
ConfigClientController
package com.smile.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RefreshScope public class ConfigClientController { @Value("${name}") String name; @Value("${age}") String age; @RequestMapping("/hello") public String hello() { return "name:"+name+",age:"+age; } }
5.Run
visit: http://localhost:8003/hello/
refresh:
update smile-config-dev.properties age=25--->age=15 save
post http://localhost:8003/actuator/refresh with firefox browser
visit: http://localhost:8003/hello/ age is changed