您好,登錄后才能下訂單哦!
【前面的話】SpringCloud為開發人員提供了快速構建分布式系統的一些工具,包括配置管理、服務發現、斷路器、路由、微代理、事件總線、全局鎖、決策競選、分布式會話等等。它配置簡單,上手快,而且生態成熟,便于應用。但是它對SpringBoot有很強的依賴,需要有一定基礎,但是SpringBoot倆小時就可以入門。另外對于“微服務架構” 不了解的話,可以通過搜索引擎搜索“微服務架構”了解下。另外這是SpringCloud的版本為Greenwich.SR2,JDK版本為1.8,SpringBoot的版本為2.1.7.RELEASE。
添加pom依賴和SpringCloud和SpringBoot的版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.eelve.lovincloud</groupId>
<artifactId>lovincloud</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>lovincloud</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
在這里,我們需要用的的組件上Spring Cloud Netflix的Eureka ,eureka是一個服務注冊和發現模塊。
新建一個子工程lovin-eureka-server作為服務的注冊中心
<parent>
<artifactId>lovincloud</artifactId>
<groupId>com.eelve.lovincloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lovin-eureka-server</artifactId>
<packaging>jar</packaging>
<name>eurekaserver</name>
<version>0.0.1</version>
<description>eureka服務端</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</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>
package com.eelve.lovin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
- eureka是一個高可用的組件,它沒有后端緩存,每一個實例注冊之后需要向注冊中心發送心跳(因此可以在內存中完成),在默認情況下erureka server也是一個eureka client ,必須要指定一個 server。eureka server的配置文件appication.yml:
~~~yaml
spring:
application:
naem: lovineurkaserver # 服務模塊名稱
server:
port: 8881 # 設置的eureka端口號
eureka:
instance:
hostname: localhost # 設置eureka的主機地址
client:
registerWithEureka: false #表示是否將自己注冊到Eureka Server,默認為true。由于當前應用就是Eureka Server,故而設置為false
fetchRegistry: false #表示是否從Eureka Server獲取注冊信息,默認為true。因為這是一個單點的Eureka Server,不需要同步其他的Eureka Server節點的數據,故而設置為false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #Eureka server地址,查詢服務和注冊服務都需要依賴這個地址,多個地址可用逗號(英文的)分割
新建一個子工程lovin-eureka-server作為服務的注冊中心
<parent>
<artifactId>lovincloud</artifactId>
<groupId>com.eelve.lovincloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lovin-eureka-client</artifactId>
<packaging>jar</packaging>
<name>eurekaclient</name>
<version>0.0.1</version>
<description>eureka的一個消費端</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
package com.eelve.lovin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
- 然后我們需要連接到服務端,具體配置如下
~~~yaml
server:
port: 8801 # 服務端口號
spring:
application:
name: lovineurkaclient # 服務名稱
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8881/eureka/ # 注冊到的eureka服務地址
package com.eelve.lovin.controller;
import com.eelve.lovin.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
@Version 1.0**/
@RestController
br/>**/
@RestController
@Autowired
ServerConfig serverConfig;
@RequestMapping("hello")
public String hello(){
return serverConfig.getUrl()+"###"+ HelloController.class.getName();
}
}
# 肆、分別啟動注冊中心的服務端和客戶端
訪問localhost:8881查看結果
![注冊中心](https://cache.yisu.com/upload/information/20200312/76/254765.jpg)
到這里我們可以已經看到已經成功將客戶端注冊到服務端了,然后我們訪問測試接口
![192202](https://cache.yisu.com/upload/information/20200312/76/254766.jpg)
可以看到已經訪問成功,至此Eureka的搭建已經完成。
# 伍、加入安全配置
在互聯網中我們一般都會考慮安全性,尤其是管理服務的注冊中心,所以我們可以用**spring-boot-starter-security**來做安全限制
- 給**lovin-eureka-server**添加**spring-boot-starter-security**的pom依賴
~~~pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
spring:
application:
naem: lovineurkaserver # 服務模塊名稱
security:
basic:
enabled: true
user:
name: lovin
password: ${REGISTRY_SERVER_PASSWORD:lovin}
server:
port: 8881 # 設置的eureka端口號
eureka:
instance:
hostname: localhost # 設置eureka的主機地址
metadata-map:
user.name: ${security.user.name}
user.password: ${security.user.password}
client:
registerWithEureka: false #表示是否將自己注冊到Eureka Server,默認為true。由于當前應用就是Eureka Server,故而設置為false
fetchRegistry: false #表示是否從Eureka Server獲取注冊信息,默認為true。因為這是一個單點的Eureka Server,不需要同步其他的Eureka Server節點的數據,故而設置為false
serviceUrl:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/ #Eureka server地址,查詢服務和注冊服務都需要依賴這個地址,多個地址可用逗號(英文的)分割
package com.eelve.lovin.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
/**
@Version 1.0**/
@EnableWebSecurity
br/>**/
@EnableWebSecurity
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/css/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.httpBasic();
super.configure(http);
}
}
- 給**lovin-eureka-client**添加**spring-boot-starter-security**的pom依賴
~~~pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
server:
port: 8801 # 服務端口號
spring:
application:
name: lovineurkaclient # 服務名稱
security:
basic:
enabled: true
user:
name: lovin
password: ${REGISTRY_SERVER_PASSWORD:lovin}
eureka:
client:
serviceUrl:
defaultZone: http://lovin:lovin@localhost:8881/eureka/ # 注冊到的eureka服務地址
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
metadata-map:
user.name: lovin
user.password: lovin
package com.eelve.lovin.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
/**
@Version 1.0**/
@Configuration
br/>**/
@Configuration
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
- 另外為了測試多客服端注冊,我們可以修改再給客戶端新建一個配置文件,然后開啟IDEA的多節點運行,如下圖所示勾選**Allow parallel run**
![192203](https://cache.yisu.com/upload/information/20200312/76/254767.jpg)
- 然后為了區分是哪個節點的請求我們可以添加獲取端口
~~~java
package com.eelve.lovin.config;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
@Version 1.0**/
@Component
br/>**/
@Component
private int serverPort;
public String getUrl() {
InetAddress address = null;
try {
address = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return "http://"+address.getHostAddress() +":"+this.serverPort;
}
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
this.serverPort = event.getWebServer().getPort();
}
}
- 然后我們一次重啟服務端和兩個客戶端,這個時候我們訪問http://localhost:8881/
![192205](https://cache.yisu.com/upload/information/20200312/76/254769.jpg)
可以看到,這里已經讓我們輸入用戶名和密碼了,說明**spring-boot-starter-security**已經配置成功,這時我們輸入配置的用戶名:lovin和密碼:lovin
![192204](https://cache.yisu.com/upload/information/20200312/76/254772.jpg)
這里我們可以看到已經成功了,那么到這里Eureka的配置已經全部成功了。
* [最后的最后是本博客的源碼,歡迎關注這一套SpringCloud的實踐](https://github.com/lovinstudio/lovincloud)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。