美文网首页
三、spring cloud eureka(实战演练)

三、spring cloud eureka(实战演练)

作者: 小manong | 来源:发表于2019-03-24 23:16 被阅读0次

在开始试验之前,先回顾下eureka的架构:


eureka架构图

一、eureka入门案例(hello world)

eureka入门案例架构图

1、创建父工程

父工程项目结构图
父工程pom文件配置:
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

2、创建eureka server(注册中心)

(1)pom文件配置为

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

(2)创建注册中心启动项

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

(3)配置注册中心

server.port=8081

#设置当前实例的主机名称
eureka.instance.hostname=localhost
#是否将应用实例注册到eureka server上,默认是true
eureka.client.register-with-eureka=false
#检索服务(默认是true)
eureka.client.fetch-registry=false
#指定服务注册中心地址,类型为 HashMap,并设置有一组默认值,默认的Key为 defaultZone;
# 默认的Value为 http://localhost:8761/eureka ,如果服务注册中心为高可用集群时,多个注册中心地址以逗号分隔。
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
#当eureka server启动的时候,不能从对等节点获取instance注册信息的情况,应等待多长时间。
eureka.server.wait-time-in-ms-when-sync-empty=0
#启动注册中心的自保护机制(默认是true)
eureka.server.enable-self-preservation=false

3、创建eureka client(服务提供者)

(1)pom文件

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

(2)创建服务提供者启动项及controller 访问url

@SpringBootApplication
@EnableEurekaClient
public class EurakeClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurakeClientApplication.class, args);
    }
}
@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello() {
        return "hello world";
    }
}

(3)配置

server.port=8082
spring.application.name=eurake-client-demo1
#向注册中心注册
eureka.client.service-url.defaultZone=http://localhost:8081/eureka/

4、创建eureka consumer(服务消费者)

(1)pom配置

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

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

(2)创建启动类及配置ribbon复杂均衡

@SpringBootApplication
@EnableDiscoveryClient
public class RibbonConsumer1Application {
    public static void main(String[] args) {
        SpringApplication.run(RibbonConsumer1Application.class, args);
    }
}
@Configuration
public class RibbonLoadBalancedConfig {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

(3)进行服务消费

@RestController
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;
    @RequestMapping("/consumer")
    public String helloConsumer() {
        ResponseEntity<String> forEntity = 
//EURAKE-CLIENT-DEMO1必须大写,需要和服务提供者的实例一样且大写
restTemplate.getForEntity("http://EURAKE-CLIENT-DEMO1/hello", String.class);
        return forEntity.getBody();
    }
}

(4)配置

server.port=8083
spring.application.name=ribbon-consumer
#向注册中心注册并获取相关的服务提供者信息
eureka.client.service-url.defaultZone=http://localhost:8081/eureka/

5、进行试验

  • 先开启注册中心server,看eureka监控界面(此时没有服务)
  • 然后开启client服务提供者,看eureka监控界面(此时有一个client的服务)
  • 然后开启consumer服务消费者,看eureka监控界面(此时有两个服务了)
  • 访问http://localhost:8083/consumer(可以看到服务消费者成功消费服务提供者的服务)

二、eureka集群案例

集群案例结构图
  • 集群案例只是在单个实例案例上复制一份注册中心和服务提供者,主要是对配置文件的修改,现在比如有两个注册中心和两个服务提供者,配置可以为:

针对服务注册中心端
peer1(端口8084):
//指向注册中心2
eureka.client.service-url.defaultZone=http://localhost:8085/eureka/
peer2(端口8085):
//指向注册中心1
eureka.client.service-url.defaultZone=http://localhost:8084/eureka/
针对客户端(服务提供者)
producer1和producer2一样,都需要配置两个注册中心的地址:
eureka.client.service-url.defaultZone=http://localhost:8084/eureka/http://localhost:8085/eureka/

三、实现服务提供者动态扩容

  • 需要结合配置中心来实现,在配置中心中配置相关的项,然后重新刷新配置中心实现扩容。

四、一个region下面的多zone集群

  • 例如我们只有一个region,而一个region有两个zone(zone1,zone2),每一个zone都有两个server。可以配置如(其他类似):
server.port=8761
spring.application.name=eureka-server

eureka.instance.hostname=localhost
eureka.instance.ip-address=true
eureka.instance.metadata-map.zone=zone1

eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.region=region-east
eureka.client.service-url.zone1=http://localhost:8671/eureka/,http://localhost:8672/eureka/
eureka.client.service-url.zone2=http://localhost:8673/eureka/,http://localhost:8674/eureka/
eureka.client.availability-zones.region-east=zone1,zone2

eureka.server.wait-time-in-ms-when-sync-empty=0
eureka.server.enable-self-preservation=false

五、多region下面的多zone集群

  • 可以参考书籍《从新定义springcloud》

六、开启http basic和https

  • 可以参考书籍《从新定义springcloud》

七、使用开源的eureka管理工具

相关文章

网友评论

      本文标题:三、spring cloud eureka(实战演练)

      本文链接:https://www.haomeiwen.com/subject/kjgevqtx.html