spring cloud 学习(一) 服务注册与发现

最近开始学习spring cloud,
跟着这个学:http://blog.didispace.com/spring-cloud-starter-dalston-1/

以下是我抄的定义:
Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种简单的开发方式。

Spring Cloud包含了多个子项目(针对分布式系统中涉及的多个不同开源产品),比如:Spring Cloud Config、Spring Cloud Netflix、Spring Cloud0 CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring Cloud Commons、Spring Cloud Zookeeper、Spring Cloud CLI等项目。

一 .服务中心
有两种方法,一种是使用Spring Cloud Eureka,另一种是使用Spring Cloud Consul。
前者保证高可用后者保证强一致性

https://www.zhihu.com/question/55749122/answer/313757969

然后来撘框架吧
创建maven项目 eureka-server
pom.xml

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

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

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

启动类
通过@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话

@EnableEurekaServer
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class)
                    .web(true).run(args);
    }
}

在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,只需要在application.properties配置文件中增加如下信息:

spring:
  application:
    name: eureka-server
server:
  port: 1001

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false

这里写图片描述
注册中心完成,下面来看服务提供者

pom

  <parent> 
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

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

实现/dc请求处理接口,通过DiscoveryClient对象,在日志中打印出服务实例的相关内容。

@RestController
public class DcController {

    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("/dc")
    public String dc() {
        String services = "Services: " + discoveryClient.getServices();
        System.out.println(services);
        return services;
    }

}

启动项
@EnableDiscoveryClient注解,该注解能激活Eureka中的DiscoveryClient实现,这样才能实现Controller中对服务信息的输出。

@EnableDiscoveryClient
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(
                Application.class)
            .web(true).run(args);
    }
}

配置文件

spring:
  application:
    name: eureka-client
server:
  port: 2001

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1001/eureka/

这里写图片描述
上面显示的红字这里还不知道什么原因,会有什么影响
这里先标记下解决方案。
1.https://www.cnblogs.com/breath-taking/articles/7940364.html
2.https://blog.csdn.net/lc0817/article/details/54375802

也可以通过直接访问eureka-client服务提供的/dc接口来获取当前的服务清单,只需要访问:http://localhost:2001/dc,我们可以得到如下输出返回:
这里写图片描述

下面来实现Spring Cloud Consul
先去官网安装服务中心

https://www.consul.io/downloads.html

启动

consul agent -dev

在浏览器打开http://localhost:8500/ui/#/dc1/services
这里写图片描述
修改eureka的pom

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>

修改配置文件

spring:
  application:
    name: consul-client
  cloud:
    consul:
      host: localhost
      port: 8500

server:
  port: 2002

启动就可以在服务中心发现自己的服务了
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值