IDEA 部署spring Cloud

Spring cloud Eureka

Eureka Server,注册中心

Eureka Client,所有要进行注册的微服务通过Eureka Client 连接到 Eureka Server ,完后注册

一.创建父工程,pom.xml

<modelVersion>4.0.0</modelVersion>

<groupId>com.southwind</groupId>
<artifactId>aispringcloud</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eurekaserver</module>
<module>eurekaclient</module>
</modules>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--解决 jdk 9 以上没有 jaxb api 的问题-->
<!--<dependency>-->
<!--<groupId>javax.xml.bind</groupId>-->
<!--<artifactId>jaxb-api</artifactId>-->
<!--<version>2.3.0</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>com.sun.xml.bind</groupId>-->
<!--<artifactId>jaxb-impl</artifactId>-->
<!--<version>2.3.0</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>com.sun.xml.bind</groupId>-->
<!--<artifactId>jaxb-core</artifactId>-->
<!--<version>2.3.0</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>javax.activation</groupId>-->
<!--<artifactId>activation</artifactId>-->
<!--<version>1.1.1</version>-->
<!--</dependency>-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>

</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.然后在父类里面创建两个子工程

在第一个子项目中创建配置文件 application.yml 添加EUREKA Server相关配置

#端口号
server:
port: 7070
#是否在注册中心把自己当成一个微服务注册
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:9090/eureka/
属性说明:
server.por:当前Eureka Server 服务端口
eureka.client.register-with-eureka:是否将当前的Eureka Server 服务作为客户端进行注册
eureka.client.fetch-fegistry:是否获取其他Eureka Server 服务的数据
eureka.client.service-url.defaultzone:注册中心访问地址
性对应的pom.xml的配置:
<parent>
<artifactId>aispringcloud</artifactId>
<groupId>com.southwind</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>eurekaserver</artifactId>
<!--注册中心-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
.创建启动类
@SpringBootApplication     //声明该类是springboot的入口
@EnableEurekaServer //声明该类少一个Eureka Server微服务,提供注册和发现功能(注册中心
public class EurekaServerApplication {
public static void main(String[] args){
SpringApplication.run(EurekaServerApplication.class,args);
}
}
####### Eureka Client代码的实现
1.创建 Module,pom.xml
第二个子项目中 pom.xml 的配置 yml 配置 启动类配置
<parent>
<artifactId>aispringcloud</artifactId>
<groupId>com.southwind</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>eurekaclient</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>

yml的配置:
server:
port: 9090
# 当前服务器注册在Eureka Server上面的名字
spring:
application:
name: provider
# 注册中心访问的地址
eureka:
client:
service-url:
defaultZone: http://localhost:9090/eureka/
# 是否将当前服务的ip注册到 Eureka Server
instance:
prefer-ip-address: true

实体类:
package com.ssouthwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Created by 86182 on 2019/7/29.
*/
@SpringBootApplication
public aspect ProviderAppliaction {
public static void main(String[] args){
SpringApplication.run(ProviderAppliaction.class,args);
}
}
 

猜你喜欢

转载自www.cnblogs.com/shxkey/p/11263189.html