springCloud Euraka (HA && docker)
生活随笔
收集整理的這篇文章主要介紹了
springCloud Euraka (HA && docker)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
由于spingcloud 版本更新比較快,此處重新整理一版:
版本: Java 8
spring boot <version> 2.1.15.RELEASE </version>
<spring-cloud.version>Greenwich.SR6</spring-cloud.version>
1.Euraka 的使用
1.1服務(wù)端: 概述: Eureka server ,健康檢測, 安全認(rèn)證
依賴:
<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-security</artifactId>
</dependency>
<!--健康檢測 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
主類:
@EnableEurekaServer
@SpringBootApplication
public class EurekaServceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServceApplication.class, args);
}
}
配置文件: application.yml
server:
port: 8761
#需要導(dǎo)入spring-boot-starter-security
#為Euraka server 添加一個認(rèn)證,當(dāng)訪問localhost:8761會要求驗(yàn)證
management:
endpoint:
health: #健康檢測 查看 http://localhost:8761/actuator/health
show-details: always
spring:
application:
name: micro-discovery-eureka
security: #安全配置
basic:
enabled: true
user:
name: root
password: root
eureka:
client:
service-url:
#erueka server的地址,/eureka
defaultZone: http://root:root@127.0.0.1:8761/eureka
# 是否從eureka server注冊,這里我們選擇false
fetch-registry: false
# 是否從eureka server 中拉取服務(wù)
register-with-eureka: false
添加配置類: springcloud升級到2.x后Eureka安全配置與1.x有部分變動,新版本的security默認(rèn)開啟csrf了,這里我們先關(guān)掉它,否則Eureka 服務(wù)端注冊不上
新建一個配置類:
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//關(guān)閉csrf
http.csrf().disable();
//開啟認(rèn)證
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
高可用配置:
spring:
application:
name: microservice-eureka-server
---
server:
port: 8761 #指定端口
spring:
profiles: peer1 #指定profile=peer1
#配置eureka的信息
eureka:
instance:
hostname: peer1 #指定當(dāng)profile=peer1時,主機(jī)的名字是peer1
client:
service-url:
defaultZone: http://peer2:8762/eureka,http://peer3:8763/eureka #將自己注冊到peer2這個Eureka服務(wù)上
---
server:
port: 8762 #指定端口
spring:
profiles: peer2 #指定profile=peer2
#配置eureka的信息
eureka:
instance:
hostname: peer2 #指定當(dāng)profile=peer2時,主機(jī)的名字是peer2
client:
service-url:
defaultZone: http://peer1:8761/eureka,http://peer3:8763/eureka #將自己注冊到peer1和peer2的Eureka服務(wù)上
---
server:
port: 8763 #指定端口
spring:
profiles: peer3 #指定profile=peer3
#配置eureka的信息
eureka:
instance:
hostname: peer3 #指定當(dāng)profile=peer3時,主機(jī)的名字是peer3
client:
service-url:
defaultZone: http://peer2:8762/eureka,http://peer1:8761/eureka #將自己注冊到peer2這個Eureka服務(wù)上
注意:多個注冊地址之間使用","隔開,一定是英語的逗號,而且不能有空格
java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1 java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2 java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer3
到這時候,三臺的集群也搭建成功了,再多的也不用說了吧,就是以此類推的。
Docker 版本
Dockerfile:
FROM frolvlad/alpine-oraclejre8:slim
VOLUME /tmp
ADD eureka-server-1.0.0.jar app.jar
ENV AP_ENV=$AP_ENV
VOLUME /tmp/logs/
EXPOSE 8761
# ENTRYPOINT [ "sh", "-c", "java -Denv=$AP_ENV -Xmx64m -Xss256k -jar app.jar --apollo.meta=${CONFIG_SERVERS} --eureka.client.serviceUrl.defaultZone=${EK_CLUSTER_URL}" ]
ENTRYPOINT ["sh", "docker-entrypoint.sh"]
docker-entrypoint.sh
# Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #!/bin/sh if [ x"$AP_ENV" = x ] then echo "AP_ENV IS NULL , USE DEFAULT DEV AS DEFAULT !" echo "dev=$AP_ENV" else echo "AP_ENV IS $AP_ENV !" fi if [ x"$AGENT_SERVICE_NAME" = x ] then echo "No MS Trace Agent $AGENT_COLLECTOR_ADDRESS Setting, @@ NOT USE MS TRACE !" java -Denv=$AP_ENV -jar /opt/app.jar --apollo.meta=$CONFIG_SERVERS else echo " USE MS TRACE Agent to start the service !" java -Denv=$AP_ENV -javaagent:/usr/local/skyagent/skywalking-agent.jar=agent.service_name=$AGENT_SERVICE_NAME,collector.backend_service=$AGENT_COLLECTOR_ADDRESS $JAVA_OPTS -jar /opt/app.jar --apollo.meta=$CONFIG_SERVERS fi
客戶端:
依賴:
<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-actuator</artifactId>
</dependency>
配置文件:application.yml
server:
port: 9001
spring:
application:
name: micro-clent1_user
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health: #健康檢測 查看 http://localhost:8761/actuator/health
show-details: always
eureka:
client:
service-url:
defaultZone: http://root:root@127.0.0.1:8761/eureka/
instance:
# 是否顯示ip,如果不設(shè)置那么就會顯示主機(jī)名,默認(rèn)false
prefer-ip-address: true
主類:
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaServceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServceApplication.class, args);
}
}
登錄 http://localhost:8761
總結(jié)
以上是生活随笔為你收集整理的springCloud Euraka (HA && docker)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SAP Analytics Cloud和
- 下一篇: SAP Analytics Cloud里