标签:entity toc view 方法调用 one 需要 target bin 访问
一. soa简单介绍
面向服务的架构(SOA)是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来。SOA是解决复杂业务模块,提高扩展性,维护性,可伸缩性的最基本方案。我们的业务服务可以根据需求通过网络对松散耦合的粗粒度应用组件进行分布式部署、组合和使用。服务层是SOA的基础,可以直接被应用调用,不同的服务通过不同的团队来维护。
有效的业务分割是soa基础中的基础
二.soa基本架构图(以spring-cloud为例)
1.注册中心:通常情况下是以接口为契约,保存服务提供方暴露的服务信息,常见的注册中心有zookeeper eureka
2.服务提供方:提供服务者
3.消费方:当需要调用远程服务接口时,必须在注册中心发现服务找到服务提供者,从而进行远程方法调用
三.SpringCloud
Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线)。使用Spring Cloud开发人员可以快速地支持实现这些模式的服务和应用程序。
gradle配置:
buildscript { ext { springBootVersion = ‘1.5.8.RELEASE‘ } repositories { maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } allprojects { apply plugin: ‘java‘ apply plugin: ‘idea‘ apply plugin: ‘org.springframework.boot‘ sourceSets { main { java { srcDirs = ["src/main/java"] } resources { srcDirs = ["src/main/resources"] } } } repositories { maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } } ext { springCloudVersion = ‘Dalston.SR3‘ } dependencies { compile(‘org.springframework.cloud:spring-cloud-starter-config‘) compile(‘org.springframework.cloud:spring-cloud-config-server‘) compile ‘org.springframework.cloud:spring-cloud-starter-eureka-server‘ compile ‘org.springframework.boot:spring-boot-starter-actuator‘ compile ‘org.springframework.cloud:spring-cloud-starter-ribbon‘ compile ‘org.springframework.boot:spring-boot-starter-web‘ compile(‘org.springframework.boot:spring-boot-starter-cloud-connectors‘) testCompile(‘org.springframework.boot:spring-boot-starter-test‘) } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } } } group = ‘com.example‘ version = ‘0.0.1-SNAPSHOT‘ sourceCompatibility = 1.8 targetCompatibility = 1.8
当gradle下载完所有jar包时,依次创建一下几个项目模块:
这里说明一下:
register-center:注册中心
service-api:接口契约
service-provider:服务提供方
service-consumer:服务消费方
四.实现步骤
4.1 实现注册中心:
application.yml配置:
server: port: 8000 eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:8000/eureka/
register-with-eureka:false
fetch-registry: false
service-url:注册中心访问的地址
程序入口类:
package com.bdqn.springcloud.study.provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class RegisterCenterProvider { public static void main(String[] args) { SpringApplication.run(RegisterCenterProvider.class, args); } }
@EnableEurekaServer 激活Eureka服务器相关配置
4.2 实现service-api
定义暴露服务的接口契约
package com.bdqn.springcloud.study.api; import com.bdqn.springcloud.study.dto.UserDTO; /** * UserService测试接口 * * @author chen.nie * @date 2017/11/13 */ public interface UserService { UserDTO findUser(); }
定义DTO
package com.bdqn.springcloud.study.dto; public class UserDTO { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
4.3 实现服务提供方:
gradle配置:先依赖service-api
dependencies{ compile project(":service-api") }
application-yml的配置:
server: port: 8001 spring: application: name: "service-provider-demo" eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://localhost:8000/eureka/
注意:这里面要配置应用名称 注册中心的服务地址
接口实现类:
package com.bdqn.springcloud.study.provider.impl; import com.bdqn.springcloud.study.api.UserService; import com.bdqn.springcloud.study.dto.UserDTO; import org.springframework.stereotype.Service; @Service("userService") public class UserServiceImpl implements UserService { @Override public UserDTO findUser() { UserDTO userDTO = new UserDTO(); userDTO.setName("张三丰"); userDTO.setId(10); return userDTO; } }
Controller:
package com.bdqn.springcloud.study.provider.controller; import com.bdqn.springcloud.study.api.UserService; import com.bdqn.springcloud.study.dto.UserDTO; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController public class UserController { @Resource private UserService userService; @GetMapping("/user") public UserDTO getUser() { return this.userService.findUser(); } }
启动类:
package com.bdqn.springcloud.study.provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class ServiceProvider { public static void main(String[] args) { SpringApplication.run(ServiceProvider.class, args); } }
@EnableDiscoveryClient:用于启用发现客户端实现的注释
打来浏览器访问:http://localhost:8000
我们看到刚才的接口服务已经成功注册至Eureka中心
4.3实现服务消费方:
gradle配置:先依赖service-api
dependencies{ compile project(":service-api") }
application.yml配置:
server: port: 8002 spring: application: name: "service-consumer-demo" eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://localhost:8000/eureka/
编写Controller:
package com.bdqn.springcloud.study.consumer.controller; import com.bdqn.springcloud.study.dto.UserDTO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class ConsumerController { @Autowired private RestTemplate restTemplate; @Autowired private LoadBalancerClient loadBalancerClient; @GetMapping("/getUser") public String getUser() { ResponseEntity<UserDTO> responseEntity = restTemplate.getForEntity("http://SERVICE-PROVIDER-DEMO/user", UserDTO.class); UserDTO userDTO = responseEntity.getBody(); return userDTO.getName(); } }
注意我们通过http://service-provider-demo/user/ 指定service-provider的application name,让系统从注册中心去发现服务。
编写启动项:
package com.bdqn.springcloud.study.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableDiscoveryClient public class ServiceConsumer { @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ServiceConsumer.class, args); } }
在启动项里,我们配置了一个RestTemplate 主意 @LoadBalanced注解修饰的restTemplate,具备负载均衡能力的restTemplate,即每次都会用负载均衡算法,从可用服务列表中,挑一个进行调用。
启动完毕时,我们发现注册中心中多了一个服务:
当我们访问http://localhost:8002/getUser得到如下显示就说明我们的服务运行成功了:
当停止register-center时,我们发现该访问地址依然能够拿到结果,说明消费端本地是有缓存的
标签:entity toc view 方法调用 one 需要 target bin 访问
原文地址:http://www.cnblogs.com/niechen/p/7824967.html