SpringCloudAlibaba enterprise applications in actual combat: Based Nacos achieve service discovery

Achieve service discovery based Nacos

Remarks

In this paper, a simple example to demonstrate how to SpringClouduse eco-system Nacosto implement the service discovery function. Business processes are as follows:
Here Insert Picture Description

Service provider implementation steps

Dependency Management

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

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

Service Profiles

server.port=7000
spring.application.name=sca-account-service
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
management.endpoints.web.exposure.include=*

Create a service provider startup class

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ScaAccountServiceApplication implements CommandLineRunner {

    Logger logger = LoggerFactory.getLogger(this.getClass());

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

    public void run(String... args) throws Exception {
        logger.info("-------------- 账户应用服务提供者-启动成功 --------------");
    }

Create a server controller

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AccountController {


    @GetMapping(value = "/echo/{string}")
    public String echo(@PathVariable String string) {
        return "您好:" + string;
    }

}

These are the main code service provider

Service consumer implementation steps

Dependency Management

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

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

Service Profiles

server.port=7001
spring.application.name=sca-account-service
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
management.endpoints.web.exposure.include=*

Create a service consumer class start

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class AccountConsumerApplication implements CommandLineRunner {

    Logger logger = LoggerFactory.getLogger(this.getClass());

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

    public void run(String... args) throws Exception {
        logger.info("-------------- 账户应用服务消费者-启动成功 --------------");
    }

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

Create a server controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class AccountConsumerController {


    @Autowired private RestTemplate restTemplate;

    @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
    public String echo(@PathVariable String str) {
        return restTemplate.getForObject("http://sca-account-service/echo/" + str, String.class);
    }

}

These are the main consumers of services codes

Test Procedure

Respectively, to start the service provider and service consumer, you can Nacostake a look at the list of services console, as shown below:
Here Insert Picture Description
browser address by entering this request and observe the output page:
Service Provider Request: http://localhost:7000/echo/我是服务提供者. The results shown below:
Here Insert Picture Description
Service consumers http://localhost:7001/echo/我是服务消费者request: . The results as shown below:
Here Insert Picture Description

Quote

He published 188 original articles · won praise 150 · Views 650,000 +

Guess you like

Origin blog.csdn.net/myNameIssls/article/details/103742685