spring clould(一)服务注册与发现(Eureka)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lh87270202/article/details/83989062

前言

Spring Cloud是一个基于Spring Boot实现的云应用开发工具。Spring cloud包含了很多子项目,用于解决我们服务开发中需要对面的问题,比如服务集群、服务发现、断路器、智能路由。 本次开发项目中是用Spring Cloud Eureka实现在服务治理。

1、服务治理

Spring cloud提供了多个服力治理框架,比如:Netflix Eureka、Consul、Zookeeper。

2、Spring Cloud Eureka实现服务治理

Spring Cloud Eureka是Spring Cloud Netflix项目下的服务治理模块
(1)创建服务注册中心
创建一个spring boot 项目eureka-service,在Pom.xml中加入如下依赖

<parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.0.2.RELEASE</version>
     <relativePath/>
</parent>
 <dependencies>
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka-server</artifactId>
     </dependency>
 </dependencies>
 <dependencyManagement>
     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-dependencies</artifactId>
             <version>Finchley.RELEASE</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
     </dependencies>
 </dependencyManagement>

(2)启动方法

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

(3)application.propertis配置

spring.application.name=eureka-server
server.port=80791
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

启动工程后,访问:http://localhost:80791/,可以看到注册服务页面。
(4)创建服务提供方
创建一个spring boot 项目eureka-client,在Pom.xml中加入如下依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</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>Finchley.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

(5)启动方法

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

(6)application.properties配置

spring.application.name=eureka-client
server.port=80792
##eureka注册中心的地址
eureka.client.serviceUrl.defaultZone=http://localhost:80791/eureka/

猜你喜欢

转载自blog.csdn.net/lh87270202/article/details/83989062