美文网首页
springboot配置Eureka

springboot配置Eureka

作者: employeeeee | 来源:发表于2019-05-17 15:52 被阅读0次

    需要解决分布式事务的问题 准备搭建一个服务的注册中心

    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">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.emp</groupId>
        <artifactId>example</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>example</name>
        <description>Spring cloud Registry</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</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>
    
    

    然后只需要在启动类中 添加注释@EnableEurekaServer
    这样就把当前的服务 作为注册中心了
    然后再通过application.yml进行 配置一些简单的配置

    server:
      port: 8761
    
    spring:
      application:
        name: registry
      security:
        user:
          name: zhou
          password: 12345678
    
    eureka:
      client:
        register-with-eureka: false
        fetch-registry: false
        serviceUrl:
          defaultZone: http://localhost:${server.port}/eureka/
    

    需要注意的是 springboot2.x之后 好像不支持security.user.name 这样方式来进行自定义了
    需要使用
    spring.security.user.name这样的方式来进行初始化了
    然后启动项目

    Eureka登录界面
    • 使用初始化的用户名和密码登陆
    首页

    然后配置Client 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">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.emp</groupId>
        <artifactId>proxy</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>proxy</name>
        <description>Spring cloud proxy</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-autoconfigure</artifactId>
                <version>RELEASE</version>
                <scope>compile</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>
    
    

    然后启动类 添加@EnableZuulProxy,@EnableEurekaClient
    配置文件如下

    server:
      port: 8888
    
    spring:
      application:
        name: proxy
    
    eureka:
      client:
        serviceUrl:
          defaultZone: http://zhou:12345678@localhost:8761/eureka/
    

    然后启动 出现报错

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

    现在找不到server 但是项目正在启动 配置文件 也没问题
    发现是security 在springboot2.x之后 默认为csrf检验
    在server端给关闭就可以了
    新建一个config配置文件

    @Configuration
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity httpSecurity) throws Exception{
            httpSecurity.csrf().disable();
            super.configure(httpSecurity);
        }
    }
    

    重新启动

    首页

    相关文章

      网友评论

          本文标题:springboot配置Eureka

          本文链接:https://www.haomeiwen.com/subject/ogjoaqtx.html