搭建spring-cloud项目
第一步:服务中心
服务中心又叫注册中心,管理各种服务功能包括服务的注册、发现、熔断、负载、降级等,比如dubbo admin后台的各种功能。
spring-cloud默认使用Eureka
Eureka有三个角色组成:
- Eureka Server:提供服务注册和发现
- Service Provider:服务提供方 将自身服务注册到Eureka,从而使服务消费方能够找到
- Service Consumer:服务消费方 从Eureka获取注册服务列表,从而能够消费服务
1.创建maven项目
2.在pom中添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3、添加启动代码中添加@EnableEurekaServer
注解
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaApplication.class, args);
}
}
4、配置文件
在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,在application.properties
添加以下配置:
spring.application.name=spring-cloud-eureka
server.port=8000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
eureka.client.register-with-eureka
:表示是否将自己注册到Eureka Server,默认为true。eureka.client.fetch-registry
:表示是否从Eureka Server获取注册信息,默认为true。eureka.client.serviceUrl.defaultZone
:设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8000/eureka ;多个地址可使用 , 分隔。
启动工程后,访问:http://localhost:8000/ 其中还没有发现任何服务
第二步:提供服务
1.创建maven项目
2.pom配置
<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>
3.配置文件
application.properties配置如下:
spring.application.name=spring-cloud-producer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
4.启动类
启动类中添加@EnableDiscoveryClient
注解
@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
5.提供服务编写controller
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index(@RequestParam String name) {
return "hello "+name+",this is first messge";
}
}
添加@EnableDiscoveryClient
注解后,项目就具有了服务注册的功能。启动工程后,就可以在注册中心的页面看到SPRING-CLOUD-PRODUCER服务。
第三部:服务调用
1.同样创建一个mave项目
2.pom配置
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
3.配置文件
application.properties配置如下:
spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
4.启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
@EnableDiscoveryClient
:启用服务注册与发现@EnableFeignClients
:启用feign进行远程调用
5.feign调用实现
@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {
@RequestMapping(value = "/hello")
public String hello(@RequestParam(value = "name") String name);
}
name:远程服务名,及spring.application.name配置的名称
此类中的方法和远程服务中contoller中的方法名和参数需保持一致。
6.web层调用远程服务
将HelloRemote注入到controller层,像普通方法一样去调用即可。
@RestController
public class ConsumerController {
@Autowired
HelloRemote HelloRemote;
@RequestMapping("/hello/{name}")
public String index(@PathVariable("name") String name) {
return HelloRemote.hello(name);
}
}
第三步 调用测试
依次启动spring-cloud-eureka、spring-cloud-producer、spring-cloud-consumer三个项目
先输入:http://localhost:9000/hello?name=neo
检查spring-cloud-producer服务是否正常
返回:hello neo,this is first messge
说明spring-cloud-producer正常启动,提供的服务也正常。
浏览器中输入:http://localhost:9001/hello/neo
返回:hello neo,this is first messge
说明客户端已经成功的通过feign调用了远程服务hello,并且将结果返回到了浏览器。
文章来源:http://www.ityouknow.com/springcloud/2017/05/12/eureka-provider-constomer.html