preface

In the last post we talked about how to build NaCos using K8S. In this post we will talk about how to register and configure micro-services through the built NaCos service.

operation

Service A, Service B, and Service A use Feign to call methods in Service B.

Service A and Service B introduce related dependency packages,

  • Spring Cloud version: 2020.0.0
  • SpringBoot version: 2.4.2
  • Alibaba version: 2021.1

    <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> < version > < / version 2.4.2 > < / parent > < properties > < Java version > 1.8 < / Java version > < spring - the boot version > < 2.4.2 / spring - the boot. Version > < spring - cloud. Version > 2020.0.0 < / spring - cloud. Version > < spring - cloud - alibaba. Version > 2021.1 < / spring - cloud - alibaba. Version > < / properties > < dependencyManagement > < dependencies > <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <! - spring cloud depend on - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <! Cloud </ grouppid > -- Dependency > <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency>  <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency> </dependencies>

    The bootstrap.yml file for A and B services to register and configure to the Nacos service:

    Spring: Application: Name: A-Service # B-Service Profiles: Active: Test Cloud: Nacos: server-addr: http://nacos-headless.default.svc.cluster.local:8848 namespace: xxx-xxx-xxx-xxx service: -config: config: Nacos config: server-addr -config: Nacos config: server-addr -addr http://nacos-headless.default.svc.cluster.local:8848 file-extension: yaml prefix: a-service # b-service namespace: xxx-xxx-xxx-xxx server: port: a-port # b-port

You can create configuration files for A and B services directly on NaCos:



B Service method:

@SpringBootApplication @EnableDiscoveryClient @RestController @RefreshScope public class BServiceApplication { public static void main(String[] args) { SpringApplication.run(BServiceApplication.class, args); } @Value("${server.port}") String port; @GetMapping("/hi") public String hi(@RequestParam(value = "name", defaultValue = "forezp",required = false) String name) { return "hello " + name + ", i'm provider ,my port:" + port; }}

Service A wants to call the method of Service B, first create the class BFeignClient:

@FeignClient("b-service")
public interface BFeignClient {
    @GetMapping("/hi")
    String hi(@RequestParam(value = "name", defaultValue = "forezp", required = false) String name);
}

Next, create the method to call:

@SpringBootApplication @RestController @EnableDiscoveryClient @EnableFeignClients @RefreshScope public class AServiceApplication { public static void main(String[] args) { SpringApplication.run(AServiceApplication.class, args); } @Autowired BFeignClient bFeignClient; @GetMapping("/hi-feign") public String hiFeign(){ return bFeignClient.hi("feign"); }}

Run to see if the two services have been successfully registered with NaCos, as shown in the figure:

After the registration is successful, we can test whether service A successfully invokes service B:

conclusion

1. Because we are using service headless, so we need to use DNS to find NACOS, default is your service name and fixed writing: Default. The SVC. Cluster. The local, my nacos service name is nacos – headless, namely: nacos – headless. Default. SVC. Cluster. The local

2, Nacos is using the latest version, so Discovery must add service attribute, otherwise error will be reported

discovery:
  server-addr: http://nacos-headless.default.svc.cluster.local:8848
  namespace: xxx-xxx-xxx-xxx
  service: a-service  # b-service

3. Because Spring Cloud :2020.0 is being used, POM introduces the bootstrap package, otherwise bootstrap.yml will not work

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

reference

SpringCloud 2020 tutorial 1: Using Nacos as a Registry and Configuration Center Inside a K8S Cluster Using DNS Domain Name Description – Methodology, Other Access Services via Domain Name Like K8S Deploy Nacos 3 Nodes…. N All nodes can cluster Kubernetes NaCos