springcloud系列 (二) 服务治理和服务发现 集成组合Eureka

6 篇文章 0 订阅
1 篇文章 0 订阅
  1. 创建maven父项目,创建maven子项目,添加springcloud的eurea依赖,配置和启动注解。
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
     <version>2.2.3.RELEASE</version>
 </dependency>
 
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR7</version>
            <type>pom</type>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

添加server配置:

spring:
  application:
    name: server
server:
  port: 7001
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:7001/eureka/

添加user或product模块配置:

spring:
  application:
    name: product
server:
  port: 9002
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/

添加启动注解:
server子项目需要在启动类添加:@EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class LearnServerApplication {

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

}

具体业务子项目(user或product)启动类不需要添加注解(旧版本SpringCloud需要在启动类添加@EnableDiscoveryClient注解)。

@SpringBootApplication
//@EnableDiscoveryClient(旧版本springcloud需要添加此注解)
public class LearnProductApplication {

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

}
  1. 启动测试,访问localhost:7001,成功
  2. 打jar包,添加插件:
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
  1. 确定包名:
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
    <finalName>learn-product</finalName>
</build>
  1. maven----lifecycle–clean;package;如果报错,检查springcloud版本与springboot版本是否匹配。具体匹配实例见项目:https://gitee.com/manongfaner/cloud-maven-learn
  2. 如果匹配,可以打出jar包,然后运行java -jar learn-server.jar 即可运行项目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值