Use spring cloud service framework set up micro

   bubbo + zookeeper production of microprocessor-based service framework dubbo can be said to be a little out of date, let's look at what is micro-services.

 

  Each micro-services API is an application system effectively split, and in some way connected to provide services to the API call other applications, implementation, and solve a series of problems during the call integrated micro services architecture is

 

  As the name suggests, micro-services have two ways to understand, what is the "micro" and what is "service."

  micro-

  Simple talk is small, run anywhere, to develop simple. The traditional spring mvc framework to configure complicated, messy project dependencies often make development and maintenance headaches.

  service  

  The so-called service, must be distinguished from the system, a service or a set of relatively small and independent functional units, a user may perceive a minimum set of features.

  First micro-service co-sponsored by the Martin Fowler and James Lewis in 2014, is a micro-service architecture style using a small set of services to develop a single application mode approach , each service runs in its own process, and the use of lightweight communication mechanism, usually HTTP API, these services are based on operational capacity building, and can be deployed independently by automating the deployment mechanism, these services use different programming language, as well as different data storage technologies, and to maintain a minimum of centralized management.

  Split applications

  Programmers A, B, C, they now need to develop a set of order management systems.

  A single programmer system architecture, service code do mvc split tip portion made using php render layers. 

  Programmer B multi-application architecture, product, order, logistics, work orders, customer service and so do the split-level applications, and then use the REST API to interact directly or tcp, each application is a mvc systems, developed independently.

  C programmers using micro-services, products, orders, logistics, work orders, customer service and so do the split-level applications, and then use the micro-services framework will be split they broke into one service, such as order tracking list, product list tracking, product details inquiry services. Use one service composition application and then integrated into order management system

  There is no doubt that the way architecture can only programmer A small amount in order to eat the company's application on the Dayton's.

  The programmer B architecture is similar to most of the early electronic business platform logic architecture, this architecture to adapt to very good, but with the continuous expansion of the system, maintenance becomes extremely complicated, API between versions of change, along with the application call a variety of complex needs.

  With the evolution of architecture B, C programmers architecture model was born, slightly services. Micro understanding is that the service will be similar to the list of orders tracking, list of products tracking, product details inquiry service segmentation, differentiation. So that our code can be integrated through a unified gateway, allowing the entire system's development efforts completely integrate micro and service management service, fuse, load balancing, etc. functions to strengthen the management capacity of the entire system.

  Currently the background in architecture to deal with large sites, micro-service is the most appropriate set of patterns. Simultaneous spring boot of micro-services allow developers easier and easier to understand, for example dubbo development approach is actually relatively simple, understandable, spring cloud is the ability to deep integration spring boot, to borrow friends a comparison chart:

Figure Source: https: //blog.csdn.net/zhouyanqingbfq/article/details/79407285

In addition there are a lot of knowledge about the concept of micro-services, compiled some bloggers:

  https://blog.csdn.net/fly_zhyu/article/details/76408158

  https://www.cnblogs.com/wintersun/p/6219259.html

 

 

spring boot

   Spring Boot is not a new technology, so do not be nervous. In essence, Spring Boot is Spring, Spring Bean as it did those without it you will do the configuration. It uses the "habit over configuration" (configuration exists a large number of projects, in addition to built-in configuration of a habit, so you do not need to manually configure) concept to get your project up and running quickly. Use Spring Boot very easy to create a separate run (run jar, embedded Servlet container), the level of production-based Spring framework of the project, you can not use Spring Boot requires little or Spring configuration.

  This section is no longer bloggers recount, provide some information we look like, click through this blog of people here assume that you will some of it.

    [Turn] Spring Boot series of dry goods :( a) elegant Beginners

 

spring cloud

  Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state). Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud developers can quickly stand up services and applications that implement those patterns. They will work well in any distributed environment, including the developer's own laptop, bare metal data centres, and managed platforms such as Cloud Foundry.

  spring cloud provides tools to quickly build some common patterns of distributed systems (such as configuration management, service discovery, circuit breakers, routing, micro broker, a control bus, token, global lock, distributed sessions, cluster state), developed using Spring Cloud you can quickly stop the services and applications that implement these patterns. They can run well in any distributed environment, including the developer's own laptops, bare metal data center and hosting platforms such as Cloud Foundry.

  The first sentence of introduction from the official website of the spring cloud, spring cloud provides a range of tools to build a distributed system, these tools together to form a micro-service architecture system, registry, gateways, provider, consumer, Rest, distributed lock, distributed session and more.

  spring cloud of learning data compilation:

   http://www.ityouknow.com/springcloud/2016/12/30/springcloud-collect.html     

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

Registry

   Build bloggers eureka registration center set up to use IDEA

  1 First, create an empty maven project

  

  Fill parameters:

    

  

  finish it ok

  

  After the finish src folder delete the directory

  

  

  2. Then the new module, right in the root directory of cloud, select the new "module  

  

 

  Create a spring boot project, and add the eureka dependent

  

 

 

Note content root address, erueka in cloudDemo directory

 

Click to finish, it generated a springboot project, waiting maven initialization is complete, then the project structure, and eureka's pom files are as follows:

  

 

 

 Start eureka registry is very simple, with the start in the class springboot comments

@EnableEurekaServer

 

  Then configure our spring boot start:

  将resource下面的 application.properties文件改为application.yml文件并添加如下配置:

 

server:
  port: 8801

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

 

然后启动项目访问localhost:8801,看到这个界面,Eureka注册中心就已经启动成功,接下来我们可以编写一个提供者,一个消费者。

 

 spring cloud的消费者也可以是提供者,二者只有逻辑上的区别,它是链式的存在。在spring boot中有两种消费方式:

 1.rest+ribbon

 2.Feign

接下来我们编写消费者

rest+ribbon消费

   首先保持Eureka的启动不变,然后在cloudDemo下新建一个module 叫做ribbon,它也是spring boot结构:

  

  

  

  注意我们要添加三个依赖,如上图红框依次选择即可。

  

  一样,要注意content root的目录。

  创建完成后,编写springboot的启动类,首先加上 @ComponentScan 注解,然后添加 @EnableDiscoveryClient 注册服务,然后注入RestTemplate对象,

 @LoadBalanced 表示开启负载均衡

  完整的代码如下:

 

@SpringBootApplication
@EnableDiscoveryClient //注册服务
@ComponentScan
public class RibbonApplication {

    public static void main(String[] args) {

        SpringApplication.run(RibbonApplication.class, args);
    }
    @Bean
    @Autowired
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

 

  然后我们创建一个测试用的service文件:

  

  

  代码如下:

 

@Service
public class HelloService {
    public String sayHello(){
        return "helloWorld"; // 提供一个hello World
    }
}

 

然后我们再编写一个controller,提供一个hello的controller:

完整代码如下:

 

@RestController
public class HelloController {
    @Autowired
    private HelloService helloService;
    @GetMapping("/hello")
    public String sayHello(String name){
        return helloService.sayHello() + " " + name;
    }
}

 

 

 然后我们再配置application.yml , 一样的 将配置文件改为yml格式,然后输入配置:

  

 

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8801/eureka/
server:
  port: 8802
spring:
  application:
    name: ribbon-provider

 

之后启动服务,启动后访问 localhost:8802/hello?name=zhangtaifeng :

  

测试完成,这个服务我们用他做服务提供者(provider),然后我们再重新建一个module,可以叫做 ribbon-consumer,整体配置一致,我们只需要修改service和controller部分(注意package):

 

然后修改配置文件为:

 

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8801/eureka/
server:
  port: 8803
spring:
  application:
    name: ribbon-consumer

 

改了端口和应用名称。

 

 

 然后复制上面的spring boot入口文件代码,复制service和controller文件,依然 注意包名和路径,然后修改HelloService:

我们启动应用看看:

  

RestTemplate成功调用了8802端口上的provider。

到了这里,相信大家对spring cloud调用服务有了一定的了解,cloud通过rest调用服务进行服务间的通信,每一个controller的方法对外提供对应的服务接口。相比较dubbo的rpc调用有一些不一样的优势,同时它的开发方式更简单,配合zuul网关的使用能够达到更好的效果。

Feign消费

 feign消费是通过注解的方式进行的消费模式,它默认打开了负载均衡,还是在cloudDemo下创建一个module,名字叫feign:

  

 

 

注意引用的包。

创建完成,首先编写入口文件,主要是加上

@EnableDiscoveryClient
@EnableFeignClients

  这两个注解,用于注册和发现服务。

  

然后我们在目录下新建一个service,controller,或者从前面的工程复制,另外需要新建一个rpc文件夹,然后在rpc下新建一个GetHello接口:

  

 

编写访问rpc的方法,使用feign调用更加简便,添加@FeignClient注解即可,value的意思就是目标服务器,可以直接用yml中配置的application名称,完整代码如下:

@FeignClient(value = "ribbon-consumer")
public interface GetHello {
    @RequestMapping(value = "/hello?name=feign",method = RequestMethod.GET)
    public String sayHello();
}

然后再改造下service代码:

 

@Service
public class HelloService {
    @Autowired
    private GetHello getHello; //注入rpc

    public String sayHello(){
        return getHello.sayHello(); // 提供一个hello World
    }
}

 

最后还是修改application.yml文件,配置大致相同,修改应用名和端口,代码如下:

 

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8801/eureka/
server:
  port: 8804
spring:
  application:
    name: feign-consumer

 

然后我们启动这个工程看看有什么效果,注意,这所有的步骤,前面的服务都没有关闭噢:

 

启动后效果如图。

 

 

小结

  下载文件地址 

  从这里看,spring cloud将我们的几个应用串联起来,各个服务之间互相通过restAPI调用,我觉得Feign要优于ribbon,另外Eureka注册中心也可以换成zookeeper或者consul.

 

本文转自:https://www.cnblogs.com/ztfjs/p/9230374.html

发布了19 篇原创文章 · 获赞 12 · 访问量 1857

Guess you like

Origin blog.csdn.net/AnNanDu/article/details/104311229