微服务初体验,学习,创建提供者

上篇文章我们介绍了微服务注册中心的创建,以及详细讲解,链接如何搭建注册中心

https://blog.csdn.net/qq_41426763/article/details/101049964

这篇文章我们来介绍一下提供者:

创建接口,运行并发布到注册中心
实现步骤:

1、依赖jar
<dependency> 
<groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
2、编写代码

控制器层(随便写点内容)

@RestController 
public class HelloController { 

@GetMapping("/provider/hello/first.do") 
public String first(){
 System.out.println("提供者:"+System.currentTimeMillis());
  return "七夕"; } 
  }
3、开关类配置

在开关类上使用注解:@EnableEurekaClient或者@EnableDiscoveryClient //注册服务 标记这是客户端

@SpringBootApplication @EnableDiscoveryClient //注册服务 
public class HelloApplication { 
public static void main(String[] args) { SpringApplication.run(HelloApplication.class,args);
 } 
 }
4、全局配置文件设置

application.yml中:

server: 
	port: 8801 
eureka: 
	client: 
		serviceUrl: 
			defaultZone: http://localhost:8761/eureka/ 
spring: 
	application: 
		name: HelloProvider
5、启动并测试

运行开关类,查看注册中心页面,观察服务是否注册

在这里插入图片描述
下篇文章我们来聊聊服务消费者

发布了20 篇原创文章 · 获赞 36 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41426763/article/details/101106379