Spring Eureka的使用入门--服务端与客户端

接上篇:

 Eureka作为注册中心,连接服务端与客户端;

服务端:

 依赖包:

apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

ext {
	springCloudVersion = 'Edgware.SR4'
}

dependencies {
	compile 'org.springframework.boot:spring-boot-starter-web'
	compile 'org.springframework.boot:spring-boot-starter-actuator'
	
    compile 'org.springframework.cloud:spring-cloud-starter-eureka'
    compile 'org.springframework.cloud:spring-cloud-config-client'
    compile 'org.springframework.cloud:spring-cloud-starter-bus-amqp'

    compile 'org.springframework:springloaded'
    compile 'org.springframework.boot:spring-boot-devtools'
}

dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
	}
}

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class ServerApplication {
	
	public static void main(String[] args) {
		SpringApplication.run(ServerApplication .class, args);
	}
}

配置文件app.yml

server:
  port: 1800
      
eureka:
  client: 
    service-url: 
      defaultZone: http://localhost:8761/eureka
  instance:
    instance-id: server
    prefer-ip-address: true
 
info: 
  app.name: a-server
  company.name: www.*.com

核心代码:服务提供者

import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;


@FeignClient(name = "hello", fallbackFactory = HelloFallbackFactory.class)
@ResponseBody
public interface HelloApi {

	@PostMapping(path = "/api/hello")
	public String sayHI(@RequestBody SayHiRequest request);


}

服务已接想口形式提供,注册到Eurka注册中心里:

import org.springframework.stereotype.Component;
import feign.hystrix.FallbackFactory;

@Component
public class HelloFallbackFactory implements FallbackFactory<HelloApi> {

	@Override
	public HelloApicreate(Throwable cause) {
		return new HelloApi() {

			@Override
			public String sayHi(SayHiRequest request) {
				// TODO Auto-generated method stub
				
			}

		};
	}

}

  接口实现:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;

@Controller
public class HelloApiImpl implements HelloApi {

	@Override
	public String sayHi(@RequestBody @Validated SayHiRequest request) {
		//do somenting
            return "";
	}

}

 服务端搭建完成;

微服务架构里,接口一般抽象出来,将接口和接口实现抽离,放到不同的服务里面;

启动服务,当我注册中心htttp://127.0.0.1:8761/eureka 查看服务注册情况;

 

客户端:

    通过注册中心查找服务,进行服务调用;

    依赖包:重点是引入接口方提供jar包

apply plugin: 'io.spring.dependency-management'

dependencies {
    compile 'org.springframework.cloud:spring-cloud-starter-eureka'
    compile 'org.springframework.cloud:spring-cloud-starter-ribbon'
    compile 'org.springframework.cloud:spring-cloud-starter-feign'
    compile 'org.springframework.cloud:spring-cloud-starter-config'
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-actuator'
    
	compile(project(':hello-api'))
}

ext {
	springCloudVersion = 'Edgware.SR4'
}

dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
	}
}

 启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.feign.EnableFeignClients;


@SpringCloudApplication
@EnableFeignClients
public class ClientApplication {

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

}

 调用服务类:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api/test/")
public class TestRS {

	@Autowired
	private TestService _testService;

	@RequestMapping(value = "/say", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseBase<String> test() {
String test = _testService.sayHi(); return new ResponseBase<String>("0", "success", ""); } }

@service
public class TestService {
	@Autowired
	private HelloApi _helloApi;

	public String sayHi() {
String test = _helloApi.sayHi(); return test; } }

配置文件app.yml

server:
  port: 1668
  contextPath: /hello

eureka:
  client:
    service-url: 
      defaultZone: http://127.0.0.1:8761/eureka
  instance:
    prefer-ip-address: true
    
info: 
  app.name: hello

feign:
  client:
    config:
      default:
        connectTimeout: 60000
        readTimeout: 60000

 启动服务,访问注册中心查看是否注册成功;调用接口测试;

posted @ 2018-08-31 16:27  凉城  阅读(1011)  评论(0编辑  收藏  举报