springcloud(二)服务的消费者(eureka)

文章目录

      • 服务的消费者
          • 1.启动eureka-server跟provider(参考我的上一篇文章[springcloud(一)服务的注册与治理(eureka)](https://blog.csdn.net/JanHezz/article/details/88994585))
          • 2.新建一个springboot工程
          • 3.书写项目核心配置文件
          • 4.测试
            • 1.编写接口.
            • 2.编写控制器
            • 3.启动测试

服务的消费者

1.启动eureka-server跟provider(参考我的上一篇文章springcloud(一)服务的注册与治理(eureka))
2.新建一个springboot工程

选中以下模块
springcloud(二)服务的消费者(eureka)_第1张图片

pom文件详情如下



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.3.RELEASE
		 
	
	com.janhe
	consumer1
	0.0.1-SNAPSHOT
	consumer1
	Demo project for Spring Boot

	
		1.8
		Greenwich.SR1
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-client
		
		
			org.springframework.cloud
			spring-cloud-starter-openfeign
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		

	
		
			com.netflix.hystrix
			hystrix-javanica
			RELEASE
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



3.书写项目核心配置文件

详细配置如下


    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:9000/eureka/
    server:
      port: 8050
    spring:
      application:
        name: serice-custum

``

4.测试
1.编写接口.
package com.janhe.consumer1.service;

import com.janhe.consumer1.service.Hystrix.HelloServiceFeedBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "service-provider")//指定服务的提供者
@Service
public interface HelloService {

     @RequestMapping(value = "/hello", method = RequestMethod.GET)
     String sayHiFromClientOne(@RequestParam(value = "name") String name);


}

2.编写控制器
package com.janhe.consumer1.controller;

import com.janhe.consumer1.service.HelloService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Auther: Jan 
 * @Date: 2019-4-1 10:01
 * @Description:
 */
@RestController
public class HelloController {

    @Autowired
    HelloService helloService;
    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "/hi", method = RequestMethod.GET)

    public String sayHi(@RequestParam String name) {
        return helloService.sayHiFromClientOne(name);
    }


}

3.启动测试

http://localhost:8050/hi?name=bbb


你可能感兴趣的:(springclound,微服务)