Spring Cloud + nacos + Feign to realize the registration center and configuration center

Write in front

The concepts of registration center and configuration center are not explained here. Found that the service used to be Eureka, because this guy is closed source, uncomfortable. Then I found nacos, Alibaba's, good stuff, a registration center and configuration center. Official website: https://nacos.io/en-us/ . The official website's manual can be more introductory, and practicality needs to be combined with the project. Next, let's briefly introduce how to use nacos to build a registration center and service center.

Before proceeding to the following operations, go to git and download a packaged and released nacos service at https://github.com/alibaba/nacos/releases . Linux downloads the first, and windows downloads the second. The last two are the source code. How to start using nacos, the nacos official website is very detailed and will not go into details.

Registration Center

service providers

Download an original spring boot project from https://start.spring.io/ , don't forget to add Web dependencies, how to download is not mentioned here. Add dependencies:

<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  <version>2.2.1.RELEASE</version>
</dependency>

Configuration file application.properties add configuration:

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

Start class :

 

 

 Add a Test Controller:

 

 

 At this point, the service provider is complete.

Serving consumers

Operate with the service provider, download an original spring boot project, add dependencies, note that because you want to use feign to call the service provider, you need to add the corresponding dependencies:

<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  <version>2.2.1.RELEASE</version>
</dependency>
  <dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
  <version>2.2.2.RELEASE</version>
</dependency>

Configuration file application.properties add configuration:

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

Start class:

 

 

 Add feign client:

 

 

 Add the test controller:

 

 

 Preparation work is completed, start nacos, service providers, and service consumers. After all are successfully started, you can see two registered services in the nacos console interface:

 

 

 

 

 Use the browser to visit directly: http://127.0.0.1:8080/consumer/hello-consumer .

Configuration Center

Use the nacos configuration center directly in the service provider above . Add dependencies:

<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  <version>2.2.1.RELEASE</version>
</dependency>

 

Add the following annotations and code to the Controller, and modify the interface:

 Configuration file application.properties add configuration:

spring.cloud.nacos.config.server-addr=127.0.0.1:8848

Restart the service provider.

 Add the configuration in the [configuration list] of the nacos console interface:

 

 

 

 After publishing, visit http://127.0.0.1:8080/consumer/hello-consumer .

Write at the end

It briefly introduces how to use it. As for the use of the annotations, everyone will use Baidu.

Guess you like

Origin www.cnblogs.com/ncwuwsh/p/12732516.html