【SpringCloud】Eureka注册中心(集群)、服务者、消费者

本文学习资料来源于蚂蚁课堂。

一、项目结构

项目依赖:只需在根目录的pom.xml引入

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>springboot</groupId>
    <artifactId>springboot</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>
    <modules>
        <module>kk-app</module>
        <module>kk-heros-service</module>
        <module>kk-story-service</module>
        <module>kk-region-service</module>
        <module>kk-heros-service-9100</module>
        <module>kk-heros-service-7100</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <!-- 管理依赖 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.M7</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <!-- 管理依赖 -->
    <dependencies>
        <!--SpringCloud eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <!-- SpringBoot整合Web组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- SpringBoot整合eureka客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
    <!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>


</project>

注册中心:

kk-heros-serive kk-heros-serive-7100 kk-heros-serive-9100

3个注册中心yml配置文件只是,端口不一样,其他都一样,defaultZone为其他两个注册中心的访问地址

# 服务端口号
server:
  port: 8100
#eureka 基本信息配置
eureka:
  instance:
    ###注册到eurekaip地址
    hostname: 127.0.0.1
  client:
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:7100/eureka/,http://${eureka.instance.hostname}:9100/eureka/
    #因为自己是为注册中心,不需要自己注册自己(集群需要为true)
    register-with-eureka: true
    fetch-registry: true


spring:
  application:
    name: kk-heros-server

服务提供者:

kk-story-server
# 服务端口号
server:
  port: 8000
#eureka 基本信息配置
eureka:
  instance:
    ###注册到eurekaip地址
    hostname: 127.0.0.1
  client:
    serviceUrl:
      #注册到eureka的服务地址
      defaultZone: http://${eureka.instance.hostname}:8100/eureka/,http://${eureka.instance.hostname}:9100/eureka/,http://${eureka.instance.hostname}:7100/eureka/
    #需要将我的服务注册到eureka上
    register-with-eureka: true
    #需要检索服务
    fetch-registry: true
spring:
  application:
    name: kk-story-server

服务消费者:

kk-region-server
# 服务端口号
server:
  port: 8001
#eureka 基本信息配置
eureka:
  instance:
    ###注册到eurekaip地址
    hostname: 127.0.0.1
  client:
    serviceUrl:
      #注册到eureka的服务地址
      defaultZone: http://${eureka.instance.hostname}:8100/eureka/,http://${eureka.instance.hostname}:9100/eureka/,http://${eureka.instance.hostname}:7100/eureka/
    #需要将我的服务注册到eureka上
    register-with-eureka: true
    #需要检索服务
    fetch-registry: true
spring:
  application:
    name: kk-region-server

二、代码

注册中心:另两个注册中心一样

package com.google;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @author wk
 * @Description:注册中心
 * @date 2019/11/26 12:00
 **/
@EnableEurekaServer
@SpringBootApplication
public class HerosEureka {
    public static void main(String[] args) {
        SpringApplication.run(HerosEureka.class, args);
    }
}

服务提供者:

package com.google.api.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @author wk
 * @Description:服务提供者
 * @date 2019/11/26 12:00
 **/
@EnableEurekaClient
@SpringBootApplication
public class StoryEureka {
    public static void main(String[] args) {
        SpringApplication.run(StoryEureka.class, args);
    }
}
package com.google.api.controller.service;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wk
 * @Description:
 * @date 2019/11/26 13:55
 **/
@RestController
public class MessageController {

    @RequestMapping(value = "/getMessage")
    public String getMessage() {
        return "盖伦来自德玛西亚";
    }
}

服务消费者:

package com.google.api.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @author wk
 * @Description:消费者
 * @date 2019/11/26 16:46
 **/
@EnableEurekaClient
@SpringBootApplication
public class RegionEureka {

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

    /**
     * @LoadBalanced就能让这个RestTemplate在请求时拥有客户端负载均衡的能力
     */

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
package com.google.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author wk
 * @Description:
 * @date 2019/11/26 16:50
 **/
@RestController
public class RegionController {

    /**
     * restTemplate底层是httpClient
     */

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/getRegionMessage")
    public String getRegionMessage() {
        return restTemplate.getForObject("http://kk-story-server/getMessage", String.class);
    }
}

三、效果演示

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值