SpringCloud Eureka升级到springboot2.0x Springboot和Springcloud版本的统一

springboot 1.0x 升级到springboot 2.0x 部分依赖做了改变:

springboot 2.0x + springcloud Finchley.RELEASE
springboot 1.5.10.Release  -- Edgware.SR2 springboot2.0.1.Release -- Finchley.RELEASE
spring-cloud-starter-eureka-client spring-cloud-starter-netflix-eureka-client
spring-cloud-starter-eureka-server spring-cloud-starter-netflix-eureka-server
spring-cloud-starter-hystrix spring-cloud-starter-netflix-hystrix
spring-cloud-starter-hystrix-dashboard spring-cloud-starter-netflix-hystrix-dashboard
spring-cloud-starter-ribbon spring-cloud-starter-netflix-ribbon
spring-cloud-starter-turbine spring-cloud-starter-netflix-turbine
spring-cloud-starter-zuul  spring-cloud-starter-netflix-zuul
spring-cloud-starter-feign spring-cloud-starter-openfeign

目录

Eureka服务

1、相应的eureka的pom.xml

2、相应的application.yml

3、主类的注解

客户端client

1、相应的client的pom.xml

2、相应的application.xml

3、主类的注解


Eureka服务

1、相应的eureka的pom.xml

<?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.smileLsf</groupId>
    <artifactId>eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.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>
        <!--添加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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--添加cloud的版本依赖-->
    <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>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、相应的application.yml

eureka:
   client:
      serviceUrl:
          defaultZone: http://localhost:8761/eureka/
      register-with-eureka: false
   server:
     enable-self-preservation: false #显示客户端连接状态
spring:
   application:
      name: spring-cloud-eureka

server:
   port: 8761

3、主类的注解


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

客户端client

1、相应的client的pom.xml

此处注意feign的改为openFeign接口

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <order-common.version>0.0.1-SNAPSHOT</order-common.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>
            <version>1.4.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.smilelsf</groupId>
            <artifactId>order-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!--jpa-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--json转换-->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>
        <!--应用间通信-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--config配置中心-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <!--spring-bus 操作消息队列-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
            <version>1.3.3.RELEASE</version>
        </dependency>
    </dependencies>


    <!--添加cloud的版本依赖-->
    <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>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

2、相应的application.xml

eureka:
   client:
      serviceUrl:
         defaultZone: http://localhost:8761/eureka/
spring:
   application:
      name: order
   datasource:
     driver-class-name: com.mysql.jdbc.Driver
     username: root
     password: 123456
     url: jdbc:mysql://127.0.0.1:3306/springcloud_sell?characterEncoding=utf-8&useSSL=false 
   rabbitmq:
     host: 192.168.1.105
     port: 5672
     username: admin
     password: admin

management:
  security:
    enabled: false  #忽略权限拦截
env:
  dev
port:
  9001

3、主类的注解

注意:@EnableDiscoveryClient 换成了该注解,进行客户端注册


@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients(basePackages = "com.smilelsf.product.client")
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_35275233/article/details/89113133

相关文章