Springboot 整合 SpringCloud组件-Config 配置中心 ConfigServer (六)

这篇我们来整合Config组件,就是专门用于读取配置文件的组件,这篇博文将教大家怎么将项目与github打通。

不多说,我们开始整合, 创建一个springboot项目,起名config-server:

pom.xml:

 (springcloud我使用的是Finchley.RELEASE 版本,跟之前的教程博文保持一致版本)

<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.cloud</groupId>
	<artifactId>config-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>config-server</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
			<version>2.0.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<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>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

然后是application.yml:


eureka:
  instance:
#以IP地址注册到服务中心,相互注册使用IP地址
    preferIpAddress: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8888
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/JCcccT/TestConfig.git
          searchPaths: testInfo
        default-label: master
  application:
    name: config-server

这里,我需要给初学者特意讲下, config-server

1.在配置文件里面是将这个配置中心也注册到了Eureka注册中心里去了,这个并不是强制的,也可以不注册到EurekaServer的

2.git的uri,怎么获取的呢,如下图:

3.searchPaths 这个是搜查具体配置文件的路径,因为我的配置文件 名为:client-test-dev.properties,而这个文件放在了文件夹testInfo里;

4.这个配置文件起名字也是有讲究的,

一共支持以下几种方式:

  1. /{application}/{profile}[/{label}]
  2. /{application}-{profile}.yml
  3. /{label}/{application}-{profile}.yml
  4. /{application}-{profile}.properties
  5. /{label}/{application}-{profile}.properties

而我采用的是第2种方式(client-test-dev.properties):

  • label 分支名称 如:master dev ,不写就是master。
  • application 配置文件名称(client-test 后面我们在写config client服务的时候,项目名就是取 client-test)
  • profiles 环境名称,不可省略,假如我们的仓库中配置文件命名没有环境名称,可以profile可以写为-a (dev)

 

 我的文件里面的内容写了一个key和值:

最后在项目启动类加上注解:

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;

@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

}

 

OK,Config Server到此 已经整合完毕了,我们将服务跑起来,访问下:http://localhost:8888/client-test-dev.properties :

可以看到返回值就是我们配置文件里面的值,证明 服务实例已经和github成功打通, 那么下篇https://blog.csdn.net/qq_35387940/article/details/94619086

我们来实现Config Client(这里的Config Client其实也就是一个微服务),通过连接 Config Server去读取配置文件中的值。 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Security OAuth2 Authorization Server 是一个基于 Spring Security 的 OAuth2 认证服务器,用于管理 OAuth2 模式下的授权和令牌。 要将 Spring BootSpring Security OAuth2 Authorization Server 集成,可以遵循以下步骤: 1. 添加依赖 在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-oauth2-authorization-server</artifactId> <version>0.2.1</version> </dependency> ``` 2. 配置认证服务器 创建一个配置类,用于配置 OAuth2 认证服务器。这个类需要继承 AuthorizationServerConfigurerAdapter 类,并且实现 configure 方法。 ```java @Configuration public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { // 配置客户端信息 clients.inMemory() .withClient("client") .secret("{noop}secret") .authorizedGrantTypes("authorization_code", "refresh_token") .redirectUris("http://localhost:8080/client") .scopes("read", "write"); } @Override public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { // 配置安全性 security.checkTokenAccess("isAuthenticated()"); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { // 配置端点 endpoints.authenticationManager(authenticationManager); } } ``` 上面的代码中,我们配置了一个名为 "client" 的客户端,使用了授权码模式和刷新令牌模式。授权成功后,将重定向到 "http://localhost:8080/client" 页面。 3. 配置 Spring Security 为了使 OAuth2 认证服务器正常工作,需要配置 Spring Security。可以创建一个配置类,用于配置 Spring Security。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { // 配置 HTTP 安全性 http.authorizeRequests() .antMatchers("/oauth2/authorize").authenticated() .and().formLogin().and().csrf().disable(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { // 配置身份认证管理器 auth.inMemoryAuthentication() .withUser("user").password("{noop}password").roles("USER"); } } ``` 在上面的代码中,我们配置了 HTTP 安全性和身份认证管理器。只有经过身份认证的用户才能访问 "/oauth2/authorize" 端点。 4. 启动应用程序 现在可以启动应用程序,并访问 "http://localhost:8080/oauth2/authorize?response_type=code&client_id=client&redirect_uri=http://localhost:8080/client" 来进行授权。授权成功后,将重定向到 "http://localhost:8080/client" 页面。 以上就是整合 Spring BootSpring Security OAuth2 Authorization Server 的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小目标青年

对你有帮助的话,谢谢你的打赏。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值