SpringCloud微服务基础

SpringCloud

SpringCloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、负载均衡、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。另外说明spring cloud是基于Springboot的,所以需要开发中对Springboot有一定的了解,如果不了解的话可以看蚂蚁课堂SpringBoot课程。

服务提供者与消费关系

服务提供者:提供服务被人调用

消费者:调用被人服务

服务的注册与发现(Eureka )

在这里,我们需要用的的组件上Spring Cloud Netflix的Eureka ,eureka是一个服务注册和发现模块。

 

什么是Eureka

官方的介绍在这里Eureka wiki。Eureka是Netflix开源的一个RESTful服务,主要用于服务的注册发现。Eureka由两个组件组成:Eureka服务器和Eureka客户端。Eureka服务器用作服务注册服务器。Eureka客户端是一个java客户端,用来简化与服务器的交互、作为轮询负载均衡器,并提供服务的故障切换支持。Netflix在其生产环境中使用的是另外的客户端,它提供基于流量、资源利用率以及出错状态的加权负载均衡。
在我看来,Eureka的吸引力来源于以下几点:

开源:大家可以对实现一探究竟,甚至修改源码。

可靠:经过Netflix多年的生产环境考验,使用应该比较靠谱省心

功能齐全:不但提供了完整的注册发现服务,还有Ribbon等可以配合使用的服务。

基于Java:对于Java程序员来说,使用起来,心里比较有底。

spring cloud可以使用Spring Cloud, 与Eureka进行了很好的集成,使用起来非常方便。

 

实现服务注册

 

创建EureKaserver 项目

Maven依赖

  <parent>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-parent</artifactId>

            <version>1.5.2.RELEASE</version>

            <relativePath /> <!-- lookup parent from repository -->

      </parent>

      <properties>

            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

            <java.version>1.8</java.version>

      </properties>

 

      <dependencies>

            <!--eureka server -->

            <dependency>

                  <groupId>org.springframework.cloud</groupId>

                  <artifactId>spring-cloud-starter-eureka-server</artifactId>

            </dependency>

            <!-- spring boot test -->

            <dependency>

                  <groupId>org.springframework.boot</groupId>

                  <artifactId>spring-boot-starter-test</artifactId>

                  <scope>test</scope>

            </dependency>

      </dependencies>

      <dependencyManagement>

            <dependencies>

                  <dependency>

                       <groupId>org.springframework.cloud</groupId>

                       <artifactId>spring-cloud-dependencies</artifactId>

                       <version>Dalston.RC1</version>

                       <type>pom</type>

                       <scope>import</scope>

                  </dependency>

            </dependencies>

      </dependencyManagement>

      <build>

            <plugins>

                  <plugin>

                       <groupId>org.springframework.boot</groupId>

                       <artifactId>spring-boot-maven-plugin</artifactId>

                  </plugin>

            </plugins>

      </build>

      <repositories>

            <repository>

                  <id>spring-milestones</id>

                  <name>Spring Milestones</name>

                  <url>https://repo.spring.io/milestone</url>

                  <snapshots>

                       <enabled>false</enabled>

                  </snapshots>

            </repository>

      </repositories>

 

配置application.yml

server:

  port: 8888

eureka:

  instance:

    hostname: localhost

  client:

    registerWithEureka: false

    fetchRegistry: false

    serviceUrl:

      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 

启动EurekaServer

@SpringBootApplication

@EnableEurekaServer

public class App {

      public static void main(String[] args) {

            SpringApplication.run(App.class, args);

      }

}

打开eureka server 界面的

http://localhost:8761 ,界面如下:

 

No application available 没有服务被发现 ……^_^ 
因为没有注册服务当然不可能有服务被发现了。

 

 

实现案例订单服务调用会员服务查询用户信息

 

 

服务提供者

创建一个服务提供者 会员服务工程 (eurekaMember),提供会员查询服务信息

创建项目service-member

Maven依赖

<parent>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-parent</artifactId>

            <version>1.5.2.RELEASE</version>

            <relativePath /> <!-- lookup parent from repository -->

      </parent>

      <properties>

            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

            <java.version>1.8</java.version>

      </properties>

      <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>

            <dependency>

                  <groupId>org.springframework.boot</groupId>

                  <artifactId>spring-boot-starter-test</artifactId>

                  <scope>test</scope>

            </dependency>

      </dependencies>

      <dependencyManagement>

            <dependencies>

                  <dependency>

                       <groupId>org.springframework.cloud</groupId>

                       <artifactId>spring-cloud-dependencies</artifactId>

                       <version>Dalston.RC1</version>

                       <type>pom</type>

                       <scope>import</scope>

                  </dependency>

            </dependencies>

      </dependencyManagement>

      <build>

            <plugins>

                  <plugin>

                       <groupId>org.springframework.boot</groupId>

                       <artifactId>spring-boot-maven-plugin</artifactId>

                  </plugin>

            </plugins>

      </build>

      <repositories>

            <repository>

                  <id>spring-milestones</id>

                  <name>Spring Milestones</name>

                  <url>https://repo.spring.io/milestone</url>

                  <snapshots>

                       <enabled>false</enabled>

                  </snapshots>

            </repository>

      </repositories>

application.yml配置

eureka:

  client:

    serviceUrl:

      defaultZone: http://localhost:8888/eureka/

server:

  port: 8762

spring:

  application:

    name: service-member

服务接口

@RestController

public class MemberController {

 

      @RequestMapping("/getUserList")

      public List<String> getUserList() {

            List<String> listUser = new ArrayList<String>();

            listUser.add("zhangsan");

            listUser.add("lisi");

            listUser.add("yushengjun");

            return listUser;

      }

 

}

 

发布服务

通过注解@EnableEurekaClient 表明自己是一个eurekaclient.

@SpringBootApplication

@EnableEurekaClient

public class AppMember {

 

      public static void main(String[] args) {

            SpringApplication.run(AppMember.class, args);

      }

 

}

演示效果

需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。 
启动工程,打开127.0.0.1:8888 ,即eureka server 的网址:

 

你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI ,端口为7862

这时打开 http://127.0.0.1:8762/getUserList ,你会在浏览器上看到 :

["zhangsan","lisi","yushengjun"]

 

服务消费者

创建项目sercice-order

Maven依赖

<parent>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-parent</artifactId>

            <version>1.5.2.RELEASE</version>

            <relativePath /> <!-- lookup parent from repository -->

      </parent>

 

      <properties>

            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

            <java.version>1.8</java.version>

      </properties>

 

      <dependencies>

            <dependency>

                  <groupId>org.springframework.cloud</groupId>

                  <artifactId>spring-cloud-starter-eureka</artifactId>

            </dependency>

            <dependency>

                  <groupId>org.springframework.cloud</groupId>

                  <artifactId>spring-cloud-starter-ribbon</artifactId>

            </dependency>

            <dependency>

                  <groupId>org.springframework.boot</groupId>

                  <artifactId>spring-boot-starter-web</artifactId>

            </dependency>

 

            <dependency>

                  <groupId>org.springframework.boot</groupId>

                  <artifactId>spring-boot-starter-test</artifactId>

                  <scope>test</scope>

            </dependency>

      </dependencies>

 

      <dependencyManagement>

            <dependencies>

                  <dependency>

                       <groupId>org.springframework.cloud</groupId>

                       <artifactId>spring-cloud-dependencies</artifactId>

                       <version>Dalston.RC1</version>

                       <type>pom</type>

                       <scope>import</scope>

                  </dependency>

            </dependencies>

      </dependencyManagement>

 

      <build>

            <plugins>

                  <plugin>

                       <groupId>org.springframework.boot</groupId>

                       <artifactId>spring-boot-maven-plugin</artifactId>

                  </plugin>

            </plugins>

      </build>

 

      <repositories>

            <repository>

                  <id>spring-milestones</id>

                  <name>Spring Milestones</name>

                  <url>https://repo.spring.io/milestone</url>

                  <snapshots>

                       <enabled>false</enabled>

                  </snapshots>

            </repository>

      </repositories>

 

application.yml配置

eureka:

  client:

    serviceUrl:

      defaultZone: http://localhost:8888/eureka/

server:

  port: 8764

spring:

  application:

    name: service-order

 

编写service,调用service-member

@SuppressWarnings("unchecked")

@Service

public class MemberService {

      @Autowired

      RestTemplate restTemplate;

 

      public List<String> getOrderByUserList() {

            return restTemplate.getForObject("http://service-member/getUserList", List.class);

      }         

}

 

演示效果

@EnableEurekaClient

@SpringBootApplication

public class AppOrder {

 

      public static void main(String[] args) {

            SpringApplication.run(AppOrder.class, args);

      }

 

      @Bean

      @LoadBalanced

      RestTemplate restTemplate() {

            return new RestTemplate();

      }

 

}

 


在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。 

posted @ 2019-02-22 16:11  虚极静笃  Views(167)  Comments(0Edit  收藏  举报