一、搭建注册中心
1、创建一个demo项目,demo点击右键创建module
2、设置项目参数
3、选择spring cloud discovery,一直下一步,最后点击完成,创建注册中心
4、将application.properties文件修改为application.yml(格式比较清晰),复制下面参数
server:
port: 8001 # 服务端口
eureka:
instance:
hostname: localhost # 设置主机名
client:
registerWithEureka: false # 是否向 Eureka 注册服务。该应用为服务注册中心,不需要自注册,设置为 false
fetchRegistry: false # 是否检索服务。该应用为服务注册中心,职责为注册和发现服务,无需检索服务,设置为 false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 注册中心url
spring:
application:
name: eurka-server #服务名称
5、在项目启动类中添加@EnableEurekaServer注解
6、启动项目访问http://localhost:8001/,8001是配置文件中设置的端口号
spring cloud的消费者也可以是提供者,二者只有逻辑上的区别,在spring boot中有两种消费方式:rest+ribbon和Feign
二、spring cloud提供者(rest+ribbon)
1、同理,新建module
2、创建ribbon,不要忘记将spring cloud discovery也勾选上,否则将缺少jar包,一部分注解不显示
3、application.yml配置文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8001/eureka/
server:
port: 8002
spring:
application:
name: ribbon-provider
4、启动类添加@EnableEurekaClient注册服务,然后注入RestTemplate对象,@LoadBalanced表示开启负载均衡
package com.cloud.ribbonprovider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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
@EnableEurekaClient
public class RibbonProviderApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonProviderApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
View Code
5、创建controller和service
package com.cloud.ribbonprovider.controller;
import com.cloud.ribbonprovider.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProviderController {
@Autowired
ProviderService providerService;
@GetMapping(value = "/index")
public String index() {
return providerService.getProvider();
}
}
View Code
package com.cloud.ribbonprovider.service;
import org.springframework.stereotype.Service;
@Service
public class ProviderService {
public String getProvider() {
return "ribbon provider";
}
}
View Code
6、启动项目,访问提供者http://localhost:8002/index,可以看到返回结果,接着创建消费者来获取提供者的内容
三、spring cloud消费者(rest+ribbon)
1、新建module
2、勾选ribbon,步骤同提供者
3、application.yml配置文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8001/eureka/
server:
port: 8003
spring:
application:
name: ribbon-consumer
4、启动类配置
package com.cloud.ribbonconsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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
@EnableEurekaClient
public class RibbonConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
View Code
5、controller和service,注意service需要注入RestTemplate,用来访问提供者
package com.cloud.ribbonconsumer.controller;
import com.cloud.ribbonconsumer.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
@Autowired
ConsumerService consumerService;
@GetMapping(value = "/index")
public String index() {
return "ribbon consumer/ " + consumerService.getConsumer();
}
}
View Code
package com.cloud.ribbonconsumer.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ConsumerService {
@Autowired
RestTemplate restTemplate;
public String getConsumer() {
return restTemplate.getForObject("http://ribbon-provider/index", String.class);
}
}
View Code
6、启动项目,访问消费者http://localhost:8003/index,在返回的结果中,ribbon consumer/属于消费者自身的信息,ribbon provider属于提供者提供的内容,简单的springcloud搭建成功
7、访问注册中心http://localhost:8001/,可以看的提供者和消费者都已经注册成功
四、Feign消费
1、新建module,勾选openfeign
2、application.yml配置文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8001/eureka/
server:
port: 8004
spring:
application:
name: feign-consumer
3、启动类添加@EnableDiscoveryClient和@EnableFeignClients注解
package com.cloud.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
View Code
4、使用feign调用ribbon-consumer(既是消费者也是提供者),添加@FeignClient注解即可,value的意思就是目标服务器,可以直接用yml中配置的application名称
package com.cloud.feign.feignrpc;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(value = "ribbon-consumer")
public interface GetFeign {
@GetMapping(value = "index")
public String getFeign();
}
View Code
5、创建controller和service,service中注入feign的接口
package com.cloud.feign.service;
import com.cloud.feign.feignrpc.GetFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class FeignService {
@Autowired
GetFeign getFeign;
public String getFeign() {
return getFeign.getFeign();
}
}
View Code
package com.cloud.feign.controller;
import com.cloud.feign.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FeignController {
@Autowired
FeignService feignService;
@GetMapping(value = "/feign/index")
public String getFeign() {
return "feign consumer/ " + feignService.getFeign();
}
}
View Code
6、启动项目,访问feign消费者http://localhost:8004/feign/index,其中feign consumer/是feign自身提供的信息,ribbon consumer/ ribbon provider是ribbon consumer提供者的信息
7、再次查看注册中心
到此,简单的微服务构架搭建完成。