StringCloud之快速上手Eureka

2019-04-15  本文已影响0人  fanbuer

Eureka服务端开发

1.创建eureka模块
2.引入依赖 父工程pom.xml定义SpringCloud版本

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.M9</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement>

tensquare_eureka模块pom.xml引入eureka-server

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
</dependencies>

3.添加application.yml

server:
    port: 6868 #服务端口 
eureka:
    client:
        registerWithEureka: false #是否将自己注册到Eureka服务中,本身就是所有无需 注册 
        fetchRegistry: false #是否从Eureka中获取注册信息 
        serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址 
            defaultZone: http://127.0.0.1:${server.port}/eureka/

4.编写启动类

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

5.测试
启动运行启动类,然后在浏览器地址栏输入 http://localhost:6868/ 运行效果如 下:

image.png
主界面中
system status为系统信息
General Info为一般信息
Instances currently registered with Eureka为注册到的所有微服务列表

服务注册

1.将其他微服务模块添加依赖

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

2.修改每个微服务的application.yml,添加注册eureka服务的配置

eureka:
    client:
        service‐url:
            defaultZone: http://localhost:6868/eureka 
    instance:
        prefer‐ip‐address: true

3.修改每个服务类的启动类,添加注解

@SpringBootApplication
@EnableEurekaClient//必须的
public class UserApplication {

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

4.启动测试

保护模式

上一篇下一篇

猜你喜欢

热点阅读