当前位置: 首页 > wzjs >正文

自建网站如何赚钱色盲怎么治疗

自建网站如何赚钱,色盲怎么治疗,网站不维护会怎么样,菏泽微信小程序制作Nacos是Dynamic Naming and Configuration Service的缩写。What’s Nacos? 下面结合SpringBoot项目,为你介绍Nacos的基本功能以及如何使用Feign进行微服务间的通信。 一、Nacos的基本功能 Nacos是阿里巴巴开源的一个更易于构建云原生应用的动态服务发现、配置管…

Nacos是Dynamic Naming and Configuration Service的缩写。What’s Nacos?

下面结合SpringBoot项目,为你介绍Nacos的基本功能以及如何使用Feign进行微服务间的通信。

一、Nacos的基本功能

Nacos是阿里巴巴开源的一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。它的主要功能包括:

  1. 服务发现与注册:微服务可以在Nacos中注册自己,并发现其他服务。
  2. 配置管理:集中管理所有微服务的配置。
  3. 服务健康监测:检查服务的健康状态。
  4. 动态DNS服务:通过域名来访问服务。

二、依赖配置

要在SpringBoot项目中使用Nacos和Feign,需要在pom.xml中添加以下依赖:

<!-- 服务注册与发现 -->
<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><!-- Feign客户端 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency><!-- Spring Cloud 依赖管理 -->
<dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR12</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.9.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>

三、配置文件

application.propertiesapplication.yml中配置Nacos服务地址:

# 应用名称
spring:application:name: service-consumercloud:nacos:discovery:server-addr: 127.0.0.1:8848  # Nacos服务器地址config:server-addr: 127.0.0.1:8848  # Nacos配置中心地址file-extension: yaml  # 配置文件格式# 端口号
server:port: 8080

四、代码实现

1. 服务提供者(service-provider)

首先创建一个简单的服务提供者,提供一个API接口:

package com.example.provider.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 HelloController {@GetMapping("/hello/{name}")public String sayHello(@PathVariable String name) {return "Hello, " + name + "! I'm from service-provider.";}
}

启动类上添加@EnableDiscoveryClient注解:

package com.example.provider;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDiscoveryClient
public class ServiceProviderApplication {public static void main(String[] args) {SpringApplication.run(ServiceProviderApplication.class, args);}
}
2. 服务消费者(service-consumer)

创建Feign客户端接口,调用服务提供者的API:

package com.example.consumer.feign;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;@FeignClient(name = "service-provider")  // 服务提供者的应用名称
public interface HelloFeignClient {@GetMapping("/hello/{name}")  // 服务提供者的API路径String sayHello(@PathVariable String name);
}

创建Controller,使用Feign客户端调用服务:

package com.example.consumer.controller;import com.example.consumer.feign.HelloFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ConsumerController {@Autowiredprivate HelloFeignClient helloFeignClient;@GetMapping("/call/{name}")public String callProvider(@PathVariable String name) {return helloFeignClient.sayHello(name);}
}

启动类上添加@EnableFeignClients注解:

package com.example.consumer;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableFeignClients
public class ServiceConsumerApplication {public static void main(String[] args) {SpringApplication.run(ServiceConsumerApplication.class, args);}
}

五、Nacos配置中心使用示例

创建一个配置类,读取Nacos配置中心的配置:

package com.example.consumer.config;import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;@Component
@RefreshScope  // 支持配置动态刷新
public class AppConfig {@Value("${app.name:default}")private String appName;@Value("${app.version:1.0.0}")private String appVersion;public String getAppName() {return appName;}public String getAppVersion() {return appVersion;}
}

创建Controller,获取配置信息:

package com.example.consumer.controller;import com.example.consumer.config.AppConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ConfigController {@Autowiredprivate AppConfig appConfig;@GetMapping("/config")public String getConfig() {return "App Name: " + appConfig.getAppName() + ", Version: " + appConfig.getAppVersion();}
}

在Nacos配置中心创建配置文件service-consumer.yaml

app:name: MyServiceConsumerversion: 1.0.1

六、测试步骤

  1. 启动Nacos服务器(下载并解压Nacos,运行startup.sh -m standalone)。
  2. 启动服务提供者(service-provider)。
  3. 启动服务消费者(service-consumer)。
  4. 访问服务消费者的API:http://localhost:8080/call/World,会返回Hello, World! I'm from service-provider.
  5. 访问配置信息:http://localhost:8080/config,会返回Nacos配置中心的配置信息。

通过上述示例,你可以看到Nacos的服务注册与发现、配置管理功能,以及Feign如何简化微服务间的通信。

http://www.dtcms.com/wzjs/151105.html

相关文章:

  • 公司建网站制作平台国内推广平台
  • 南京网站开发就业培训课程百度怎么进入官方网站
  • 郑州官网seogoogle seo教程
  • 竞价代运营公司哪家好排名优化外包公司
  • 网站建设任务分解网站如何进行seo
  • 有了域名后怎样做网站百度推广运营工作是什么
  • 用美国服务器做中国盗版网站要看网的域名是多少
  • 做金融的喜欢逛哪些网站百度seo推广计划类型包含
  • 免费单页网站建设深圳seo顾问
  • 昆明建网站的公司西安seo哪家好
  • 如何自己做资源网站整站优化代理
  • 做免费小说网站怎样赚钱怎么注册网址
  • 个人网站模板html网络热词有哪些
  • 用阿里云怎么建网站百度文库官网入口
  • asp.net 企业网站后台管理系统源码武汉企业网站推广
  • 四川网站建设的公司排名太原网站优化
  • 网页游戏开服表今日windows优化大师兑换码
  • 那个网站专做文具批发大片ppt免费下载安装
  • 设计师个人网站欣赏 中国企业官网推广
  • 天津建网站沈阳百度seo排名优化软件
  • 全国卫生计生机构建设管理系统网站seo运营是做什么的
  • 天津网站建设推广在哪里找软件开发公司
  • 什么样的公司愿意做网站网站的优化从哪里进行
  • 做网站推广的需要了解哪些知识成都建设网官网
  • 做电影网站侵权吗想做一个网站
  • 网站用什么空间好班级优化大师免费下载学生版
  • 最新网站建设软件站长工具网
  • 什么网站做视频赚钱百度关键词排名优化
  • 番禺区成都搜索优化整站优化
  • 织梦网站备份几种方法网络营销推广渠道