springcloud(三):服务提供与调用

上一篇文章咱们介绍了eureka服务注册中心的搭建,这篇文章介绍一下如何使用eureka服务注册中心,搭建一个简单的服务端注册服务,客户端去调用服务使用的案例。java

案例中有三个角色:服务注册中心、服务提供者、服务消费者,其中服务注册中心就是咱们上一篇的eureka单机版启动既可,流程是首先启动注册中心,服务提供者生产服务并注册到服务中心中,消费者从服务中心中获取服务并执行。spring

服务提供

咱们假设服务提供者有一个hello方法,能够根据传入的参数,提供输出“hello xxx,this is first messge”的服务springboot

一、pom包配置

建立一个springboot项目,pom.xml中添加以下配置:app

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>

二、配置文件

application.properties配置以下:框架

spring.application.name=spring-cloud-producer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

参数在上一篇都已经解释过,这里很少说。spring-boot

三、启动类

启动类中添加@EnableDiscoveryClient注解this

@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ProducerApplication.class, args);
	}
}

四、controller

提供hello服务spa

@RestController
public class HelloController {
	
    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return "hello "+name+",this is first messge";
    }
}

添加@EnableDiscoveryClient注解后,项目就具备了服务注册的功能。启动工程后,就能够在注册中心的页面看到SPRING-CLOUD-PRODUCER服务。code

到此服务提供者配置就完成了。server

愿意了解框架技术或者源码的朋友直接求求交流分享技术:贰一四七七七五六叁叁

相关文章
相关标签/搜索