分享程序网
首页
  • java
微服务
微前端
环境搭建
数据库
设计模式
算法
软件
解决问题
链接
首页
  • java
微服务
微前端
环境搭建
数据库
设计模式
算法
软件
解决问题
链接
  • 微服务

    • 介绍
  • 微服务搭建

    • 初步搭建
  • 服务发现

    • Eureka
    • nacos
  • 网关

    • zuul
    • 网关配置
    • 过滤器
    • 动态加载
  • 认证(Oauth)

    • 初始化项目
    • Oauth2配置
    • 对外接口
  • 通用服务

    • 通用功能父模块
    • redis
  • 任务调度

    • 任务调度服务
    • xxl-job示例
  • 业务服务

    • 业务设计

初始化项目

背景

注册创建成功之后,需要微服务注册上去,所有基于业务的微服务架构离不开认证中心,首先要创建一个认证服务。

创建项目

创建pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.shareprog</groupId>
		<artifactId>shareprog-pom</artifactId>
		<version>1.0.0</version>
	</parent>
	<artifactId>shareprog-auth</artifactId>
	<name>权限认证</name>
	<description>权限统一认证</description>

	<dependencies>
		<!-- eureka 注册中心依赖 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

		<!-- spring boot -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- springcloud-oauth -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-oauth2</artifactId>
		</dependency>
		
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

创建启动类

package com.shareprog.auth;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @ClassName: AuthApplication
 * @Description: 权限认证启动类
 * @author cl
 * @date 2021年1月13日
 */
@SpringBootApplication
@EnableDiscoveryClient
public class AuthApplication {

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

创建配置文件

bootstrapt.yml

server:
  port: 8004
spring:
  application:
    name: shareprog-auth
eureka:
  instance:
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true #注册服务器IP,而不是服务器名称
    health-check-url-path: /actuator/health
    lease-renewal-interval-in-seconds: 10 #心跳值;其设置为小于30的值可以加快使客户端连接到其他服务的过程
    lease-expiration-duration-in-seconds: 30
  client:
    registry-fetch-interval-seconds: 5 #指示从eureka获取注册表信息的频率(秒)
    service-url: 
      defaultZone: http://eureka:eureka@localhost:8001/eureka

application.yml

spring:
  profiles:
    active: dev

application-dev.yml

spring: 
  security:
    user:
      name: auth
      password: auth

这时候一个微服务就创建好了。

Last Updated:
Contributors: clcheng
Next
Oauth2配置