码迷,mamicode.com
首页 > 其他好文 > 详细

SprngCloud微服务框架搭建(一)

时间:2019-05-20 16:47:31      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:基于   maven工程   1.2   配置   def   man   template   figure   浏览器   

参照来源 :https://blog.csdn.net/forezp/article/details/70148833

1、简介

目前来说,SpringCloud是比较完整的微服务解决方案框架。不像其他rpc远程调用框架,只是解决某个微服务中的问题。

2、微服务框架搭建

2.1、服务的注册与发现Eureka(Finchley版本)

本次采用Eureka作为服务注册与发现的组件。

2.1.1、创建服务注册中心

首先创建一个空的maven工程,在其pom文件引入依赖,

Spring Boot 版本采用  2.0.3.RELEASE。

Spring Cloud 版本采用 Finchley.RELEASE。

这个pom文件作为父pom文件,起到依赖版本控制的作用,其他module工程继承该pom

技术图片
<?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>com.ucredit</groupId>
    <artifactId>dataPlatform</artifactId>
    <version>1.0-SNAPSHOT</version>

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

    <modules>
        <module>eureka-server</module>
        <module>eureka-client</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <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>${spring-cloud.version}</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>


</project>
View Code

 

2.1.2 创建两个model工程

一个model工程作为服务注册中心,即Eureka Server,另一个作为 Eureka Client 。

先创建server,邮件工程 -> 创建 model  ->  选择 spring initializr(如图)

技术图片

下一步 -> 选择 cloud discovery -> eureka server ,然后一直下一步就行了。

技术图片

创建完成后的工程,其pom.xml继承了父pom文件,并引入  spring-cloud-starter-netflix-eureka-server 的依赖,代码如下:

技术图片
<?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>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.3.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.ucredit</groupId>
   <artifactId>eureka-server</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>eureka-server</name>
   <description>Demo project for Spring Boot</description>

   <properties>
      <java.version>1.8</java.version>
      <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-server</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>${spring-cloud.version}</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>

</project>
View Code

2.1.3 启动一个服务注册中心

只需要一个注解  @EnableEurekaServer,这个注解需要加在SpringBoot工程的启动 Application 类上加载:

技术图片
/**
 *  eureka server 是有界面的,启动工程,打开浏览器访问:
 *  http://localhost:8761
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaSeverApplication {

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

2.1.4 修改配置文件

Eureka 是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下 eureka server 也是一个 

eureka client ,必须要指定一个server。 eureka server的配置文件 application.yml:

技术图片
server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: eurka-server
View Code

通过 eureka.client.registerWithEureka:false,和 fetchRegistry:false来表明自己是一个eureka server。

2.1.5 eureka server 是有界面的,启动工程,打开浏览器访问: http://localhost:8761/,界面如下:

技术图片

No application available 没有服务被发现 ...

因为没有注册服务当然不可能有服务发现了 。。。

2.1.6 创建一个服务提供者(eureka client)

当client 向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。eureka server 从每个client实例接收心跳信息。如果心跳超时,则通常将该实例从注册中心删除。

创建过程同server类似,创建完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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.ucredit</groupId>
    <artifactId>eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>${spring-cloud.version}</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>

</project>
View Code

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

技术图片
package com.ucredit.eurekaclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@RestController

/**
 * 通过注解@EnableEurekaClient 表明自己是一个eurekaclient.
 * 仅仅@EnableEurekaClient是不够的,还需要在配置文件中注明自己的服务注册中心的地址
 */
public class EurekaClientApplication {

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

    //读取配置文件
    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }
}
View Code

仅仅@EnableEurekaClient 是不够的,还需要在配置文件中注明自己的服务注册中心的地址。

技术图片
server:
  port: 8762

spring:
  application:
    name: eureka-client

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
View Code

需要指明 spring.application.name 和这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name。

启动工程,打开 http://localhost:8761/ ,即 eureka server的网址。

技术图片

你会发现一个服务已经注册在服务中了,服务名为 EUREKA-CLIENT,端口号是 8762。这时打开浏览器 http://localhost:8762/hi?name=forezp ,

会在浏览器中看到:

技术图片

2.2 服务消费者(rest+ribbon)(Finchley版本)

2.2.1 讲了服务的注册与发现。在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于htttp restful的。

Spring Cloud 有两种服务的调用方式:一种是ribbon + restTemplate,另一种是feign。在此讲述基于 ribbon + rest。

Ribbon 是一个负载均衡客户端,可以很好的控制http和tcp的一些行为。

Feign 默认集成了ribbon。

2.2.2 准备工作

这一篇基于上一篇的工程,启动 eureka-server工程,启动 server-client工程,它的端口号为8762,将server-client的配置文件的端口改为8763,并启动,这时你会发现:

server-client 在 eureka-server 注册了2个实例,这就相当于一个小的集群了。

如何一个工程启动多实例,请看:

https://blog.csdn.net/forezp/article/details/76408139

2.2.3 建一个服务消费者。

重新新建一个spring-boot工程,取名service-ribbon,在它的pom.xm继承了父pom文件,并引入了以下依赖:

技术图片
<?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>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.3.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.ucredit</groupId>
   <artifactId>server-ribbon</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>server-ribbon</name>
   <description>Demo project for Spring Boot</description>

   <properties>
      <java.version>1.8</java.version>
      <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
   </properties>

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

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-web</artifactId>
         <version>4.3.18.RELEASE</version>
      </dependency>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-netflix-eureka-client</artifactId>
         <version>RELEASE</version>
      </dependency>
   </dependencies>

   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</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>

</project>
View Code

在工程的配置文件指定服务的注册中心地址为  http://localhost:8761/eureka

程序名称为 eureka-ribbon,程序端口为8764。配置文件 application.yml 如下:

技术图片
server:
  port: 8764

spring:
  application:
    name: eureka-ribbon

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
View Code

在工程的启动类中,通过 @EnableDiscoveryClient 向服务中心注册,并且向程序IOC注入一个bean:restTemplate

并通过 @LoadBalanced 注解表名这个 restTemplate开启负载均衡的功能。

技术图片
 package com.ucredit.serverribbon;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    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;

    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableEurekaClient //表名自己是一个eureka客户端
    public class ServerRibbonApplication {

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


        @Bean
        @LoadBalanced
        RestTemplate restTemplate() {
            return new RestTemplate();
        }

    }
View Code

写一个测试类,HelloService,通过之前注入ioc容器的restTemplate来消费 eureka-client 服务的 "/hi" 接口,在这里我们直接用的程序名替代了具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候回用具体的url替换掉服务名。

技术图片
package com.ucredit.serverribbon.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {
    @Autowired
    RestTemplate restTemplate;


    public String hiService(String name) {
        return restTemplate.getForObject("http://eureka-client/hi?name="+name,String.class);
    }

}
View Code

写一个controller ,在controller中调用 HelloService

技术图片
package com.ucredit.serverribbon.controller;

import com.ucredit.serverribbon.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloControler {
    @Autowired
    HelloService helloService;

    @GetMapping(value = "/hi")
    public String hi(@RequestParam String name) {
        return helloService.hiService( name );
    }


}
View Code

在浏览器中访问 http://localhost:8764/hi?name=forezp ,浏览器交替显示:

技术图片

这说明当我们调用 restTemplate.getForObject("http://eureka-client/hi?name="+name,String.class); 
方法时,已经做了负载均衡,访问了不同的端口的服务实例。

2.2.4 此时的架构
技术图片

一个服务注册中心,eureka server,端口为8761

service-hi 工程跑了两个实例,端口分别为8762,8763,分别向服务注册中心注册。

service-ribbon 端口为8764,向服务注册中心注册。

当service-ribbon 通过 restTemplate 调用 service-hi的 hi 接口时,因为ribbon 进行了负载均衡,会轮流的调用 service-hi:8762和8763两个端口的hi接口。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

技术图片

SprngCloud微服务框架搭建(一)

标签:基于   maven工程   1.2   配置   def   man   template   figure   浏览器   

原文地址:https://www.cnblogs.com/ssqq5200936/p/10761278.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!