SpringCloud 学习(2):第一个eureka程序

1.介绍:Eureka是一个服务管理器,其他业务组件(服务)通过rest注册到Eureka服务器,由服务器对服务进行监控,管理。
2.原理:

Eureka服务器:统一管理服务
Eureka客户端(服务提供者):将服务注册到Eureka服务器,获取注册列表
Eureka客户端(服务调用者):同样将服务注册到Eureka服务器,并且获取注册列表,再去调用具体的某个服务。

如果还不理解,可以举个例子:

小明现在想报警,又不知道报警电话是多少,但是知道咨询平台,于是小明先向咨询平台咨询,知道了报警电话之后再去拨打报警电话。。在这里,我们可以把咨询平台当成Eureka服务器,警察局当成服务提供者,小明当成服务调用者。首先,警察局会先把报警电话提供给到咨询平台,当小明去咨询平台查询时会把警局电话给小明,小明再去拨打电话。

3.第一个Eureka程序(下面我们模拟上面报警的例子,写一个Eureka程序)
  • Eureka服务器(建立项目consultingPlatform_server):
    1).pom.xml
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.13.RELEASE</version>
    </parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
2).application.properties
server.port=6001

#不把自己作为客户端注册
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
#eureka服务器路径
eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka/
3).Application.java(启动类)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

 
@SpringBootApplication
@EnableEurekaServer
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }
}
  • 服务提供者(创建项目police_client)
1).pom.xml
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.13.RELEASE</version>
    </parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>
2 ).application.properties
server.port=6002

#服务名称
spring.application.name=police
#注册路径
eureka.client.service-url.defaultZone=http://localhost:6001/eureka/
3 ).Application.java
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @author: lqh
 * @description:
 * @program: police_client
 * @create: 2018-06-06 11:50
 **/
@SpringBootApplication
@EnableEurekaClient
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }
}
4 ).PoliceController.java
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: lqh
 * @description:
 * @program: police_client
 * @create: 2018-06-06 11:59
 **/
@RestController
public class PoliceController {
    @RequestMapping("call")
    public String call(){
        return "call the police";
    }
}


  • 服务调用者(创建项目people_caller):
  • 1).pom.xml
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.13.RELEASE</version>
        </parent>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Edgware.SR3</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            </dependency>
        </dependencies>
    2.application.properties
    server.port=6003
    
    #服务名称
    spring.application.name=people
    #注册路径
    eureka.client.service-url.defaultZone=http://localhost:6001/eureka/
    
    
    3.Application
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @SpringBootApplication
    @EnableEurekaClient
    public class Application {
        public static void main(String[] args) {
            new SpringApplicationBuilder(Application.class).web(true).run(args);
        }
    }
    

    4.PeopleController
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
     
    @Configuration
    @RestController
    public class PeopleController {
    
        @Bean
        @LoadBalanced
        RestTemplate getRestTemplate() {
            return new RestTemplate();
        }
    
        @RequestMapping("call")
        public String call() {
            return getRestTemplate().getForObject("http://police/call",String.class);
        }
    }

    OK,跑起来。。




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值