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

示例代码


bluesnail95
69 声望6 粉丝

Java开发工程师