前言
因爲剛開始用微服務沒多久,看網上很多博客都是將配置中心作爲一個服務,然後將配置中心的服務在註冊中心進行註冊使用,個人感覺有點多餘,不僅增加了維護成本,而且多啓一個服務也就也就意味這個又需要多開闢新的內存資源。所以打算將配置中心和註冊中心作爲一箇中心服務。使用的時候沒有注意到其實阿里已經開源過一個組件了,叫做Nacos,它就是整合配置中心和註冊中心,將其作爲一體服務。如果不想使用spring的eureka和config組件可以使用阿里的這款開源組件。
這裏主要是來分享一下針對eureka和config組件的實現方案。
整合出現的主要問題
整合後主要出現了eureka註冊中心wro.css wro.js 404。
並且client端的服務無法進行正常的註冊。
實現方案
server端配置
pom主要依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
<!-- spring cloud config 服務端包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
啓動類註解配置:同時添加兩個server註解
package com.clf.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@EnableConfigServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
yml的配置:主要是在config的配置處添加prefix前綴
server:
port: 8761
spring:
application:
name: eureka-server
security:
user:
name: clf
password: clf123
roles: SUPERUSER
cloud:
config:
server:
git:
uri: https://gitee.com/kvenclf/cloud-config.git # 你個人的配置中心倉庫
search-paths: repo1 # 路由文件搜索路徑
username: "git的用戶名"
password: "git的密碼"
prefix: /config # 自定義的過濾前綴,避免在內嵌服務時對全部的url進行git的轉發,這裏就是解決配置中心的css和js找不到的問題
label: master
eureka:
instance:
hostname: localhost
prefer-ip-address: true
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@localhost:8761/eureka/
instance:
prefer-ip-address: true #當猜測主機名時,服務器的IP地址應該在操作系統報告的主機名中使用
security的放行:
package com.clf.eurekaserver.conf;
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;
/**
* @author : clf
* @description : SpringSecurity配置
**/
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
/**
* 對eureka註冊的URL不進行CSRF防禦
* @param http
* @throws Exception
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}
client端配置
pom依賴:
<!-- spring cloud config 客戶端包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
啓動類配置:添加@EnableDiscoveryClient註解
package com.clf.userserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class UserServerApplication {
public static void main(String[] args) {
SpringApplication.run(UserServerApplication.class, args);
}
}
yml配置:
server:
port: 8201
spring:
application:
name: user-server
cloud:
config:
uri: http://clf:clf123@localhost:8761/config/ #這就是對應的config服務的路徑,必須加上配置的config前綴,否則無法獲取配置
profile: pro
label: master
eureka:
client:
service-url:
defaultZone: http://clf:clf123@localhost:8761/eureka/ # 註冊中心