💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
将服务端注册到Nacos中。 <br/> 步骤如下: **1.构建服务端模块:cloudalibaba-provider-payment9001** **2. 在当前模块的`pom.xml`添加 alibaba-nacos-discovery 依赖** ```xml <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> ... </dependencies> ``` **3. 当前模块的`application.yml`配置** ```yml server: port: 9001 spring: application: name: nacos-payment-provider #当前服务注册到nacos的名称 cloud: nacos: discovery: server-addr: localhost:8848 #nacos的访问地址 management: endpoints: web: exposure: include: "*" ``` **4. 在当前模块中创建类 PaymentController 方便观察效果** ```java @RestController @RequestMapping("/payment") public class PaymentController { @Value("${server.port}") public String serverPort; @RequestMapping("/getPayment/{id}") public String getPayment(@PathVariable("id") Integer id) { return "Alibaba Nacos server " + serverPort + "-----" + id; } } ``` **5. 在当前模块的启动类上添加注解`@EnableDiscoveryClient`** ```java @SpringBootApplication @EnableDiscoveryClient public class NacosProviderMain9001 { public static void main(String[] args) { SpringApplication.run(NacosProviderMain9001.class, args); } } ``` **6. 测试** (1)先启动 nacos,后启动当前模块。 (2)浏览器访问nacos服务: http://localhost:8848/nacos (用户名:`nacos`、密码:`nacos`)。 ![](https://img.kancloud.cn/ec/0d/ec0d449455923fbb5d66ba5708c79191_1433x486.jpg) (3)看下图,可见 payment9001 服务端已经注册到nacos了。 ![](https://img.kancloud.cn/97/3b/973bd5d6b819056694e90d0fd5b1523b_1867x477.jpg)