3)配置中心

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010475041/article/details/79590846

3)配置中心

  • spring boot 1.5.10-RELEASE
  • spring cloud Edgware.SR2

代码分支:https://github.com/grissomsh/cas/tree/1.x-config

实现目标:
1. 搭建spring cloud config server(本地配置,端口为8888)
2. sso-server从配置中心读取配置文件(修改端口为8081)

a. 搭建配置中心

https://start.spring.io/ 选择1.5.10-RELEASE版本,输入config server选择下载

pom.xml

<?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.0</modelVersion>
    <groupId>com.kawhii.auth</groupId>
    <artifactId>sso-config</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <name>CAS Config</name>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR2</spring-cloud.version>
    </properties>

    <!--为了加快速度,修改成国内的代理-->
    <repositories>
        <repository>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </repository>
    </repositories>
</project>

application.yml

修改端口为8888,本地配置

server:
  port: 8888
spring:
  profiles:
    active:
      - native

SsoConfigApplication.java

启动允许为配置中心

package com.kawhii.auth.ssoconfig;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class SsoConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(SsoConfigApplication.class, args);
    }
}

b. 新增sso-server-dev配置文件

在目录resources/config下新增sso-server-dev.properties

server.port=8081

为何这么命名?

规则如下:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

结论:
单点登录服务名称需要为sso-server,profile为dev即可读取以上文件

c. 测试

访问 http://localhost:8888/sso-server/dev

8081字眼则视为正确

响应报文如下:

{
  "name": "sso-server",
  "profiles": Array[1][
    "dev"
  ],
  "label": null,
  "version": null,
  "state": null,
  "propertySources": Array[1][
    {
      "name": "classpath:/config/sso-server-dev.properties",
      "source": {
        "server.port": "8081"
      }
    }
  ]
}

d. 配置sso-server启用配置中心

新建bootstrap.properties

# 服务名称
spring.application.name=sso-server
#profile
spring.profiles.active=dev

#配置中心服务
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.enabled=true

e. 打包启动测试

先启动配置中心,再启动sso-server

启动sso-server有以下字眼则视为成功

[org.springframework.cloud.config.client.ConfigServicePropertySourceLocator] - <Fetching config from server at: http://localhost:8888>
2018-03-17 13:53:34,951 INFO [org.springframework.cloud.config.client.ConfigServicePropertySourceLocator] - <Located environment: name=sso-server, profiles=[dev], label=null, version=null, state=null>
2018-03-17 13:53:34,952 INFO [org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration] - <Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource {name='classpath:/config/sso-server-dev.properties'}]]>

http://localhost:8081/login 生效
http://localhost:8080/login 失效

sso-server 最终代码

配置中心最终代码

猜你喜欢

转载自blog.csdn.net/u010475041/article/details/79590846
今日推荐