SpringCloud搭建專題【註冊中心&認證註冊】

SpringCloud搭建專題①【註冊中心】

這裏講的是eureka,nacos後續會寫,首先需要使用Spring Initializr創建一個SpirngBoot項目(如果是其他ide直接引入相關依賴即可),然後集成一些組件就可以使用了。

步驟1

file->new project,然後填寫項目配置,直接下一步
在這裏插入圖片描述

步驟二

這裏SpirngBoot版本我選擇的是最新的2.2,然後選擇依賴,註冊中心除了基礎的spring-boot-starter-parent依賴之外,只需要一個eureka-sever依賴就可以了,我這裏多加了SpringSecurity權限認證依賴(後面解釋爲什麼)
在這裏插入圖片描述
在這裏插入圖片描述

步驟三

這裏項目就骨架就算搞好了,在做一點簡單的配置,註冊中心就完成了。

首先在啓動類上加上@EnableEurekaServer註冊,證明這是一個eurekaserver
在這裏插入圖片描述
然後在修改一下配置文件,如果不使用security的話到這裏就結束了,直接啓動,打開網頁輸入 http://localhost:8090/ 就可以打開註冊頁面

server:
  port: 8090
spring:
  profiles: dev
  application:
    name: basecloud-server #service-id
  security:  #這裏是可選的,如果服務需要認證之後才能加入註冊中心這裏需要配置
    user:
      name: base #自定義
      password: base #自定義
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false #自己不註冊自己
    fetch-registry: false # 多註冊中心時開啓
    service-url:
        defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka

步驟四

如果使用了security的話需要做一下basic簡單認證的配置

@EnableWebSecurity 
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter{
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}

然後輸入 http://localhost:8090/ 會先彈出認證窗口
在這裏插入圖片描述
輸入在配置文件中配置的用戶名密碼即可進入註冊中心頁面
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章