Eureka配置及问题集

本文详细介绍Eureka服务端和客户端的Maven配置,以及YAML配置文件的设置。涵盖服务端安全配置、客户端注册与发现流程,并解决TransportException错误,通过禁用CSRF检查确保服务正常运行。

Eureka配置及问题集

By rambo

1.maven 服务端:

<?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.hxdl</groupId>
    <artifactId>hxdl-eureka2</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>
	<name>eureka管理模块</name>
    <description>EurekaServer project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.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-security</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-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-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>

</project>      

2.服务端yml配置:

spring:
  application:
    name: eureka-server2
  security:
    user:
      name: eureka
      password: eureka
### 服务器配置
server:
  port: 10004
  servlet:
    context-path: /eureka2
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false #是否向服务注册中心注册自己
    fetchRegistry: false #是否检索服务
    serviceUrl: #服务注册中心的配置内容,指定服务注册中心的位置
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka2/eureka
      

3.maven 客户端:

<?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.hxdl</groupId>
    <artifactId>hxdl-eureka2</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>
	<name>eureka管理模块</name>
    <description>EurekaServer project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.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.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </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>      

4.服务端yml配置:

### 服务器配置
server:
  port: 8081
  servlet:
    context-path: /hxdl
### spring配置
spring:
  application:
    name: hxdl-manage
  main:
    allow-bean-definition-overriding: true #当遇到同样名字的时候,是否允许覆盖注册
## jpa配置
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
    properties:
      hibernate.dialect: com.hxdl.framework.mysql.MySQLDialectUTF8
      hibernate.format_sql: true
      hibernate.use_sql_comments: false #是否显示注释
    open-in-view: true
## eureka配置
eureka:
  instance:
    #instance-id: ${spring.application.name}
    #prefer-ip-address: true
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://eureka:eureka@localhost:10004/eureka2/eureka
    register-with-eureka: true
    fetch-registry: false
      

问题

1.com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

解决:新版的security默认启用了csrf检验,要在eureka服务端那边配置security的csrf检验为false
在server端,编写WebSecurityConfigurer类,再重启server和client

package com.hxdl.manage;

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;

/** 
 * ClassName: WebSecurityConfigurer <br/> 
 * Function: 新版的security默认启用了csrf检验,要在eureka服务端那边配置security的csrf检验为false. <br/> 
 * Reason: TODO ADD REASON(可选). <br/> 
 * date: 2019年7月31日 下午4:06:17 <br/> 
 * 
 * @author bobolnear@163.com
 */  
@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        super.configure(http);
    }
}

记录:

时间作者内容本版
2019-07-31bobolnear创建V1.0

苔花如米小,也学牡丹开。

版权声明:本文为原创文章,转载请附上链接!

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值