服务治理Spring Cloud Eureka使用

1.Spring Cloud Eureka的作用

(1)建立Eureka服务端(也称为注册中心)

我使用的springboot的版本是2.0.5.RELEASE版本

① 添加依赖

在书中引入的是spring-cloud-eureka-server,但是当我引入这个依赖的时候,找不到@EnableEurekeServer

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

<dependencyManagement>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-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>

② 配置application-peer1.properties和application-peer2.properties文件

以下是application-peer1.properties的配置:

server.port=8001
spring.application.name=eureka-server

#eureka
eureka.instance.hostname=127.0.0.1
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://127.0.0.1:8002/eureka/
eureka.instance.prefer-ip-address=true

以下是application-peer2.properties的配置

server.port=8002
spring.application.name=eureka-server

#eureka
eureka.instance.hostname=127.0.0.1
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://127.0.0.1:8001/eureka/
eureka.instance.prefer-ip-address=true

③ 建立启动文件,添加@EnableEurekaServer注解

@ComponentScan(basePackages = "gdut.ff")
@EnableAutoConfiguration
@EnableEurekaServer
public class Main {
    public static void main(String args[]) {
        SpringApplication.run(Main.class, args);
    }
}

④ 启动,指定使用的配置文件

eureka-server-peer1
eureka-server-peer2

以上就建立了一个注册中心集群。

⑤ 演示效果

图片描述
图片描述

(2)建立Eureka客户端(也称为服务提供方)

① 添加依赖

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

<dependencyManagement>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-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>

② 配置application.properties

server.port=9001
spring.application.name=hello-service
eureka.client.service-url.defaultZone=http://127.0.0.1:8001/eureka/,http://127.0.0.1:8002/eureka/
eureka.instance.prefer-ip-address=true

③ 提供服务

@RestController
@RequestMapping("/hello")
public class HelloController {

    private final Logger logger = LoggerFactory.getLogger(HelloController.class);

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index() {
        String serviceId = "hello-service";
        List<ServiceInstance> instance = discoveryClient.getInstances(serviceId);
        if(null != instance) {
            for(int i = 0;i < instance.size();i++) {
                System.out.println(instance.get(i).getHost() + ":" + instance.get(i).getPort());
            }
        }
        return "Hello,world";
    }
}

④ 建立启动文件,添加EnableDiscoveryClient注解

@ComponentScan(basePackages = "gdut.ff")
@EnableAutoConfiguration
@EnableDiscoveryClient
public class Main {
    public static void main(String args[]) {
        SpringApplication.run(Main.class, args);
    }
}

⑤ 启动,指定使用的端口

启动Main文件时,指定--server.port=xxx

访问http://ip:port/hello/index

(3)建立ribbon-consumer(也称为服务调用方)

① 添加依赖

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

<dependencyManagement>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-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>

② 配置application.properties

server.port=7001
spring.application.name=ribbon-consumer
eureka.client.service-url.defaultZone=http://127.0.0.1:8001/eureka/,http://127.0.0.1:8002/eureka/
eureka.instance.prefer-ip-address=true

③ 获取服务

@RestController
public class ConsumerController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "/ribbon-consumer", method= RequestMethod.GET)
    public String helloConsumer() {
        return restTemplate.getForEntity("http://hello-service/hello/index",String.class).getBody();
    }
}

④ 建立启动文件,添加EnableDiscoveryClient注解

@ComponentScan(basePackages = "gdut.ff")
@EnableAutoConfiguration
@EnableDiscoveryClient
public class Main {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

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

⑤ 启动

访问http://ip:port/ribbon-consumery

示例代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值