Micro Spring Cloud Services Portal

What is micro-services?

       Micro service is to all modules of a project originally bloated split open and do not associate with each other, you can not even use the same database.   For example: the project there User Modules and Power Modules, but User Modules and Power Modules are not directly related, just some data that need to interact, it can be right that two separate modules separately, when the user needs to call the power of, power is a service side, but the power you need to call the user when the user is the service side, so, they do not care who is the party who is serving the caller, they are two separate services, this time, the concept of micro-services He came out.

The difference between micro and distributed services

       Speaking difference, let's briefly talk about what distributed is called a distributed, is the huge system is divided into multiple modules (This and services like micro) deployed on different machines (since a machine could not afford cost so much pressure or a very good server can be several of the ordinary), each module exchanges data through the interface, in fact, distributed micro is also a service. Because the modules are now split off into a separate unit, provides the interface to call, then the difference in the nature of what they do? Their main difference is reflected in the "target", what is the goal of this architecture is that you project to do. What Distributed goal is? We just saw it, that is, a machine can not afford, or cost, have to use multiple machines to complete the deployment of services, and micro-target service just let each module split open, will not be affected by each other , such as upgrading or also appear BUG modules etc ..., you can also use a word to understand: is distributed micro kind of service, and micro-services he may be on a single machine.

Micro Services and Spring-Cloud relationship (difference)

 Micro-service architecture is just a way to project or a concept, just as our MVC architecture the same, then Spring-Cloud is the realization of this technology.

Spring Cloud quickly build project

1. Create a new project, choose Maven , click Next

2. relevant information to fill in the project, go to next step

3. Fill in the project name and project location

 4. The project as a Parent project exists, src files can be deleted

Add Eureka Server Component 

1. On the Project Right -> new -> module -> Spring Initializr -> next

 2. Fill in project-related information

3. Select Cloud Discovery

4. Fill in the project name and project location

5. Application class, adding @EnableEurekaServer annotations

Note : This comment indicates that label class is a Eureka Server

 

 6. modify the configuration, start switch port

   I was yml application.yml

# 服务注册中心 (单节点)
server:
  port: 8090
eureka:
  instance:
    hostname: localhost
  client:
    fetch-registry: false # 表示是否从Eureka Server获取注册信息,默认为true.因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,这里设置为false
    register-with-eureka: false # 表示是否将自己注册到Eureka Server,默认为true.由于当前应用就是Eureka Server,故而设置为false.
    service-url:
      # 设置与Eureka Server的地址,查询服务和注册服务都需要依赖这个地址.默认是http://localhost:8090/eureka/;多个地址可使用','风格.
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

7.启动项目

在浏览器中输入你设置的端口号我的是8090

localhost:8090

添加 EurekaProducer 服务生产者

1.按照同样的方式,创建一个项目,这里我们创建一个Spring Boot风格的服务,

创建时需要勾选 Spring Cloud Discover--> Eureka Discover Client Spring Web 的依赖

 2.application启动类中加入注解@EnableEurekaClient,表明自己属于一个生产者。这里为了方便测试,直接使用@RestController获取返回值。 

3.修改配置

server:
  port: 8081
 
spring:
  application:
    name: eureka-producer
 
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8090/eureka # 指定服务注册中心

 4.启动应用,刷新Eureka控制台

   可以看到服务已经注册到Eurek

创建 Eureka Consumer 服务消费者

1.通过 Spring Initializr,创建一个 Eureka Discovery Client 模块,同时要勾选加入Spring Web依赖。

 

 2.修改原有配置,指定服务注册中心,这里还是使用yml文件。

# 服务注册中心 (单节点)
server:
  port: 8082

spring:
    application:
      name: eureka-consumer

eureka:
    client:
      service-url:
        defaultZone: http://localhost:8090/eureka # 指定服务注册中心

3.在启动类中添加@EnableDiscoveryClient表明标注类是消费者,加入restTemplate来消费相关的服务。

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaConsumerApplication {

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

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

4..创建controller层,消费远程服务

@RestController
public class DemoController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("greet")
    public String sayHello(@RequestParam String name){

        return restTemplate.getForObject("http://EUREKA-PRODUCER/index?param=" + name, String.class);
    }

}

5.配置完毕以后,启动服务消费者,刷新Eureka控制台,可以看到消费者已经注册。

5.打开浏览器输入localhost:{server.port}/path 进行服务调用,

这里我用 http://localhost:8082/greet?name=eureka ,可以看到请求正确返回,正确调用了服务提供者。

 

 

 

Guess you like

Origin www.cnblogs.com/limengcheng/p/12239152.html