玩转SpringCloud(F版本) 一.服务的注册与发现(Eureka)
一.服务的注册与发现(Eureka)
spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等
1. 主项目:版本控制
Springboot集中声明
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> </parent>
Springcloud版本
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
主项目管理的依赖Jar:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Springboot组件:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
2.创建2个model工程
一个model工程作为服务注册中心,即Eureka Server,另一个作为Eureka Client。
1)创建服务注册中心(Eureka Server)
项目架构:
引入主项目:
<parent> <groupId>com.fsdm</groupId> <artifactId>SpringCloud_test1</artifactId> <version>1.0-SNAPSHOT</version> </parent>
引入spring-cloud-starter-netflix-eureka-server的依赖:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
Application启动类:
@EnableEurekaServer @SpringBootApplication public class Demo1Application { public static void main(String[] args) { SpringApplication.run(Demo1Application.class, args); } }
注解解析:
@SpringBootApplication
1. 复合注解主要包括@ComponentScan,和@SpringBootConfiguration,@EnableAutoConfiguration。
2. @SpringBootConfiguration标注当前类是配置类
3. @EnableAutoConfiguration启动自动的配置,根据你添加的jar包来配置你项目的默认配置
4. @EnableAutoConfiguration扫描当前包及其子包下被@Component,@Controller,@Service,@Repository注解标记的类并纳入到spring容器中进行管理
@EnableEurekaServer
1. 表明这是一个EurekaServer(服务注册中心)
2. 配合yml文件使用:
Eureka:
Client:
registerWithEureka: false
fetchRegistry: false
yml配置:
server: port: 8761 eureka: instance: hostname: localhost client: # 表明自己是一个eureka server. registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ spring: application: name: eurka-server
启动程序,访问http://localhost:8761/
当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。
No application available 表示没有服务被发现
因为我们还没有注册服务当然没有发现服务啦,
2)创建一个服务提供者 (eureka client)
项目架构:
引入主项目:
<parent> <groupId>com.fsdm</groupId> <artifactId>SpringCloud_test1</artifactId> <version>1.0-SNAPSHOT</version> </parent>
需要的jar以及组件:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
Application启动类:
@SpringBootApplication @EnableEurekaClient @RestController public class Demo2Application { public static void main(String[] args) { SpringApplication.run(Demo2Application.class, args); } @Value("${server.port}") String port; @RequestMapping("/hi") public String home(@RequestParam String name) { return "hi "+name+",i am from port:" +port; } }
注解解析:
@EnableEurekaClient
1. 表明这是一个EurekaClient(服务提供者)
2. 基于spring-cloud-netflix,只能为eureka作用。
@RestController
1. 复合注解主要包括@Controller和@ResponseBody
2. 标注controller层,可供url访问
3. 无法返回jsp页面,或者html,配置的视图解InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
yml配置:
server: port: 8762 spring: application: #工程名称 name: service-hi eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。
启动这个项目,刷新http://localhost:8761/
发现一个服务已经注册在服务中了,服务名为SERVICE-HI ,端口为8762
你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI
关于红色字体的警告解释:
https://www.cnblogs.com/breath-taking/articles/7940364.html
访问http://localhost:8762/hi?name=fsdm
未完,待续。。。
本文来自博客园,作者:房上的猫,转载请注明原文链接:https://www.cnblogs.com/lsy131479/p/9613755.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库