Spring Cloud微服务集成配置中心

2021-07-20 19:05

阅读:738

标签:str   start   Delve   span   string   apache   ase   动手   文件   


 

1. 搭建Spring Cloud Config配置中心(见上一篇博客)

 

2. 创建微服务项目bounter-simon-app,pom文件如下:

xml version="1.0" encoding="UTF-8"?>
project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    modelVersion>4.0.0modelVersion>

    groupId>cn.bountergroupId>
    artifactId>bounter-simon-appartifactId>
    version>0.0.1-SNAPSHOTversion>
    packaging>jarpackaging>

    name>bounter-simon-appname>

    parent>
        groupId>org.springframework.bootgroupId>
        artifactId>spring-boot-starter-parentartifactId>
        version>2.0.4.RELEASEversion>
    parent>

    properties>
        java.version>1.8java.version>
        spring-cloud.version>Finchley.SR1spring-cloud.version>
    properties>

    dependencies>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-webartifactId>
        dependency>

dependency> groupId>org.springframework.cloudgroupId> artifactId>spring-cloud-starter-configartifactId> dependency> dependencies> dependencyManagement> dependencies> dependency> groupId>org.springframework.cloudgroupId> artifactId>spring-cloud-dependenciesartifactId> version>${spring-cloud.version}version> type>pomtype> scope>importscope> dependency> dependencies> dependencyManagement> build> plugins> plugin> groupId>org.springframework.bootgroupId> artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> project>

 

3. 配置从配置中心获取配置信息,application.properties文件如下:

server.port=8081

# 配置中心仓库的配置文件名
spring.application.name=simon
# 配置中心服务器地址
spring.cloud.config.uri=http://localhost:8888

 

4.控制器返回从配置中心获取到的配置信息,SimonController.java 内容如下:

@RestController
@CrossOrigin
@RequestMapping("/api/simon")
public class SimonController {

    @Value("${name}")
    private String name;
    
    @GetMapping("/name")
    public ResponseData> get() {
        return new ResponseData().success().data(name);
    }

}

 

5. 启动bounter-simon-app服务,浏览器访问:http://localhost:8081/api/simon/name,页面如下:

{
  success: true,
  data: "simon",
  errorMsg: null,
  timestamp: 1534917807,
  sign: null
}

 

data里就显示了从配置中心获取到的name属性值,是不是觉得很简单啊,那就自己动手试一试吧!

源代码Github地址:

https://github.com/13babybear/bounter-simon-app

https://github.com/13babybear/bounter-susan-app

Spring Cloud微服务集成配置中心

标签:str   start   Delve   span   string   apache   ase   动手   文件   

原文地址:https://www.cnblogs.com/gdufs/p/9517216.html


评论


亲,登录后才可以留言!