SpringCloud 首先要解决的就是服务注册。
服务注册用例图:
上图分析:
三个角色,服务方,业务提供方,业务消费方
服务方:开启注册服务,暴露服务注册地址和端口
业务提供方:按照服务方提供的ip地址、端口进行服务注册(可以是多个提供方,业务功能一样,这是为了防止单体宕机,使用这个集群,多个提供方他们注册的业务名都一样,具体调用是通过轮询算法进行调用,见下面代码注解的 @LoadBalanced)
业务消费者:按照服务方提供的ip地址、端口进行服务注册,通过提供方在业务注册的注册名进行远程调用(具体调用代码实例使用RestTemplate)
代码:
1. 提供方:6001导入相对应的 pom 依赖:
springcloud com.lidantao 1.0-SNAPSHOT 4.0.0 cloud-consul-provider-60018 8 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatororg.projectlombok lombokorg.springframework.cloud spring-cloud-starter-consul-discovery
yml 配置文件
server:
port: 6001
spring:
application:
name: consul-provider
cloud:
consul:
# 注册的地址
host: localhost
# 注册的端口
port: 8500
discovery:
# 对外暴露的服务名称
service-name: ${spring.application.name}
启动类
package com.lidantao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConsulProviderMain6001 {
public static void main(String[] args) {
SpringApplication.run(ConsulProviderMain6001.class, args);
}
}
controller
package com.lidantao.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Value("${server.port}")
private String consul_port;
@RequestMapping("/provider/getTest")
public String getTest(){
return "consul : " + consul_port;
}
}
2. 提供方:6002
导入相对应的 pom 依赖:
springcloud com.lidantao 1.0-SNAPSHOT 4.0.0 cloud-consul-provider-60028 8 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatororg.projectlombok lombokorg.springframework.cloud spring-cloud-starter-consul-discovery
yml 配置文件
server:
port: 6002
spring:
application:
name: consul-provider
cloud:
consul:
# 注册的地址
host: localhost
# 注册的端口
port: 8500
discovery:
# 对外暴露的服务名称
service-name: ${spring.application.name}
启动类
package com.lidantao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConsulProviderMain6002 {
public static void main(String[] args) {
SpringApplication.run(ConsulProviderMain6002.class, args);
}
}
controller
package com.lidantao.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Value("${server.port}")
private String consul_port;
@RequestMapping("/provider/getTest")
public String getTest(){
return "consul : " + consul_port;
}
}
3. 消费方:60
导入相对应的 pom 依赖:
springcloud com.lidantao 1.0-SNAPSHOT 4.0.0 cloud-consul-consumer-608 8 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatororg.projectlombok lombokorg.springframework.cloud spring-cloud-starter-consul-discovery
yml 配置文件
server:
port: 60
spring:
application:
name: consul-consumer
cloud:
consul:
# 注册的地址
host: localhost
# 注册的端口
port: 8500
discovery:
# 对外暴露的服务名称
service-name: ${spring.application.name}
启动类
package com.lidantao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConsulConsumerMain60 {
public static void main(String[] args) {
SpringApplication.run(ConsulConsumerMain60.class, args);
}
}
controller
package com.lidantao.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class TestController {
@Autowired
private RestTemplate restTemplate;
private String PROVIDER_URL = "http://consul-provider/provider/getTest";
@RequestMapping("/consumer/getTest")
public String getTest(){
return restTemplate.getForObject(PROVIDER_URL, String.class);
}
}
configuration
package com.lidantao.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class MyConfiguration {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
4. consul 注册页面
5. 服务请求页面



