Spring Cloud Alibaba combat service registration and discovery of Nacos

Registration and service discovery, service discovery is mainly used to automate various micro registration service instances and found that the service is the core of micro-governance, learning Spring Cloud Alibaba, we must first understand the framework of the service registration and discovery component --Nacos.

A, Spring Cloud service registration and discovery component

1.Spring Cloud Eureka storm closed source

In Spring Cloud subprojects, Spring Cloud Netflix offers Eureka to implement discovery services, Eureka's service found to contain two major components:

Server discovery component (EurekaServer) and client discovery component (Eureka Client).

Eureka service discovery mechanism shown in FIG.

When the client service program is embedded in the code when running through the annotations, etc., will be registered client service discovery component itself provides to the registry, and updated periodically send heartbeat service,

After three consecutive heartbeats are not able to find the service, then the service node Eureka will be removed from the service registry.

Rest will be ways to achieve call center registered by the registration information between the various services, and the service can be called directly by name.

In 2012, Netflix will Euerka open source, Euerka widely used in the Spring Cloud, but in June 2018, Netflix announced Euerka 2.0 closed-source, community integration is currently version 1.0.

After the closed-source, service discovery choose which component, the answer is Nacos.

 

2. Ali Baba open source Nacos

Alibaba official to locate Nacos is "easier to build a dynamic cloud-native application service discovery, configuration management and service management platform."

Nacos support functions including service discovery, integrated configuration center, and data management services.

 

Two, Nacos local installation and start-up

Nacos can be obtained by source and distributions in two ways, here we download the source code and build.

Download and compile the source code from Github

git clone https://github.com/alibaba/nacos.git
cd nacos/
mvn -Prelease-nacos clean install -U  
ls -al distribution/target/

// change the $version to your actual path
cd distribution/target/nacos-server-$version/nacos/bin

  

Start Nacos Service

Linux / Unix / Mac system
startup command (standalone represents stand-alone mode, non-clustered mode):

sh startup.sh -m standalone

  

Windows System

Start command:

`
cmd startup.cmd
`

  


Double-click the file or run startup.cmd

After the application is started, visit  http://127.0.0.1:8848/nacos/ , Nacos0.8 version has support for simple login function, the default user name / password: nacos / nacos.

 

Third, the integration of Spring Cloud Service Discovery

If you are not very familiar with creating Spring Cloud project, you can look at the front of this blog post.

The latest version of the relationship between the view  Imprint Wiki

Create a service provider

1. New Spring Boot project, named nacos-spring-cloud, adding a dependency in Nocos, Note: The version 0.2.x.RELEASE corresponds Spring Boot 2.x version, version 0.1.x.RELEASE corresponds Spring Boot 1.x version.

    <dependencyManagement>
        <dependencies>
            <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>0.2.2.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>1.1.0</version> </dependency> </dependencies> </dependencyManagement>

2.添加一个模块,nacos-spring-cloud-provider,作为 服务提供者,Pom文件配置如下

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <exclusions> <exclusion> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.1.0.Final</version> </dependency> </dependencies>

3.创建启动类,在启动类中添加一个Restful类型的方法,作为服务实现。

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class NacosProviderApplication { public static void main(String[] args) { SpringApplication.run(NacosProviderApplication.class, args); } @RequestMapping(value = "/hello/{string}", method = RequestMethod.GET) public String echo(@PathVariable String string) { return "Hello " + string; } } 

4.修改配置文件,注册到 Nacos 控制台。

server.port=8070
spring.application.name=service-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

5.启动工程,查看 Nacos 控制台。

 

创建服务消费者

1.添加一个新 module,nacos-spring-cloud-consumer,作为服务消费者。

2.在启动类中添加一个Restful类型的方法,Pom文件依赖如下:

<properties>
        <spring-cloud-openfeign.version>2.0.0.RELEASE</spring-cloud-openfeign.version> <spring-cloud-netflix.version>2.0.0.RELEASE</spring-cloud-netflix.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> <version>${spring-cloud-netflix.version}</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>${spring-cloud-openfeign.version}</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <exclusions> <exclusion> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> </dependency> </dependencies>

3.创建启动类和测试方法,来调用提供者的服务。

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication { public static void main(String[] args) { SpringApplication.run(NacosConsumerApplication.class, args); } @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } }

通过RestTemplate调用服务:

@RestController
public class TestController { @Autowired private RestTemplate restTemplate; @RequestMapping(value = "/hello/{str}", method = RequestMethod.GET) public String echo(@PathVariable String str) { return restTemplate.getForObject("http://service-provider/hello/" + str, String.class); } }

4.修改配置文件,订阅服务

server.port=8080
spring.application.name=service-consumer
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

5.启动工程,查看 Nacos 控制台。

6.测试服务调用,正常返回。

四、总结

这个小教程用一个简单的示例,搭建了基于Nacos的 Spring Cloud 服务发现,可以看到 Nacos 的控制台还有配置管理的功能,下一节会学习 Nacos 的配置管理。

Guess you like

Origin www.cnblogs.com/binyue/p/12095310.html