SpringCloud進行nacos的服務註冊和服務管理案例

燕子去了發表於2024-04-21

SpringCloud服務註冊

pom.xml

<!--SpringCloud 服務註冊和發現-->
<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
	<version>2.2.8.RELEASE</version>
</dependency>

bootstrap.properties

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

單獨使用配置中心的地址也可以,分開寫可以區分註冊中心和配置中心

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

啟動類使用@EnableDiscoveryClient開啟服務註冊
MyApplication

package com.lmcode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class,args);
    }
}

服務提供者的方法

package com.lmcode.ccontroller;

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

@RestController
public class UserController {
    @GetMapping("/provider")
    public String provider(){
        return "provider";
    }
}

SpringCloud服務發現

pom.xml

<!--SpringCloud 服務註冊和發現-->
<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
	<version>2.2.8.RELEASE</version>
</dependency>

application.properties

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

透過定義一個RestTemplate傳送http請求並呼叫服務
Application

package com.lmcode;

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 Application {
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

ConsumerController

package com.lmcode.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/consumer")
    public String echo(){
        // provider服務的provider方法
        return restTemplate.getForObject("http://provider/provider",String.class);
    }
}

http://localhost:9092/consumer

image

相關文章