日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

springCloud Euraka (HA && docker)

發布時間:2023/12/19 综合教程 32 生活家
生活随笔 收集整理的這篇文章主要介紹了 springCloud Euraka (HA && docker) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

由于spingcloud 版本更新比較快,此處重新整理一版:

版本: Java 8

   spring boot <version> 2.1.15.RELEASE </version>

   <spring-cloud.version>Greenwich.SR6</spring-cloud.version>

1.Euraka 的使用

1.1服務端: 概述: Eureka server ,健康檢測, 安全認證

依賴:

        <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
#需要導入spring-boot-starter-security
#為Euraka server 添加一個認證,當訪問localhost:8761會要求驗證
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 中拉取服務
    register-with-eureka: false

添加配置類: springcloud升級到2.x后Eureka安全配置與1.x有部分變動,新版本的security默認開啟csrf了,這里我們先關掉它,否則Eureka 服務端注冊不上

新建一個配置類:

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //關閉csrf
        http.csrf().disable();
        //開啟認證
         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   #指定當profile=peer1時,主機的名字是peer1
  client:
    service-url:
      defaultZone: http://peer2:8762/eureka,http://peer3:8763/eureka  #將自己注冊到peer2這個Eureka服務上
 
---
server:
  port: 8762   #指定端口
spring:
  profiles: peer2   #指定profile=peer2
#配置eureka的信息
eureka:
  instance:
    hostname: peer2   #指定當profile=peer2時,主機的名字是peer2
  client:
    service-url:
      defaultZone: http://peer1:8761/eureka,http://peer3:8763/eureka   #將自己注冊到peer1和peer2的Eureka服務上
 
---
server:
  port: 8763   #指定端口
spring:
  profiles: peer3   #指定profile=peer3
#配置eureka的信息
eureka:
  instance:
    hostname: peer3   #指定當profile=peer3時,主機的名字是peer3
  client:
    service-url:
      defaultZone: http://peer2:8762/eureka,http://peer1:8761/eureka   #將自己注冊到peer2這個Eureka服務上

注意:多個注冊地址之間使用","隔開,一定是英語的逗號,而且不能有空格

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,如果不設置那么就會顯示主機名,默認false prefer-ip-address: true

主類:

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaServceApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServceApplication.class, args);
    }
}

登錄 http://localhost:8761

總結

以上是生活随笔為你收集整理的springCloud Euraka (HA &amp;&amp; docker)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。