1.服务端读取gitee上的配置信息
在之前项目producer2基础上修改
(1)pom.xml加入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
(2)启动类加注解@EnableConfigServer
(3)在gitee.com创建仓库和文件
每个配置文件中都写一个属性springcloud.hello,属性值分别是 hello dev update/test/pro
比如springcloud-config-dev.properties写法如下
(4)applicaton.yml修改如下
cloud:
config:
server:
git:
uri: https://gitee.com/li-weiwen/my-test.git
search-paths: chapter6/springcloud-config
username: ****
password: ****
//报错
If you are using the git profile, you need to set a Git URI in your configur
//解决方案:检查application.yml是否值那儿漏了空格
//比如
//错误写法
uri:https://gitee.com/li-weiwen/my-test.git
//正确写法
uri: https://gitee.com/li-weiwen/my-test.git
(5)启动项目,访问http://localhost:8088/springcloud-config/pro
或者http://localhost:8088/springcloud-config-dev.properties
2.客户端读取服务器配置信息
修改consumer2项目
(1)在和application.yml同级目录下创建bootstrap.properties
注意:spring.cloud.config.name上的必须和gitee上名字一样,比如说
springcloud-config-dev.properties在springcloud-config目录下,则spring.cloud.config.name的值就应该是springcloud-config
spring.cloud.config.name= springcloud-config
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8088/
spring.cloud.config.label=master
(2)引入依赖包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
(3)修改HelloController类
@Autowired
HelloRemote helloRemote;
//注意:从gitee上读取到springcloud.hello=hello dev update Second
@Value("${springcloud.hello}")
private String readgitee;
@RequestMapping("/hello/{name}")
public String index(@PathVariable("name") String name) {
return helloRemote.hello(name)+this.readgitee;
}
(4)访问http://localhost:8028/hello/spring
3.高并发Spring Cloud Config
(1)consumers2项目helloController类上方加RefreshScope
(2)consumers2项目bootstrap.properties修改成
spring.application.name=spring-cloud-consumers
server.port=8028
spring.cloud.config.name=springcloud-config
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=spring-cloud-producer
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
(3)输入网址:
http://localhost:8085/hello/spring