SpringCloud(finchley版本)——Eureka

1 篇文章 0 订阅
1 篇文章 0 订阅

1、搭建Eureka服务器

新建一个父项目spring-cloud,依赖如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.taikang.bd</groupId>
    <artifactId>spring-cloud</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>eureka-server</module>
        <module>my-demo</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
</project>

在父项目spring-cloud上新建一个模块eureka-server,pom依赖如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud</artifactId>
        <groupId>com.taikang.bd</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server</artifactId>

    <dependencies>
        <!--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>
    </dependencies>

</project>

新建启动类

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

application.yml

server:
  port: 8761
spring:
  application:
    name: eureka-server
  security:
    user:
      name: admin
      password: admin
eureka:
  client:
    serviceUrl:
       defaultZone: http://admin:admin@192.168.11.111:8761/eureka/,http://admin:admin@192.168.11.112:8761/eureka/
    register-with-eureka: false
    fetch-registry: false #作为服务发现和注册,不去检索服务

这样配置完成后,发现client端注册不上,是因为新版的security默认开启csrf了,加上配置关闭即可

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable(); //关闭csrf
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); //开启认证
    }

}
参考 https://blog.csdn.net/weixin_39913200/article/details/80845867

---

eureka作为服务发现注册中心,搭建集群时,服务其实只注册到一个eureka,eureka就会通知其他的eureka,为了高可用,还需都注册一下

 

server:
  port:  
spring:
  application:
    name: 
eureka:
  client:
    serviceUrl:
      defaultZone: 
  instance:
    prefer-ip-address: true #没有这行你休想访问不同电脑的swagger接口
management:
  security:
    enabled: false
# 指定此实例的ip
eureka.instance.ip-address=
# 注册时使用ip而不是主机名
eureka.instance.prefer-ip-address=true
参考https://blog.csdn.net/neosmith/article/details/53126924


#  如果项目配置有 server.servlet.context-path 属性,想要被 spring boot admin 监控,就要配置以下属性
  instance:
    metadata-map:
      management:
        context-path: /gateway/actuator
    health-check-url: http://localhost:${server.port}/gateway/actuator/health
    status-page-url: http://localhost:${server.port}/gateway/actuator/info
    home-page-url: http://localhost:${server.port}/

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值